-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
1353 lines (1056 loc) · 49.7 KB
/
api.py
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from datetime import datetime
import json
import logging
import os
import shutil
import uuid
import zipfile
import magic
from urllib.parse import urlparse
import urllib
from django_q.tasks import async_task
from dbmi_client import fileservice
from django.conf import settings
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect
from django.template.loader import render_to_string
from django.core.files.storage import default_storage
from hypatio.auth0authenticate import user_auth_and_jwt
from contact.views import email_send
from hypatio.sciauthz_services import SciAuthZ
from hypatio.scireg_services import get_names
from hypatio.file_services import host_file
from manage.forms import EditHostedFileForm
from manage.forms import HostSubmissionForm
from manage.utils import zip_submission_file
from projects.templatetags import projects_extras
from manage.models import ChallengeTaskSubmissionExport
from projects.models import AgreementForm
from projects.models import ChallengeTaskSubmission
from projects.models import DataProject
from projects.models import HostedFile
from projects.models import Participant
from projects.models import SignedAgreementForm
from projects.models import Team
from projects.models import TeamComment
from projects.serializers import HostedFileSerializer, HostedFileDownloadSerializer
from projects.models import AGREEMENT_FORM_TYPE_MODEL, AGREEMENT_FORM_TYPE_FILE
from projects.models import InstitutionalOfficial
# Get an instance of a logger
logger = logging.getLogger(__name__)
@user_auth_and_jwt
def set_dataproject_registration_status(request):
"""
An HTTP POST endpoint for opening or closing registration to a DataProject.
"""
user = request.user
user_jwt = request.COOKIES.get("DBMI_JWT", None)
project_key = request.POST.get("project_key")
project = get_object_or_404(DataProject, project_key=project_key)
sciauthz = SciAuthZ(user_jwt, user.email)
is_manager = sciauthz.user_has_manage_permission(project_key)
if not is_manager:
logger.debug(
'[HYPATIO][DEBUG][set_dataproject_registration_status] User {email} does not have MANAGE permissions for item {project_key}.'.format(
email=user.email,
project_key=project_key
)
)
return HttpResponse("Error: permissions.", status=403)
registration_status = request.POST.get("registration_status")
if registration_status == "open":
project.registration_open = True
elif registration_status == "close":
project.registration_open = False
else:
return HttpResponse("Error: invalid status.", status=400)
project.save()
response_html = render_to_string(
'manage/registration-status.html',
context={
'project': project,
}
)
return HttpResponse(response_html)
@user_auth_and_jwt
def set_dataproject_visible_status(request):
"""
An HTTP POST endpoint for making a DataProject publicly visible or not.
"""
user = request.user
user_jwt = request.COOKIES.get("DBMI_JWT", None)
project_key = request.POST.get("project_key")
project = get_object_or_404(DataProject, project_key=project_key)
sciauthz = SciAuthZ(user_jwt, user.email)
is_manager = sciauthz.user_has_manage_permission(project_key)
if not is_manager:
logger.debug(
'[HYPATIO][DEBUG][set_dataproject_visible_status] User {email} does not have MANAGE permissions for item {project_key}.'.format(
email=user.email,
project_key=project_key
)
)
return HttpResponse("Error: permissions.", status=403)
visibile_status = request.POST.get("visible_status")
if visibile_status == "visible":
project.visible = True
elif visibile_status == "invisible":
project.visible = False
else:
return HttpResponse("Error: Invalid status.", status=400)
project.save()
response_html = render_to_string(
'manage/visible-status.html',
context={
'project': project,
}
)
return HttpResponse(response_html)
@user_auth_and_jwt
def set_dataproject_details(request):
"""
An HTTP POST endpoint for setting some details about a DataProject.
"""
user = request.user
user_jwt = request.COOKIES.get("DBMI_JWT", None)
project_key = request.POST.get("project_key")
project = get_object_or_404(DataProject, project_key=project_key)
sciauthz = SciAuthZ(user_jwt, user.email)
is_manager = sciauthz.user_has_manage_permission(project_key)
if not is_manager:
logger.debug(
'[HYPATIO][DEBUG][set_dataproject_visible_status] User {email} does not have MANAGE permissions for item {project_key}.'.format(
email=user.email,
project_key=project_key
)
)
return HttpResponse("Error: permissions.", status=403)
name = request.POST.get("name", "")
short_description = request.POST.get("short-description", "")
description = request.POST.get("description", "")
if name == "":
return HttpResponse("Error: project name required.", status=400)
if short_description == "":
return HttpResponse("Error: project short description required.", status=400)
if description == "":
return HttpResponse("Error: project description required.", status=400)
project.name = name
project.short_description = short_description
project.description = description
try:
project.save()
except Exception as ex:
return HttpResponse("Error: project details failed to save.", status=400)
response_html = render_to_string(
'manage/project-details.html',
context={
'project': project,
'message': "Changes saved!"
}
)
return HttpResponse(response_html)
@user_auth_and_jwt
def get_static_agreement_form_html(request):
"""
An HTTP GET endpoint for Intercooler.js requests to get the HTML contents of an
agreement form file and return them as HTML.
"""
user = request.user
user_jwt = request.COOKIES.get("DBMI_JWT", None)
project_key = request.GET.get("project-key")
project = get_object_or_404(DataProject, project_key=project_key)
sciauthz = SciAuthZ(user_jwt, user.email)
is_manager = sciauthz.user_has_manage_permission(project_key)
if not is_manager:
logger.debug(
'[HYPATIO][DEBUG][get_static_agreement_form_html] User {email} does not have MANAGE permissions for item {project_key}.'.format(
email=user.email,
project_key=project_key
)
)
return HttpResponse("Error: permissions.", status=403)
agreement_form_id = request.GET.get('form-id', "")
try:
agreement_form = project.agreement_forms.get(id=agreement_form_id)
except ObjectDoesNotExist:
return HttpResponse("Error: form not found.", status=404)
if agreement_form.type == AGREEMENT_FORM_TYPE_MODEL:
if agreement_form.content is None or agreement_form.content == "":
return HttpResponse("Error: form content is missing.", status=400)
form_contents = agreement_form.content
elif agreement_form.type == AGREEMENT_FORM_TYPE_FILE:
if agreement_form.content is None or agreement_form.content == "":
return HttpResponse("Error: form content is missing.", status=400)
form_contents = agreement_form.upload
else:
if agreement_form.form_file_path is None or agreement_form.form_file_path == "":
return HttpResponse("Error: form file path is missing.", status=400)
form_contents = projects_extras.get_html_form_file_contents(agreement_form.form_file_path)
return HttpResponse(form_contents)
@user_auth_and_jwt
def get_hosted_file_edit_form(request):
"""
An HTTP GET endpoint for requests to get an HTML form for editing an
existing HostedFile.
"""
user = request.user
user_jwt = request.COOKIES.get("DBMI_JWT", None)
project_key = request.GET.get("project-key")
project = get_object_or_404(DataProject, project_key=project_key)
sciauthz = SciAuthZ(user_jwt, user.email)
is_manager = sciauthz.user_has_manage_permission(project_key)
if not is_manager:
logger.debug(
'[HYPATIO][DEBUG][get_static_agreement_form_html] User {email} does not have MANAGE permissions for item {project_key}.'.format(
email=user.email,
project_key=project_key
)
)
return HttpResponse("Error: permissions.", status=403)
hosted_file_uuid = request.GET.get("hosted-file-uuid")
try:
hosted_file = HostedFile.objects.get(project=project, uuid=hosted_file_uuid)
except ObjectDoesNotExist:
return HttpResponse("Error: file not found.", status=404)
edit_hosted_file_form = EditHostedFileForm(instance=hosted_file)
response_html = render_to_string(
'manage/edit-hosted-file-form.html',
context={
'form': edit_hosted_file_form,
'file': hosted_file
},
request=request
)
return HttpResponse(response_html)
@user_auth_and_jwt
def get_hosted_file_logs(request):
"""
An HTTP GET endpoint for requests to get logs of an existing HostedFile.
"""
user = request.user
user_jwt = request.COOKIES.get("DBMI_JWT", None)
project_key = request.GET.get("project-key")
project = get_object_or_404(DataProject, project_key=project_key)
sciauthz = SciAuthZ(user_jwt, user.email)
is_manager = sciauthz.user_has_manage_permission(project_key)
if not is_manager:
logger.debug(
'[HYPATIO][DEBUG][get_hosted_file_logs] User {email} does not have MANAGE permissions for item {project_key}.'.format(
email=user.email,
project_key=project_key
)
)
return HttpResponse("Error: permissions.", status=403)
hosted_file_uuid = request.GET.get("hosted-file-uuid")
try:
hosted_file = HostedFile.objects.get(project=project, uuid=hosted_file_uuid)
except ObjectDoesNotExist:
logger.debug(
'[HYPATIO][DEBUG][get_hosted_file_logs] User {email} does not have MANAGE permissions for item {project_key}.'.format(
email=user.email,
project_key=project_key
)
)
return HttpResponse("Error: file not found.", status=404)
except Exception as e:
logger.exception(
'[HYPATIO][EXCEPTION][get_hosted_file_logs] Could not perform fetch for '
'file download logs: {e}.'.format(e=e), exc_info=True, extra={
'user': user.email, 'project': project_key, 'hosted_file': hosted_file_uuid
}
)
return HttpResponse("Error: fetch failed with error", status=500)
response_html = render_to_string(
'manage/hosted-file-logs.html',
context={
'downloads': [HostedFileDownloadSerializer(downloads).data for downloads in
hosted_file.hostedfiledownload_set.all().order_by('download_date')],
'file': HostedFileSerializer(hosted_file).data,
},
request=request
)
return HttpResponse(response_html)
@user_auth_and_jwt
def process_hosted_file_edit_form_submission(request):
"""
An HTTP POST endpoint for processing a submitted form for editing a hosted file.
"""
user = request.user
user_jwt = request.COOKIES.get("DBMI_JWT", None)
file_uuid = request.POST.get("file-uuid")
project_id = request.POST.get("project")
project = get_object_or_404(DataProject, id=project_id)
sciauthz = SciAuthZ(user_jwt, user.email)
is_manager = sciauthz.user_has_manage_permission(project.project_key)
if not is_manager:
logger.debug(
'[HYPATIO][DEBUG][get_static_agreement_form_html] User {email} does not have MANAGE permissions for item {project_key}.'.format(
email=user.email,
project_key=project.project_key
)
)
return HttpResponse("Error: permissions.", status=403)
hosted_file = HostedFile.objects.get(project=project, uuid=file_uuid)
edit_hosted_file_form = EditHostedFileForm(request.POST, instance=hosted_file)
# TODO show error messages
if not edit_hosted_file_form.is_valid():
return HttpResponse(edit_hosted_file_form.errors.as_json(), status=400)
try:
edit_hosted_file_form.save()
except Exception:
# TODO do something on failure
return HttpResponse("Error: failed to save object", status=500)
return HttpResponse("File updated.", status=200)
@user_auth_and_jwt
def download_signed_form(request):
"""
An HTTP GET endpoint that returns a text file to the user containing the signed form's content.
"""
form_id = request.GET.get("form_id")
logger.debug('[HYPATIO][download_signed_form] ' + request.user.email + ' is trying to download signed form ' + form_id + '.')
signed_form = get_object_or_404(SignedAgreementForm, id=form_id)
user = request.user
user_jwt = request.COOKIES.get("DBMI_JWT", None)
project = signed_form.project
sciauthz = SciAuthZ(user_jwt, user.email)
is_manager = sciauthz.user_has_manage_permission(project.project_key)
if not is_manager:
logger.debug(
'[HYPATIO][DEBUG][download_signed_form] User {email} does not have MANAGE permissions for item {project_key}.'.format(
email=user.email,
project_key=project.project_key
)
)
return HttpResponse("Error: permissions.", status=403)
# Set details of the file download
affected_user = signed_form.user
date_as_string = datetime.strftime(signed_form.date_signed, "%Y%m%d-%H%M")
# Check type
if signed_form.agreement_form.type == AGREEMENT_FORM_TYPE_FILE:
# Download the file
file = signed_form.upload.open(mode="rb").read()
# Determine MIME
mime = magic.from_buffer(file, mime=True)
# Get the signed URL for the file in S3
filename = signed_form.upload.name
response = HttpResponse(file, content_type=mime)
response['Content-Disposition'] = f'attachment; filename={filename}'
else:
filename = affected_user.email + '-' + signed_form.agreement_form.short_name + '-' + date_as_string + '.txt'
response = HttpResponse(signed_form.agreement_text, content_type='text/plain')
response['Content-Disposition'] = f'attachment; filename={filename}'
return response
@user_auth_and_jwt
def get_signed_form_status(request):
"""
An HTTP POST endpoint for fetching a signed form's status.
"""
form_id = request.POST.get("form_id")
logger.debug(f'[get_signed_form_status] {request.user.email} -> {form_id}')
# Get the form
signed_form = get_object_or_404(SignedAgreementForm, id=form_id)
# Confirm permissions on project form was signed for
user = request.user
user_jwt = request.COOKIES.get("DBMI_JWT", None)
project = signed_form.project
sciauthz = SciAuthZ(user_jwt, user.email)
is_manager = sciauthz.user_has_manage_permission(project.project_key)
if not is_manager:
logger.debug(
'[HYPATIO][DEBUG][change_signed_form_status] User {email} does not have MANAGE permissions for item {project_key}.'.format(
email=user.email,
project_key=project.project_key
)
)
return HttpResponse("Error: permissions.", status=403)
return HttpResponse(signed_form.status, status=200)
@user_auth_and_jwt
def change_signed_form_status(request):
"""
An HTTP POST endpoint for changing a signed form's status and notifying the user.
"""
status = request.POST.get("status")
form_id = request.POST.get("form_id")
administrator_message = request.POST.get("administrator_message")
logger.debug('[HYPATIO][change_signed_form_status] ' + request.user.email + ' try to change status for signed form ' + form_id + ' to ' + status + '.')
signed_form = get_object_or_404(SignedAgreementForm, id=form_id)
affected_user = signed_form.user
user = request.user
user_jwt = request.COOKIES.get("DBMI_JWT", None)
project = signed_form.project
sciauthz = SciAuthZ(user_jwt, user.email)
is_manager = sciauthz.user_has_manage_permission(project.project_key)
if not is_manager:
logger.debug(
'[HYPATIO][DEBUG][change_signed_form_status] User {email} does not have MANAGE permissions for item {project_key}.'.format(
email=user.email,
project_key=project.project_key
)
)
return HttpResponse("Error: permissions.", status=403)
# First change the team's status
if status == "approved":
signed_form.status = 'A'
signed_form.save()
elif status == "rejected":
signed_form.status = 'R'
signed_form.save()
logger.debug('[HYPATIO][change_signed_form_status] Emailing a rejection notification to the affected participant')
# Send an email notification to the affected person
context = {'signed_form': signed_form,
'administrator_message': administrator_message,
'site_url': settings.SITE_URL}
email_success = email_send(subject='DBMI Portal - Signed Form Rejected',
recipients=[affected_user.email],
email_template='email_signed_form_rejection_notification',
extra=context)
try:
# Fetch participant so we can revoke permissions for them and team, if applicable
participant = Participant.objects.get(user=affected_user, project=signed_form.project)
team = participant.team
# If just a participant, remove their view permissions
if not team:
sciauthz = SciAuthZ(request.COOKIES.get("DBMI_JWT", None), request.user.email)
sciauthz.remove_view_permission(signed_form.project.project_key, affected_user.email)
# Remove their VIEW permission
participant.permission = None
participant.save()
# If the team is in an Active status, move the team status down to Ready and remove everyone's VIEW permissions
elif team is not None and team.status == "Active":
team.status = "Ready"
team.save()
for member in team.participant_set.all():
sciauthz = SciAuthZ(request.COOKIES.get("DBMI_JWT", None), request.user.email)
sciauthz.remove_view_permission(signed_form.project.project_key, member.user.email)
# Remove their VIEW permission
member.permission = None
member.save()
logger.debug('[HYPATIO][change_signed_form_status] Emailing the whole team that their status has been moved to Ready because someone has a pending form')
# Send an email notification to the team
context = {'status': "ready",
'reason': 'Your team has been temporarily disabled because of an issue with a team members\' forms. Challenge administrators will resolve this shortly.',
'project': signed_form.project,
'site_url': settings.SITE_URL}
# Email list
emails = [member.user.email for member in team.participant_set.all()]
email_success = email_send(subject='DBMI Portal - Team Status Changed',
recipients=emails,
email_template='email_new_team_status_notification',
extra=context)
except ObjectDoesNotExist:
logger.error(
f'[HYPATIO][change_signed_form_status] Could not find Participant for '
f'{affected_user.email} / {signed_form.project.project_key}'
)
else:
logger.debug('[HYPATIO][change_signed_form_status] Given status "' + status + '" not one of allowed statuses.')
return HttpResponse(500)
return HttpResponse(200)
@user_auth_and_jwt
def save_team_comment(request):
"""
An HTTP POST endpoint for saving a comment made about a team by a challenge administrator.
"""
project_key = request.POST.get("project")
team_leader = request.POST.get("team")
comment = request.POST.get("comment")
logger.debug('[HYPATIO][save_team_comment] ' + request.user.email + ' entered a comment about team ' + team_leader + '.')
project = DataProject.objects.get(project_key=project_key)
user = request.user
user_jwt = request.COOKIES.get("DBMI_JWT", None)
sciauthz = SciAuthZ(user_jwt, user.email)
is_manager = sciauthz.user_has_manage_permission(project.project_key)
if not is_manager:
logger.debug(
'[HYPATIO][DEBUG][save_team_comment] User {email} does not have MANAGE permissions for item {project_key}.'.format(
email=user.email,
project_key=project.project_key
)
)
return HttpResponse("Error: permissions.", status=403)
team = Team.objects.get(team_leader__email=team_leader, data_project=project)
new_comment = TeamComment(user=request.user,
team=team,
text=comment)
new_comment.save()
return HttpResponse(200)
@user_auth_and_jwt
def set_team_status(request):
"""
An HTTP POST endpoint to set a team's status, assign the correct permissions, and notify team members.
"""
project_key = request.POST.get("project")
team_leader = request.POST.get("team")
status = request.POST.get("status")
project = DataProject.objects.get(project_key=project_key)
user = request.user
user_jwt = request.COOKIES.get("DBMI_JWT", None)
sciauthz = SciAuthZ(user_jwt, user.email)
is_manager = sciauthz.user_has_manage_permission(project.project_key)
logger.debug(
'[HYPATIO][DEBUG][set_team_status] User {email} is attempting to set team {team} to status of {status} for project {project_key}.'.format(
email=request.user.email,
team=team_leader,
status=status,
project_key=project.project_key
)
)
if not is_manager:
logger.debug(
'[HYPATIO][DEBUG][set_team_status] User {email} does not have MANAGE permissions for item {project_key}.'.format(
email=user.email,
project_key=project.project_key
)
)
return HttpResponse("Error: permissions.", status=403)
team = Team.objects.get(team_leader__email=team_leader, data_project=project)
# First change the team's status
if status == "pending":
team.status = "Pending"
team.save()
elif status == "ready":
team.status = "Ready"
team.save()
elif status == "active":
team.status = "Active"
team.save()
elif status == "deactivated":
team.status = "Deactivated"
team.save()
else:
logger.debug('[HYPATIO][set_team_status] Given status "' + status + '" not one of allowed statuses.')
return HttpResponse(500)
# If setting to Active, grant each team member access permissions.
if status == "active":
for member in team.participant_set.all():
sciauthz = SciAuthZ(request.COOKIES.get("DBMI_JWT", None), request.user.email)
sciauthz.create_view_permission(project_key, member.user.email)
# Add permission to Participant
member.permission = 'VIEW'
member.save()
# If setting to Deactivated, revoke each team member's permissions.
elif status == "deactivated":
for member in team.participant_set.all():
sciauthz = SciAuthZ(request.COOKIES.get("DBMI_JWT", None), request.user.email)
sciauthz.remove_view_permission(project_key, member.user.email)
# Remove permission from Participant
member.permission = None
member.save()
# Send an email notification to the team
context = {'status': status,
'project': project,
'site_url': settings.SITE_URL}
# Email list
emails = [member.user.email for member in team.participant_set.all()]
email_success = email_send(subject='DBMI Portal - Team Status Changed',
recipients=emails,
email_template='email_new_team_status_notification',
extra=context)
return HttpResponse(200)
@user_auth_and_jwt
def delete_team(request):
"""
Delete a team and notify members.
"""
project_key = request.POST.get("project")
team_leader = request.POST.get("team")
administrator_message = request.POST.get("administrator_message")
logger.debug(
'[HYPATIO][DEBUG][delete_team] User {email} is deleting team {team} for project {project_key}.'.format(
email=request.user.email,
team=team_leader,
project_key=project_key
)
)
project = DataProject.objects.get(project_key=project_key)
user = request.user
user_jwt = request.COOKIES.get("DBMI_JWT", None)
sciauthz = SciAuthZ(user_jwt, user.email)
is_manager = sciauthz.user_has_manage_permission(project.project_key)
if not is_manager:
logger.debug(
'[HYPATIO][DEBUG][delete_team] User {email} does not have MANAGE permissions for item {project_key}.'.format(
email=user.email,
project_key=project.project_key
)
)
return HttpResponse("Error: permissions.", status=403)
team = Team.objects.get(team_leader__email=team_leader, data_project=project)
logger.debug('[HYPATIO][delete_team] Removing all VIEW permissions for team members.')
# First revoke all VIEW permissions
for member in team.participant_set.all():
sciauthz = SciAuthZ(request.COOKIES.get("DBMI_JWT", None), request.user.email)
sciauthz.remove_view_permission(project_key, member.user.email)
# Remove permission from Participant
member.permission = None
member.save()
logger.debug('[HYPATIO][delete_team] Sending a notification to team members.')
# Then send a notification to the team members
context = {'administrator_message': administrator_message,
'project': project,
'site_url': settings.SITE_URL}
emails = [member.user.email for member in team.participant_set.all()]
email_success = email_send(subject='DBMI Portal - Team Deleted',
recipients=emails,
email_template='email_team_deleted_notification',
extra=context)
logger.debug('[HYPATIO][delete_team] Deleting the team from the database.')
# Then delete the team
team.delete()
logger.debug('[HYPATIO][delete_team] Team ' + team_leader + ' for project ' + project_key + ' successfully deleted.')
return HttpResponse(200)
@user_auth_and_jwt
def download_team_submissions(request, project_key, team_leader_email):
"""
An HTTP GET endpoint that handles downloads of participant submission files. Checks
that the requesting user has proper permissions to access this file. Downloads all
of the team's submissions into one zip file, with each submission in it's own zip
file containing a json file with metadata about the upload.
"""
logger.debug('download_team_submissions: {}'.format(request.method))
if request.method == "GET":
# Check permissions in SciAuthZ.
user_jwt = request.COOKIES.get("DBMI_JWT", None)
sciauthz = SciAuthZ(user_jwt, request.user.email)
is_manager = sciauthz.user_has_manage_permission(project_key)
if not is_manager:
logger.debug("[download_team_submissions] - No Access for user " + request.user.email)
return HttpResponse("You do not have access to download this file.", status=403)
project = get_object_or_404(DataProject, project_key=project_key)
team = get_object_or_404(Team, data_project=project, team_leader__email=team_leader_email)
# A list of file paths to each submission's zip file.
zipped_submissions_paths = []
# Get all submissions made by this team for this project.
submissions = ChallengeTaskSubmission.objects.filter(
challenge_task__in=project.challengetask_set.all(),
participant__in=team.participant_set.all(),
deleted=False
)
# For each submission, create a zip file and add the path to the list of zip files.
for submission in submissions:
zip_file_path = zip_submission_file(submission, request.user.email, request)
zipped_submissions_paths.append(zip_file_path)
# Create a directory to store the final encompassing zip file.
final_zip_file_directory = "/tmp/" + str(uuid.uuid4())
if not os.path.exists(final_zip_file_directory):
os.makedirs(final_zip_file_directory)
# Combine all the zipped tasks into one file zip file.
final_zip_file_name = project_key + "__team-submissions__" + team_leader_email + ".zip"
final_zip_file_path = os.path.join(final_zip_file_directory, final_zip_file_name)
with zipfile.ZipFile(final_zip_file_path, mode="w") as zf:
for zip_file in zipped_submissions_paths:
zf.write(zip_file, arcname=os.path.basename(zip_file))
# Prepare the zip file to be served.
final_zip_file = open(final_zip_file_path, 'rb')
response = HttpResponse(final_zip_file, content_type='application/force-download')
response['Content-Disposition'] = 'attachment; filename="%s"' % final_zip_file_name
# Delete all the directories holding the zip files.
for path in zipped_submissions_paths:
shutil.rmtree(os.path.dirname(os.path.realpath((path))))
# Delete the final zip file.
shutil.rmtree(final_zip_file_directory)
return response
@user_auth_and_jwt
def download_submission(request, fileservice_uuid):
"""
An HTTP GET endpoint that allows a user to download a ChallengeTaskSubmission's
file from AWS/fileservice.
"""
if request.method == "GET":
submission = get_object_or_404(ChallengeTaskSubmission, uuid=fileservice_uuid)
project = submission.challenge_task.data_project
# Check permissions in SciAuthZ.
user_jwt = request.COOKIES.get("DBMI_JWT", None)
sciauthz = SciAuthZ(user_jwt, request.user.email)
is_manager = sciauthz.user_has_manage_permission(project.project_key)
if not is_manager:
logger.debug("[download_submission] - No Access for user " + request.user.email)
return HttpResponse("You do not have access to download this file.", status=403)
# Download the submission file from fileservice and zip it up with the info json.
zip_file_path = zip_submission_file(submission, request.user.email, request)
zip_file_name = os.path.basename(zip_file_path)
# Prepare the zip file to be served.
final_zip_file = open(zip_file_path, 'rb')
response = HttpResponse(final_zip_file, content_type='application/force-download')
response['Content-Disposition'] = 'attachment; filename="%s"' % zip_file_name
# Delete the directory containing the zip file.
shutil.rmtree(os.path.dirname(os.path.realpath((zip_file_path))))
return response
@user_auth_and_jwt
def export_submissions(request, project_key):
"""
An HTTP GET endpoint that allows a user to download a ChallengeTaskSubmission's
file from AWS/fileservice.
"""
if request.method == "GET":
# Check permissions in SciAuthZ.
user_jwt = request.COOKIES.get("DBMI_JWT", None)
sciauthz = SciAuthZ(user_jwt, request.user.email)
is_manager = sciauthz.user_has_manage_permission(project_key)
if not is_manager:
logger.debug("[download_team_submissions] - No Access for user " + request.user.email)
return HttpResponse("You do not have access to download this file.", status=403)
project = get_object_or_404(DataProject, project_key=project_key)
# Run the task
async_task('manage.tasks.export_task_submissions', project.id, request.user.email)
# Prepare the zip file to be served.
return HttpResponse(status=201)
@user_auth_and_jwt
def download_submissions_export(request, project_key, fileservice_uuid):
"""
An HTTP GET endpoint that allows a user to download a ChallengeTask's
submissions export file from AWS/fileservice.
"""
if request.method == "GET":
# Check permissions in SciAuthZ.
user_jwt = request.COOKIES.get("DBMI_JWT", None)
sciauthz = SciAuthZ(user_jwt, request.user.email)
is_manager = sciauthz.user_has_manage_permission(project_key)
if not is_manager:
logger.debug("[download_submissions_export] - No Access for user " + request.user.email)
return HttpResponse("You do not have access to download this file.", status=403)
# Get filename
filename = f"{project_key}_export_{fileservice_uuid}.zip"
# Get the url
url = fileservice.get_archivefile_download_url(fileservice_uuid)
# Prepare the parts
protocol = urlparse(url).scheme
path = urllib.parse.quote_plus(url.replace(protocol + '://', ''))
# Let NGINX handle it
response = HttpResponse()
response['X-Accel-Redirect'] = '/proxy/' + protocol + '/' + path
response['Content-Disposition'] = 'attachment; filename="{}"'.format(filename)
logger.debug(f'Sending user to S3 proxy: {response["X-Accel-Redirect"]}')
return response
@user_auth_and_jwt
def host_submission(request, fileservice_uuid):
"""
An HTTP POST endpoint that allows a user to host a ChallengeTaskSubmission's
file from AWS/fileservice as a HostedFile.
"""
if request.method == "GET":
logger.debug('host-submission: GET')
user = request.user
user_jwt = request.COOKIES.get("DBMI_JWT", None)
submission = get_object_or_404(ChallengeTaskSubmission, uuid=fileservice_uuid)
project = submission.challenge_task.data_project
sciauthz = SciAuthZ(user_jwt, user.email)
is_manager = sciauthz.user_has_manage_permission(project.project_key)
if not is_manager:
logger.debug(
'[HYPATIO][DEBUG][get_static_agreement_form_html] User {email} does not have MANAGE permissions for item {project_key}.'.format(
email=user.email,
project_key=project.project_key
)
)
return HttpResponse("Error: permissions.", status=403)
# Parse submission info
try:
if submission.submission_info:
file_name = json.loads(submission.submission_info).get('filename')
else:
file_name = None
except Exception as e:
logger.exception('Submission info error: {e}', exc_info=True, extra={
'file_uuid': fileservice_uuid, 'submission': submission, 'submission_info': submission.submission_info
})
# Initialize some fields
initial = {
'project': project.id,
'file_name': file_name,
'file_location': project.project_key.replace('-', '/')
}
host_hosted_file_form = HostSubmissionForm(initial=initial)
response_html = render_to_string(
'manage/host-submission-form.html',
context={
'form': host_hosted_file_form,
'submission': submission,
'fileservice_uuid': fileservice_uuid,
},
request=request
)
return HttpResponse(response_html)
elif request.method == "POST":
logger.debug('host-submission: POST')
submission = get_object_or_404(ChallengeTaskSubmission, uuid=fileservice_uuid)
project = submission.challenge_task.data_project
user = request.user