Commit 995a2ae6688357513dce02492355eb3337c71fe1
1 parent
44f94be9
Exists in
dev
adding scientific_program to api
Showing
3 changed files
with
78 additions
and
20 deletions
Show diff stats
src/core/pyros_django/api/serializers.py
1 | -from common.models import PyrosUser, Sequence, Plan, Album | 1 | +from common.models import Institute, PyrosUser, ScienceTheme, ScientificProgram, Sequence, Plan, Album |
2 | from rest_framework import serializers | 2 | from rest_framework import serializers |
3 | 3 | ||
4 | + | ||
4 | class UserSerializer(serializers.ModelSerializer): | 5 | class UserSerializer(serializers.ModelSerializer): |
6 | + user_level = serializers.StringRelatedField(many=True) | ||
7 | + | ||
5 | class Meta: | 8 | class Meta: |
6 | model = PyrosUser | 9 | model = PyrosUser |
7 | - fields = ['url', 'username', 'email', 'institute', "user_level", "motive_of_registration"] | 10 | + fields = ['url', 'username', 'email', 'institute', |
11 | + "user_level"] | ||
8 | depth = 1 | 12 | depth = 1 |
9 | 13 | ||
10 | 14 | ||
15 | +class InstituteSerializer(serializers.ModelSerializer): | ||
16 | + class Meta: | ||
17 | + model = Institute | ||
18 | + fields = ["name"] | ||
19 | + | ||
20 | + | ||
21 | +class ScienceThemeSerializer(serializers.ModelSerializer): | ||
22 | + class Meta: | ||
23 | + model = ScienceTheme | ||
24 | + fields = ["name"] | ||
25 | + | ||
26 | + | ||
27 | +class ScientificProgramSerializer(serializers.ModelSerializer): | ||
28 | + sp_pi = UserSerializer(read_only=True) | ||
29 | + institute = InstituteSerializer(read_only=True) | ||
30 | + science_theme = ScienceThemeSerializer(read_only=True) | ||
31 | + | ||
32 | + class Meta: | ||
33 | + model = ScientificProgram | ||
34 | + fields = ["name", "institute", "sp_pi", "description_short", | ||
35 | + "description_long", "science_theme"] | ||
36 | + #fields = "__all__" | ||
37 | + | ||
11 | 38 | ||
12 | class SequenceSerializer(serializers.ModelSerializer): | 39 | class SequenceSerializer(serializers.ModelSerializer): |
13 | pyros_user = UserSerializer(read_only=True) | 40 | pyros_user = UserSerializer(read_only=True) |
41 | + | ||
14 | class Meta: | 42 | class Meta: |
15 | model = Sequence | 43 | model = Sequence |
16 | fields = "__all__" | 44 | fields = "__all__" |
@@ -18,6 +46,7 @@ class SequenceSerializer(serializers.ModelSerializer): | @@ -18,6 +46,7 @@ class SequenceSerializer(serializers.ModelSerializer): | ||
18 | 46 | ||
19 | class AlbumSerializer(serializers.ModelSerializer): | 47 | class AlbumSerializer(serializers.ModelSerializer): |
20 | sequence = SequenceSerializer(read_only=True) | 48 | sequence = SequenceSerializer(read_only=True) |
49 | + | ||
21 | class Meta: | 50 | class Meta: |
22 | model = Album | 51 | model = Album |
23 | fields = "__all__" | 52 | fields = "__all__" |
@@ -25,29 +54,33 @@ class AlbumSerializer(serializers.ModelSerializer): | @@ -25,29 +54,33 @@ class AlbumSerializer(serializers.ModelSerializer): | ||
25 | 54 | ||
26 | class PlanSerializer(serializers.ModelSerializer): | 55 | class PlanSerializer(serializers.ModelSerializer): |
27 | album = AlbumSerializer(read_only=True) | 56 | album = AlbumSerializer(read_only=True) |
57 | + | ||
28 | class Meta: | 58 | class Meta: |
29 | model = Plan | 59 | model = Plan |
30 | fields = "__all__" | 60 | fields = "__all__" |
31 | 61 | ||
62 | + | ||
32 | class SimplePlanSerializer(serializers.ModelSerializer): | 63 | class SimplePlanSerializer(serializers.ModelSerializer): |
33 | - | 64 | + |
34 | class Meta: | 65 | class Meta: |
35 | model = Plan | 66 | model = Plan |
36 | exclude = ["album"] | 67 | exclude = ["album"] |
37 | 68 | ||
69 | + | ||
38 | class SimpleAlbumSerializer(serializers.ModelSerializer): | 70 | class SimpleAlbumSerializer(serializers.ModelSerializer): |
39 | - plans = SimplePlanSerializer(read_only=True,many=True) | 71 | + plans = SimplePlanSerializer(read_only=True, many=True) |
72 | + | ||
40 | class Meta: | 73 | class Meta: |
41 | model = Album | 74 | model = Album |
42 | - fields = ["name","desc","plans"] | 75 | + fields = ["name", "desc", "plans"] |
76 | + | ||
77 | + | ||
43 | class FullSequenceSerializer(serializers.ModelSerializer): | 78 | class FullSequenceSerializer(serializers.ModelSerializer): |
44 | pyros_user = UserSerializer(read_only=True) | 79 | pyros_user = UserSerializer(read_only=True) |
45 | # here albums is the relationmodel with many to many realtion so we have to set many to True | 80 | # here albums is the relationmodel with many to many realtion so we have to set many to True |
46 | - albums = SimpleAlbumSerializer(read_only=True,many=True) | 81 | + albums = SimpleAlbumSerializer(read_only=True, many=True) |
47 | 82 | ||
48 | class Meta: | 83 | class Meta: |
49 | model = Sequence | 84 | model = Sequence |
50 | fields = "__all__" | 85 | fields = "__all__" |
51 | - extra_fields = ["pyros_user","albums"] | ||
52 | - | ||
53 | - | 86 | + extra_fields = ["pyros_user", "albums"] |
src/core/pyros_django/api/urls.py
@@ -9,6 +9,8 @@ router.register(r'sequences', views.SequenceViewSet) | @@ -9,6 +9,8 @@ router.register(r'sequences', views.SequenceViewSet) | ||
9 | router.register(r'albums', views.AlbumViewSet) | 9 | router.register(r'albums', views.AlbumViewSet) |
10 | router.register(r'plans', views.PlanViewSet) | 10 | router.register(r'plans', views.PlanViewSet) |
11 | router.register(r'full_sequences', views.FullSequenceViewSet) | 11 | router.register(r'full_sequences', views.FullSequenceViewSet) |
12 | +router.register(r'scientific_programs', views.ScientificProgramViewSet) | ||
13 | + | ||
12 | urlpatterns = [ | 14 | urlpatterns = [ |
13 | path('', include(router.urls)), | 15 | path('', include(router.urls)), |
14 | path('logout/', views.user_logout, name='logout'), | 16 | path('logout/', views.user_logout, name='logout'), |
src/core/pyros_django/api/views.py
@@ -6,8 +6,8 @@ from rest_framework.permissions import IsAuthenticated, AllowAny | @@ -6,8 +6,8 @@ from rest_framework.permissions import IsAuthenticated, AllowAny | ||
6 | from rest_framework.decorators import api_view, permission_classes, action | 6 | from rest_framework.decorators import api_view, permission_classes, action |
7 | from django.core.validators import ValidationError | 7 | from django.core.validators import ValidationError |
8 | from src.core.pyros_django.user_manager import views as user_views | 8 | from src.core.pyros_django.user_manager import views as user_views |
9 | -from api.serializers import AlbumSerializer, FullSequenceSerializer, PlanSerializer, SequenceSerializer, UserSerializer | ||
10 | -from common.models import PyrosUser, Sequence, Album, Plan, UserLevel, SP_Period_User | 9 | +from api.serializers import AlbumSerializer, FullSequenceSerializer, PlanSerializer, ScientificProgramSerializer, SequenceSerializer, UserSerializer |
10 | +from common.models import PyrosUser, ScientificProgram, Sequence, Album, Plan, UserLevel, SP_Period_User | ||
11 | from routine_manager.functions import check_sequence_file_validity | 11 | from routine_manager.functions import check_sequence_file_validity |
12 | from rest_framework.request import Request | 12 | from rest_framework.request import Request |
13 | from django.db.models import Q | 13 | from django.db.models import Q |
@@ -17,10 +17,11 @@ from src.pyros_logger import log | @@ -17,10 +17,11 @@ from src.pyros_logger import log | ||
17 | # Create your views here. | 17 | # Create your views here. |
18 | 18 | ||
19 | 19 | ||
20 | - | ||
21 | - | ||
22 | @api_view(["GET"]) | 20 | @api_view(["GET"]) |
23 | def user_logout(request): | 21 | def user_logout(request): |
22 | + """ | ||
23 | + Log out user and delete authentification token | ||
24 | + """ | ||
24 | 25 | ||
25 | request.user.auth_token.delete() | 26 | request.user.auth_token.delete() |
26 | 27 | ||
@@ -59,9 +60,10 @@ class UserViewSet(viewsets.ModelViewSet): | @@ -59,9 +60,10 @@ class UserViewSet(viewsets.ModelViewSet): | ||
59 | PyrosUser.objects.get(id=user)) | 60 | PyrosUser.objects.get(id=user)) |
60 | pyros_users_with_roles.append( | 61 | pyros_users_with_roles.append( |
61 | sp_period.scientific_program.sp_pi) | 62 | sp_period.scientific_program.sp_pi) |
62 | - admin_and_unit_users = PyrosUser.objects.filter( | ||
63 | - user_level__name__in=("Unit-PI", "Unit-board", "Admin")).distinct() | ||
64 | - queryset = pyros_users_with_roles + list(admin_and_unit_users) | 63 | + # Include unit_pi and unit_board users |
64 | + unit_users = PyrosUser.objects.filter( | ||
65 | + user_level__name__in=("Unit-PI", "Unit-board")).distinct() | ||
66 | + queryset = pyros_users_with_roles + list(unit_users) | ||
65 | serializer = UserSerializer( | 67 | serializer = UserSerializer( |
66 | queryset, context=serializer_context, many=True) | 68 | queryset, context=serializer_context, many=True) |
67 | return Response(serializer.data) | 69 | return Response(serializer.data) |
@@ -90,9 +92,32 @@ class SequenceViewSet(viewsets.ModelViewSet): | @@ -90,9 +92,32 @@ class SequenceViewSet(viewsets.ModelViewSet): | ||
90 | return Sequence.objects.filter(pyros_user=user).order_by("-updated") | 92 | return Sequence.objects.filter(pyros_user=user).order_by("-updated") |
91 | 93 | ||
92 | 94 | ||
95 | +class ScientificProgramViewSet(viewsets.ModelViewSet): | ||
96 | + """ | ||
97 | + API endpoint that allows users to view their scientific programs. | ||
98 | + """ | ||
99 | + queryset = ScientificProgram.objects.all().order_by("-created") | ||
100 | + serializer_class = ScientificProgramSerializer | ||
101 | + permission_classes = [IsAuthenticated] | ||
102 | + http_method_names = ["get"] | ||
103 | + | ||
104 | + def get_queryset(self): | ||
105 | + """ | ||
106 | + This view should return a list of all the sequences | ||
107 | + for the currently authenticated user. | ||
108 | + """ | ||
109 | + user = self.request.user | ||
110 | + user_role = str(UserLevel.objects.get( | ||
111 | + priority=user.get_priority()).name) | ||
112 | + if user_role in ("Unit-PI", "Unit-board", "Admin"): | ||
113 | + return ScientificProgram.objects.all().order_by("-updated") | ||
114 | + else: | ||
115 | + return user.get_scientific_program().order_by("-updated") | ||
116 | + | ||
117 | + | ||
93 | class FullSequenceViewSet(viewsets.ModelViewSet): | 118 | class FullSequenceViewSet(viewsets.ModelViewSet): |
94 | """ | 119 | """ |
95 | - API endpoint that allows users to view their sequences. | 120 | + API endpoint that allows users to view their full sequences. |
96 | """ | 121 | """ |
97 | queryset = Sequence.objects.all().order_by("-updated") | 122 | queryset = Sequence.objects.all().order_by("-updated") |
98 | serializer_class = FullSequenceSerializer | 123 | serializer_class = FullSequenceSerializer |
@@ -129,7 +154,7 @@ class FullSequenceViewSet(viewsets.ModelViewSet): | @@ -129,7 +154,7 @@ class FullSequenceViewSet(viewsets.ModelViewSet): | ||
129 | 154 | ||
130 | class AlbumViewSet(viewsets.ModelViewSet): | 155 | class AlbumViewSet(viewsets.ModelViewSet): |
131 | """ | 156 | """ |
132 | - API endpoint that allows users to view their sequences. | 157 | + API endpoint that allows users to view their Albums. |
133 | """ | 158 | """ |
134 | queryset = Album.objects.all().order_by("-updated") | 159 | queryset = Album.objects.all().order_by("-updated") |
135 | serializer_class = AlbumSerializer | 160 | serializer_class = AlbumSerializer |
@@ -182,10 +207,8 @@ class PlanViewSet(viewsets.ModelViewSet): | @@ -182,10 +207,8 @@ class PlanViewSet(viewsets.ModelViewSet): | ||
182 | @api_view(["PUT"]) | 207 | @api_view(["PUT"]) |
183 | def submit_sequence_with_json(request): | 208 | def submit_sequence_with_json(request): |
184 | sequence_json = request.data | 209 | sequence_json = request.data |
185 | - print(sequence_json) | ||
186 | response = check_sequence_file_validity(sequence_json, request) | 210 | response = check_sequence_file_validity(sequence_json, request) |
187 | if response.get("succeed") == True: | 211 | if response.get("succeed") == True: |
188 | - print(response) | ||
189 | seq = Sequence.objects.get(id=response.get("sequence_id")) | 212 | seq = Sequence.objects.get(id=response.get("sequence_id")) |
190 | log.info( | 213 | log.info( |
191 | f"User {request.user} did action submit sequence {seq} for period {seq.period} ") | 214 | f"User {request.user} did action submit sequence {seq} for period {seq.period} ") |