views.py
6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated, AllowAny
from rest_framework.decorators import api_view, permission_classes
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 routine_manager.functions import check_sequence_file_validity
from rest_framework.request import Request
# Create your views here.
class Users(APIView):
permission_classes = (IsAuthenticated,)
def get(self, request):
content = {"message" : "Hello"}
return Response(content)
@api_view(["GET"])
def user_logout(request):
request.user.auth_token.delete()
user_views.logout()
return Response('User Logged out successfully')
class UserViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed.
"""
queryset = PyrosUser.objects.all().order_by('-date_joined')
serializer_class = UserSerializer
permission_classes = [IsAuthenticated]
http_method_names = ["get"]
def list(self, request):
serializer_context = {
'request': request,
}
queryset = None
current_user = self.request.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"):
queryset = PyrosUser.objects.all().order_by("-created")
else:
sp_of_current_user = user.get_scientific_program()
pyros_users_with_roles = []
for sp in sp_of_current_user:
for sp_period in sp.SP_Periods.all():
for user in SP_Period_User.objects.filter(SP_Period=sp_period).exclude(user=current_user).values_list("user",flat=True):
pyros_users_with_roles.append(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)
serializer = UserSerializer(queryset, context = serializer_context, many=True)
return Response(serializer.data)
class SequenceViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to view their sequences.
"""
queryset = Sequence.objects.all().order_by("-updated")
serializer_class = SequenceSerializer
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 Sequence.objects.all().order_by("-updated")
else:
return Sequence.objects.filter(pyros_user=user).order_by("-updated")
class FullSequenceViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to view their sequences.
"""
queryset = Sequence.objects.all().order_by("-updated")
serializer_class = FullSequenceSerializer
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 Sequence.objects.all().order_by("-updated")
else:
return Sequence.objects.filter(pyros_user=user).order_by("-updated")
class AlbumViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to view their sequences.
"""
queryset = Album.objects.all().order_by("-updated")
serializer_class = AlbumSerializer
permission_classes = [IsAuthenticated]
http_method_names = ["get"]
def get_queryset(self):
"""
This view should return a list of all the albums
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"):
sequences = Sequence.objects.all().order_by("-updated")
else:
sequences = Sequence.objects.filter(pyros_user=user).order_by("-updated")
return Album.objects.filter(sequence__in=sequences).order_by("-updated")
class PlanViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to view their sequences.
"""
queryset = Plan.objects.all().order_by("-updated")
serializer_class = PlanSerializer
permission_classes = [IsAuthenticated]
http_method_names = ["get"]
def get_queryset(self):
"""
This view should return a list of all the plans
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"):
sequences = Sequence.objects.all().order_by("-updated")
else:
sequences = Sequence.objects.filter(pyros_user=user).order_by("-updated")
albums = Album.objects.filter(sequence__in=sequences).order_by("-updated")
return Plan.objects.filter(album__in=albums).order_by("-updated")
@api_view(["PUT"])
def submit_sequence_with_json(request):
sequence_json = request.data
print(sequence_json)
response = check_sequence_file_validity(sequence_json,request)
return Response(response)