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 | 2 | from rest_framework import serializers |
3 | 3 | |
4 | + | |
4 | 5 | class UserSerializer(serializers.ModelSerializer): |
6 | + user_level = serializers.StringRelatedField(many=True) | |
7 | + | |
5 | 8 | class Meta: |
6 | 9 | model = PyrosUser |
7 | - fields = ['url', 'username', 'email', 'institute', "user_level", "motive_of_registration"] | |
10 | + fields = ['url', 'username', 'email', 'institute', | |
11 | + "user_level"] | |
8 | 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 | 39 | class SequenceSerializer(serializers.ModelSerializer): |
13 | 40 | pyros_user = UserSerializer(read_only=True) |
41 | + | |
14 | 42 | class Meta: |
15 | 43 | model = Sequence |
16 | 44 | fields = "__all__" |
... | ... | @@ -18,6 +46,7 @@ class SequenceSerializer(serializers.ModelSerializer): |
18 | 46 | |
19 | 47 | class AlbumSerializer(serializers.ModelSerializer): |
20 | 48 | sequence = SequenceSerializer(read_only=True) |
49 | + | |
21 | 50 | class Meta: |
22 | 51 | model = Album |
23 | 52 | fields = "__all__" |
... | ... | @@ -25,29 +54,33 @@ class AlbumSerializer(serializers.ModelSerializer): |
25 | 54 | |
26 | 55 | class PlanSerializer(serializers.ModelSerializer): |
27 | 56 | album = AlbumSerializer(read_only=True) |
57 | + | |
28 | 58 | class Meta: |
29 | 59 | model = Plan |
30 | 60 | fields = "__all__" |
31 | 61 | |
62 | + | |
32 | 63 | class SimplePlanSerializer(serializers.ModelSerializer): |
33 | - | |
64 | + | |
34 | 65 | class Meta: |
35 | 66 | model = Plan |
36 | 67 | exclude = ["album"] |
37 | 68 | |
69 | + | |
38 | 70 | class SimpleAlbumSerializer(serializers.ModelSerializer): |
39 | - plans = SimplePlanSerializer(read_only=True,many=True) | |
71 | + plans = SimplePlanSerializer(read_only=True, many=True) | |
72 | + | |
40 | 73 | class Meta: |
41 | 74 | model = Album |
42 | - fields = ["name","desc","plans"] | |
75 | + fields = ["name", "desc", "plans"] | |
76 | + | |
77 | + | |
43 | 78 | class FullSequenceSerializer(serializers.ModelSerializer): |
44 | 79 | pyros_user = UserSerializer(read_only=True) |
45 | 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 | 83 | class Meta: |
49 | 84 | model = Sequence |
50 | 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 | 9 | router.register(r'albums', views.AlbumViewSet) |
10 | 10 | router.register(r'plans', views.PlanViewSet) |
11 | 11 | router.register(r'full_sequences', views.FullSequenceViewSet) |
12 | +router.register(r'scientific_programs', views.ScientificProgramViewSet) | |
13 | + | |
12 | 14 | urlpatterns = [ |
13 | 15 | path('', include(router.urls)), |
14 | 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 | 6 | from rest_framework.decorators import api_view, permission_classes, action |
7 | 7 | from django.core.validators import ValidationError |
8 | 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 | 11 | from routine_manager.functions import check_sequence_file_validity |
12 | 12 | from rest_framework.request import Request |
13 | 13 | from django.db.models import Q |
... | ... | @@ -17,10 +17,11 @@ from src.pyros_logger import log |
17 | 17 | # Create your views here. |
18 | 18 | |
19 | 19 | |
20 | - | |
21 | - | |
22 | 20 | @api_view(["GET"]) |
23 | 21 | def user_logout(request): |
22 | + """ | |
23 | + Log out user and delete authentification token | |
24 | + """ | |
24 | 25 | |
25 | 26 | request.user.auth_token.delete() |
26 | 27 | |
... | ... | @@ -59,9 +60,10 @@ class UserViewSet(viewsets.ModelViewSet): |
59 | 60 | PyrosUser.objects.get(id=user)) |
60 | 61 | pyros_users_with_roles.append( |
61 | 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 | 67 | serializer = UserSerializer( |
66 | 68 | queryset, context=serializer_context, many=True) |
67 | 69 | return Response(serializer.data) |
... | ... | @@ -90,9 +92,32 @@ class SequenceViewSet(viewsets.ModelViewSet): |
90 | 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 | 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 | 122 | queryset = Sequence.objects.all().order_by("-updated") |
98 | 123 | serializer_class = FullSequenceSerializer |
... | ... | @@ -129,7 +154,7 @@ class FullSequenceViewSet(viewsets.ModelViewSet): |
129 | 154 | |
130 | 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 | 159 | queryset = Album.objects.all().order_by("-updated") |
135 | 160 | serializer_class = AlbumSerializer |
... | ... | @@ -182,10 +207,8 @@ class PlanViewSet(viewsets.ModelViewSet): |
182 | 207 | @api_view(["PUT"]) |
183 | 208 | def submit_sequence_with_json(request): |
184 | 209 | sequence_json = request.data |
185 | - print(sequence_json) | |
186 | 210 | response = check_sequence_file_validity(sequence_json, request) |
187 | 211 | if response.get("succeed") == True: |
188 | - print(response) | |
189 | 212 | seq = Sequence.objects.get(id=response.get("sequence_id")) |
190 | 213 | log.info( |
191 | 214 | f"User {request.user} did action submit sequence {seq} for period {seq.period} ") | ... | ... |