views.py
20.8 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# Standard imports
from datetime import datetime, timezone, timedelta
# Django imports
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import viewsets, renderers
from rest_framework.permissions import IsAuthenticated, AllowAny
from rest_framework.decorators import api_view, permission_classes, action
from django.core.validators import ValidationError
from rest_framework.request import Request
from django.db.models import Q
# Project imports
from src.core.pyros_django.user_mgmt import views as user_views
from api.serializers import AgentSurveySerializer, AlbumSerializer, FullSequenceSerializer, PlanSerializer, SPPeriodSerializer, ScientificProgramSerializer, SequenceSerializer, UserSerializer, AgentCmdSerializer, EnvDataSerializer, SensorDataLastSerializer
from user_mgmt.models import PyrosUser, UserLevel, SP_Period, ScientificProgram, SP_Period_User
#from scp_mgmt.models import SP_Period, ScientificProgram, SP_Period_User
from majordome.models import AgentSurvey, AgentCmd
from env_monitor.models import Env_data, Sensors_data, Sensors_data_last_value
from seq_submit.models import Sequence, Album, Plan
from seq_submit.functions import check_sequence_file_validity_and_save, create_sequence_pickle
from src.pyros_logger import log
import yaml
# Create your views here.
@api_view(["GET"])
def user_logout(request):
"""
Log out user and delete authentification token
"""
request.user.auth_token.delete()
user_views.logout(request)
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", "Admin"):
queryset = PyrosUser.objects.all().order_by("-created")
else:
sp_of_current_user = user.get_scientific_programs()
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)
# 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)
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", "Admin"):
return Sequence.objects.all().order_by("-updated")
else:
sp_of_user = user.get_scientific_programs()
return Sequence.objects.filter(scientific_program__in=sp_of_user).order_by("-updated")
#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", "Admin"):
return ScientificProgram.objects.all().order_by("-updated")
else:
return user.get_scientific_programs().order_by("-updated")
class SPPeriodViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to view their sp_periods
"""
queryset = SP_Period.objects.all()
serializer_class = SPPeriodSerializer
permission_classes = [IsAuthenticated]
http_method_names = ["get"]
def get_queryset(self):
"""
This view should return a list of all the sp_period
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", "Admin"):
return SP_Period.objects.all().order_by("-scientific_program")
else:
user_scientific_programs = user.get_scientific_programs()
return SP_Period.objects.filter(scientific_program__in=user_scientific_programs).order_by("-scientific_program")
class FullSequenceViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to view their full sequences (Sequence with their albums and plans).
Parameters :
- scientific_program -- Filter by scientific program id \n
- night_id -- Filter by night_id (YYMMDD)
"""
queryset = Sequence.objects.all().order_by("-updated")
serializer_class = FullSequenceSerializer
permission_classes = [IsAuthenticated]
http_method_names = ["get"]
# def retrieve(self, request, pk=None):
# pk = pk.lstrip("0")
# return request
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)
scientific_program = self.request.query_params.get("scientific_program")
night_id = self.request.query_params.get("night_id")
if user_role in ("Unit-PI", "Admin"):
queryset = Sequence.objects.all().order_by("-updated")
else:
queryset = Sequence.objects.filter(pyros_user=user).order_by("-updated")
# Apply filters
if scientific_program:
queryset = queryset.filter(scientific_program=scientific_program)
if night_id:
queryset = queryset.filter(night_id=night_id)
return queryset
@action(detail=False, methods=["get"])
def get_sequences_for_period(self, request):
"""
Return the list of sequences corresponding to the requested period
"""
params = request.query_params
start_date = datetime.strptime(params.get("start_date"), "%d/%m/%Y")
end_date = datetime.strptime(params.get("end_date"), "%d/%m/%Y")
queryset = Sequence.objects.filter(
Q(start_date__gte=start_date) | Q(start_date__lte=end_date))
serializer = self.get_serializer(queryset, many=True)
response = Response(serializer.data)
return response
class AlbumViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to view their Albums.
Parameters :
sequence -- Filter by Sequence(s) id(s)
"""
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", "Admin"):
sequences = Sequence.objects.all().order_by("-updated")
else:
sequences = Sequence.objects.filter(
pyros_user=user).order_by("-updated")
sequence_id = self.request.query_params.get("sequence")
if sequence_id:
sequence_id = sequence_id.split(",")
sequences = sequences.filter(id__in=sequence_id)
return Album.objects.filter(sequence__in=sequences).order_by("-updated")
class PlanViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to view their plans.
Parameters :
sequence -- Filter by Sequence(s) id(s)
album -- Filter by Album(s) id(s)
"""
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", "Admin"):
sequences = Sequence.objects.all().order_by("-updated")
else:
sequences = Sequence.objects.filter(
pyros_user=user).order_by("-updated")
sequence_id = self.request.query_params.get("sequence")
album_ids = self.request.query_params.get("album")
if sequence_id:
sequence_id = sequence_id.split(",")
sequences = sequences.filter(id__in=sequence_id)
if album_ids:
album_ids = album_ids.split(",")
albums = Album.objects.filter(id__in=album_ids)
else:
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_file(request):
"""
Submit a sequence file (yaml)
"""
file = request.FILES.get("sequence_file")
yaml_content = None
response = None
status = 0
if file is None:
status = -1
message = "File does not exist"
elif file.size > 1000000:
status = -1
message = "File is too big (more than 1 000 000 bytes)"
else:
yaml_content = yaml.safe_load(file.read())
response = check_sequence_file_validity_and_save(yaml_content, request)
if response.get("succeed") == True:
seq = Sequence.objects.get(id=response.get("sequence_id"))
ephem_errors = create_sequence_pickle(seq)
response["errors"] = ephem_errors
if len(ephem_errors) > 0:
seq.delete()
response["succeed"] = False
response.pop("sequence_id")
else:
log.info(
f"User {request.user} did action submit sequence {seq} for period {seq.period} ")
return Response(response)
class AgentSurveyViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to view the status of agents.
"""
queryset = AgentSurvey.objects.all()
serializer_class = AgentSurveySerializer
permission_classes = [IsAuthenticated]
http_method_names = ["get"]
lookup_field = "name"
def get_queryset(self):
agents = AgentSurvey.objects.all()
datetime_now = datetime.utcnow()
date_minus_two_days = datetime_now - timedelta(days=2)
date_minus_two_days = date_minus_two_days.replace(tzinfo=timezone.utc)
agents = agents.exclude(updated__lt=date_minus_two_days)
return agents
class AgentCmdViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to view the commands of agents.
"""
queryset = AgentCmd.objects.all()
serializer_class = AgentCmdSerializer
permission_classes = [IsAuthenticated]
http_method_names = ["get"]
def get_queryset(self):
agent_name = self.request.query_params.get('agent_name')
number = self.request.query_params.get('number',20)
if agent_name is None:
if "agent_name" in self.kwargs:
agent_name = self.kwargs["agent_name"]
else:
agent_name = None
if agent_name is not None:
commands_sent_by_agent = AgentCmd.get_commands_sent_by_agent(agent_name)
commands_recivied_by_agent = AgentCmd.get_commands_sent_to_agent(agent_name)
agent_cmds = commands_sent_by_agent | commands_recivied_by_agent
agent_cmds = agent_cmds.exclude(full_name="get_specific_cmds")
agent_cmds = agent_cmds.exclude(full_name="get_all_cmds")
agent_cmds = agent_cmds.order_by("-s_deposit_time")
if number:
number = int(number)
agent_cmds = agent_cmds[:number]
return agent_cmds
return AgentCmd.objects.all().order_by("-id")
class EnvDataViewSet(viewsets.ModelViewSet):
"""
API endpoint to see environment data.
"""
queryset = Env_data.objects.all().order_by("monitoring_name")
serializer_class = EnvDataSerializer
permission_classes = [IsAuthenticated]
http_method_names = ["get"]
class SensorDataLastViewSet(viewsets.ModelViewSet):
"""
API endpoint to see sensor data.
"""
queryset = Sensors_data_last_value.objects.all().order_by("key")
serializer_class = SensorDataLastSerializer
permission_classes = [IsAuthenticated]
http_method_names = ["get"]