diff --git a/src/core/pyros_django/api/serializers.py b/src/core/pyros_django/api/serializers.py index bc239b6..0b38c99 100644 --- a/src/core/pyros_django/api/serializers.py +++ b/src/core/pyros_django/api/serializers.py @@ -1,16 +1,44 @@ -from common.models import PyrosUser, Sequence, Plan, Album +from common.models import Institute, PyrosUser, ScienceTheme, ScientificProgram, Sequence, Plan, Album from rest_framework import serializers + class UserSerializer(serializers.ModelSerializer): + user_level = serializers.StringRelatedField(many=True) + class Meta: model = PyrosUser - fields = ['url', 'username', 'email', 'institute', "user_level", "motive_of_registration"] + fields = ['url', 'username', 'email', 'institute', + "user_level"] depth = 1 +class InstituteSerializer(serializers.ModelSerializer): + class Meta: + model = Institute + fields = ["name"] + + +class ScienceThemeSerializer(serializers.ModelSerializer): + class Meta: + model = ScienceTheme + fields = ["name"] + + +class ScientificProgramSerializer(serializers.ModelSerializer): + sp_pi = UserSerializer(read_only=True) + institute = InstituteSerializer(read_only=True) + science_theme = ScienceThemeSerializer(read_only=True) + + class Meta: + model = ScientificProgram + fields = ["name", "institute", "sp_pi", "description_short", + "description_long", "science_theme"] + #fields = "__all__" + class SequenceSerializer(serializers.ModelSerializer): pyros_user = UserSerializer(read_only=True) + class Meta: model = Sequence fields = "__all__" @@ -18,6 +46,7 @@ class SequenceSerializer(serializers.ModelSerializer): class AlbumSerializer(serializers.ModelSerializer): sequence = SequenceSerializer(read_only=True) + class Meta: model = Album fields = "__all__" @@ -25,29 +54,33 @@ class AlbumSerializer(serializers.ModelSerializer): class PlanSerializer(serializers.ModelSerializer): album = AlbumSerializer(read_only=True) + class Meta: model = Plan fields = "__all__" + class SimplePlanSerializer(serializers.ModelSerializer): - + class Meta: model = Plan exclude = ["album"] + class SimpleAlbumSerializer(serializers.ModelSerializer): - plans = SimplePlanSerializer(read_only=True,many=True) + plans = SimplePlanSerializer(read_only=True, many=True) + class Meta: model = Album - fields = ["name","desc","plans"] + fields = ["name", "desc", "plans"] + + class FullSequenceSerializer(serializers.ModelSerializer): pyros_user = UserSerializer(read_only=True) # here albums is the relationmodel with many to many realtion so we have to set many to True - albums = SimpleAlbumSerializer(read_only=True,many=True) + albums = SimpleAlbumSerializer(read_only=True, many=True) class Meta: model = Sequence fields = "__all__" - extra_fields = ["pyros_user","albums"] - - + extra_fields = ["pyros_user", "albums"] diff --git a/src/core/pyros_django/api/urls.py b/src/core/pyros_django/api/urls.py index 289a773..a8b5511 100644 --- a/src/core/pyros_django/api/urls.py +++ b/src/core/pyros_django/api/urls.py @@ -9,6 +9,8 @@ router.register(r'sequences', views.SequenceViewSet) router.register(r'albums', views.AlbumViewSet) router.register(r'plans', views.PlanViewSet) router.register(r'full_sequences', views.FullSequenceViewSet) +router.register(r'scientific_programs', views.ScientificProgramViewSet) + urlpatterns = [ path('', include(router.urls)), path('logout/', views.user_logout, name='logout'), diff --git a/src/core/pyros_django/api/views.py b/src/core/pyros_django/api/views.py index 7a2ba69..a265a3c 100644 --- a/src/core/pyros_django/api/views.py +++ b/src/core/pyros_django/api/views.py @@ -6,8 +6,8 @@ from rest_framework.permissions import IsAuthenticated, AllowAny from rest_framework.decorators import api_view, permission_classes, action from django.core.validators import ValidationError from src.core.pyros_django.user_manager import views as user_views -from api.serializers import AlbumSerializer, FullSequenceSerializer, PlanSerializer, SequenceSerializer, UserSerializer -from common.models import PyrosUser, Sequence, Album, Plan, UserLevel, SP_Period_User +from api.serializers import AlbumSerializer, FullSequenceSerializer, PlanSerializer, ScientificProgramSerializer, SequenceSerializer, UserSerializer +from common.models import PyrosUser, ScientificProgram, Sequence, Album, Plan, UserLevel, SP_Period_User from routine_manager.functions import check_sequence_file_validity from rest_framework.request import Request from django.db.models import Q @@ -17,10 +17,11 @@ from src.pyros_logger import log # Create your views here. - - @api_view(["GET"]) def user_logout(request): + """ + Log out user and delete authentification token + """ request.user.auth_token.delete() @@ -59,9 +60,10 @@ class UserViewSet(viewsets.ModelViewSet): PyrosUser.objects.get(id=user)) pyros_users_with_roles.append( sp_period.scientific_program.sp_pi) - admin_and_unit_users = PyrosUser.objects.filter( - user_level__name__in=("Unit-PI", "Unit-board", "Admin")).distinct() - queryset = pyros_users_with_roles + list(admin_and_unit_users) + # Include unit_pi and unit_board users + unit_users = PyrosUser.objects.filter( + user_level__name__in=("Unit-PI", "Unit-board")).distinct() + queryset = pyros_users_with_roles + list(unit_users) serializer = UserSerializer( queryset, context=serializer_context, many=True) return Response(serializer.data) @@ -90,9 +92,32 @@ class SequenceViewSet(viewsets.ModelViewSet): return Sequence.objects.filter(pyros_user=user).order_by("-updated") +class ScientificProgramViewSet(viewsets.ModelViewSet): + """ + API endpoint that allows users to view their scientific programs. + """ + queryset = ScientificProgram.objects.all().order_by("-created") + serializer_class = ScientificProgramSerializer + permission_classes = [IsAuthenticated] + http_method_names = ["get"] + + def get_queryset(self): + """ + This view should return a list of all the sequences + for the currently authenticated user. + """ + user = self.request.user + user_role = str(UserLevel.objects.get( + priority=user.get_priority()).name) + if user_role in ("Unit-PI", "Unit-board", "Admin"): + return ScientificProgram.objects.all().order_by("-updated") + else: + return user.get_scientific_program().order_by("-updated") + + class FullSequenceViewSet(viewsets.ModelViewSet): """ - API endpoint that allows users to view their sequences. + API endpoint that allows users to view their full sequences. """ queryset = Sequence.objects.all().order_by("-updated") serializer_class = FullSequenceSerializer @@ -129,7 +154,7 @@ class FullSequenceViewSet(viewsets.ModelViewSet): class AlbumViewSet(viewsets.ModelViewSet): """ - API endpoint that allows users to view their sequences. + API endpoint that allows users to view their Albums. """ queryset = Album.objects.all().order_by("-updated") serializer_class = AlbumSerializer @@ -182,10 +207,8 @@ class PlanViewSet(viewsets.ModelViewSet): @api_view(["PUT"]) def submit_sequence_with_json(request): sequence_json = request.data - print(sequence_json) response = check_sequence_file_validity(sequence_json, request) if response.get("succeed") == True: - print(response) seq = Sequence.objects.get(id=response.get("sequence_id")) log.info( f"User {request.user} did action submit sequence {seq} for period {seq.period} ") -- libgit2 0.21.2