-
Notifications
You must be signed in to change notification settings - Fork 24
/
views.py
1004 lines (886 loc) · 36.7 KB
/
views.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
'''
Created on May 11, 2012
@author: nils
'''
import json
import logging
import os
import shutil
import traceback
import urlparse
from django import forms
from django.conf import settings
from django.core.urlresolvers import reverse
from django.http import (Http404, HttpResponse, HttpResponseBadRequest,
HttpResponseRedirect, HttpResponseServerError)
from django.shortcuts import get_object_or_404, render, render_to_response
from django.template import RequestContext
from django.views.generic import View
import boto3
from chunked_upload.models import ChunkedUpload
from chunked_upload.views import ChunkedUploadCompleteView, ChunkedUploadView
from guardian.shortcuts import get_perms
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from core.models import DataSet, ExtendedGroup, get_user_import_dir
from core.utils import get_full_url
from data_set_manager.isa_tab_parser import ParserException
from file_store.models import (generate_file_source_translator, get_temp_dir,
parse_s3_url)
from file_store.tasks import DownloadError, download_file, import_file
from .models import Assay, AttributeOrder, Study
from .serializers import AssaySerializer, AttributeOrderSerializer
from .single_file_column_parser import process_metadata_table
from .tasks import parse_isatab
from .utils import (customize_attribute_response, format_solr_response,
generate_solr_params_for_assay, get_owner_from_assay,
initialize_attribute_order_ranks, is_field_in_hidden_list,
search_solr, update_attribute_order_ranks)
logger = logging.getLogger(__name__)
# Data set import
PARSER_ERROR_MESSAGE = "Improperly structured ISA-Tab file: "
PARSER_UNEXPECTED_ERROR_MESSAGE = "ISA-Tab import Failure: "
class DataSetImportView(View):
"""Main view for data set importing"""
template_name = "data_set_manager/import.html"
success_view_name = 'data_set'
isa_tab_cookie_name = 'isa_tab_url'
def get(self, request, *args, **kwargs):
form = ImportISATabFileForm()
context = RequestContext(request, {'form': form})
response = render_to_response(self.template_name,
context_instance=context)
return response
class ImportISATabView(View):
"""Capture ISA archive URL from POST requests submitted from external sites
"""
def post(self, request, *args, **kwargs):
try:
isa_tab_url = request.POST['isa_tab_url']
except KeyError:
logger.error("ISA archive URL was not provided")
return HttpResponseBadRequest("Please provide an ISA archive URL")
else:
# set cookie and redirect to process_isa_tab view
response = HttpResponseRedirect(reverse('process_isa_tab'))
response.set_cookie('isa_tab_url', isa_tab_url)
return response
class TakeOwnershipOfPublicDatasetView(View):
"""Capture relative ISA archive URL from POST requests submitted from
external sites and formulates full url to
"""
def post(self, request, *args, **kwargs):
from_old_template = False
if 'isa_tab_url' in request.POST:
# TODO: I think isa_tab_url is already a full url,
# making this redundant.
full_isa_tab_url = get_full_url(request.POST['isa_tab_url'])
from_old_template = True
else:
request_body = request.body
if not request_body:
err_msg = "Neither form data nor a request body has been sent."
logger.error(err_msg)
return HttpResponseBadRequest(err_msg)
try:
body = json.loads(request_body)
except Exception as e:
err_msg = "Request body is no valid JSON"
logger.error("%s: %s" % (err_msg, e))
return HttpResponseBadRequest("%s." % err_msg)
if "data_set_uuid" in body:
data_set_uuid = body["data_set_uuid"]
else:
err_msg = "Request body doesn't contain data_set_uuid."
logger.error(err_msg)
return HttpResponseBadRequest(err_msg)
try:
full_isa_tab_url = DataSet.objects.get(
uuid=data_set_uuid
).get_isa_archive().get_datafile_url()
except (DataSet.DoesNotExist, DataSet.MultipleObjectsReturned,
Exception) as e:
err_msg = "Something went wrong"
logger.error("%s: %s" % (err_msg, e))
return HttpResponseBadRequest("%s." % err_msg)
if from_old_template:
# Redirect to process_isa_tab view
response = HttpResponseRedirect(
get_full_url(reverse('process_isa_tab'))
)
else:
# Redirect to process_isa_tab view with arg 'ajax' if request is
# not coming from old Django Template
response = HttpResponseRedirect(
get_full_url(reverse('process_isa_tab', args=['ajax']))
)
# set cookie
response.set_cookie('isa_tab_url', full_isa_tab_url)
return response
class ImportISATabFileForm(forms.Form):
"""ISA archive upload form"""
isa_tab_file = forms.FileField(label='ISA-Tab file', required=False)
isa_tab_url = forms.URLField(label='ISA-Tab URL', required=False,
widget=forms.TextInput(attrs={'size': '37'}))
def clean(self):
cleaned_data = super(ImportISATabFileForm, self).clean()
f = cleaned_data.get("isa_tab_file")
url = cleaned_data.get("isa_tab_url")
if f or url:
return cleaned_data
else:
raise forms.ValidationError(
"Please provide either a file or a URL")
class ProcessISATabView(View):
"""Process ISA archive"""
template_name = 'data_set_manager/isa-tab-import.html'
success_view_name = 'data_set'
isa_tab_cookie_name = 'isa_tab_url'
def get(self, request, *args, **kwargs):
# a workaround for automatic ISA archive import after logging in
try:
url_from_cookie = request.COOKIES[self.isa_tab_cookie_name]
except KeyError:
logger.info("ISA-Tab URL was not provided")
form = ImportISATabFileForm()
context = RequestContext(request, {'form': form})
return render_to_response(self.template_name,
context_instance=context)
form = ImportISATabFileForm({'isa_tab_url': url_from_cookie})
if form.is_valid():
url = form.cleaned_data['isa_tab_url']
else:
context = RequestContext(request, {'form': form})
response = render_to_response(self.template_name,
context_instance=context)
response.delete_cookie(self.isa_tab_cookie_name)
return response
u = urlparse.urlparse(url)
file_name = u.path.split('/')[-1]
temp_file_path = os.path.join(get_temp_dir(), file_name)
try:
# TODO: refactor download_file to take file handle instead of path
download_file(url, temp_file_path)
except DownloadError as e:
logger.error("Problem downloading ISA-Tab file. %s", e)
error = "Problem downloading ISA-Tab file from: '{}'".format(url)
context = RequestContext(request, {'form': form, 'error': error})
response = render_to_response(self.template_name,
context_instance=context)
response.delete_cookie(self.isa_tab_cookie_name)
return response
logger.debug("Temp file name: '%s'", temp_file_path)
try:
dataset_uuid = parse_isatab(
request.user.username,
False,
temp_file_path
)
except ParserException as e:
error_message = "{} {}".format(
PARSER_ERROR_MESSAGE,
e.message
)
logger.error(error_message)
return HttpResponseBadRequest(error_message)
except Exception as e:
error_message = "{} {}".format(
PARSER_UNEXPECTED_ERROR_MESSAGE,
traceback.format_exc(e)
)
logger.error(error_message)
return HttpResponseBadRequest(
PARSER_UNEXPECTED_ERROR_MESSAGE +
e.message
)
# TODO: exception handling
os.unlink(temp_file_path)
if dataset_uuid:
if 'ajax' in kwargs and kwargs['ajax']:
return HttpResponse(
json.dumps({'new_data_set_uuid': dataset_uuid}),
'application/json'
)
else:
response = HttpResponseRedirect(
reverse(self.success_view_name, args=(dataset_uuid,)))
response.delete_cookie(self.isa_tab_cookie_name)
return response
else:
error = "Problem parsing ISA-Tab file"
context = RequestContext(request, {'form': form, 'error': error})
response = render_to_response(self.template_name,
context_instance=context)
response.delete_cookie(self.isa_tab_cookie_name)
return response
def post(self, request, *args, **kwargs):
form = ImportISATabFileForm(request.POST, request.FILES)
if form.is_valid() or request.is_ajax():
try:
f = form.cleaned_data['isa_tab_file']
except KeyError:
f = None
try:
url = form.cleaned_data['isa_tab_url']
except KeyError:
url = None
if url:
response = self.import_by_url(url)
else:
try:
response = self.import_by_file(f)
except Exception as e:
logger.error(traceback.format_exc(e))
return HttpResponseBadRequest(
"{} {}".format(
PARSER_UNEXPECTED_ERROR_MESSAGE, e)
)
# get AWS Cognito identity ID
if settings.REFINERY_DEPLOYMENT_PLATFORM == 'aws':
try:
identity_id = request.POST.get('identity_id')
except (KeyError, ValueError):
error_msg = 'identity_id is missing'
error = {'error_message': error_msg}
if request.is_ajax():
return HttpResponseBadRequest(
json.dumps({'error': error_msg}),
'application/json'
)
else:
return render(request, self.template_name, error)
else:
identity_id = None
if not response['success']:
if request.is_ajax():
return HttpResponseBadRequest(
json.dumps({'error': response["message"]}),
content_type='application/json'
)
return render_to_response(
self.template_name,
context_instance=RequestContext(
request,
{
'form': form,
'error': response["message"]
}
)
)
logger.debug(
"Temp file name: '%s'", response['data']['temp_file_path']
)
try:
parse_isatab_invocation = parse_isatab(
request.user.username,
False,
response['data']['temp_file_path'],
identity_id=identity_id
)
except ParserException as e:
error_message = "{} {}".format(
PARSER_ERROR_MESSAGE,
e.message
)
logger.error(error_message)
return HttpResponseBadRequest(error_message)
except Exception as e:
error_message = "{} {}".format(
PARSER_UNEXPECTED_ERROR_MESSAGE,
traceback.format_exc(e)
)
logger.error(error_message)
return HttpResponseBadRequest(
"{} {}".format(PARSER_UNEXPECTED_ERROR_MESSAGE, e)
)
else:
dataset_uuid = parse_isatab_invocation
try:
os.unlink(response['data']['temp_file_path'])
except OSError as e:
logger.error(
"Couldn't unlink temporary file: %s %s",
response['data']['temp_file_path'], e
)
# import data files
if dataset_uuid:
try:
dataset = DataSet.objects.get(uuid=dataset_uuid)
except (DataSet.DoesNotExist, DataSet.MultipleObjectsReturned):
logger.error(
"Cannot import data files for data set UUID '%s'",
dataset_uuid
)
else:
# start importing uploaded data files
for file_store_item in dataset.get_file_store_items():
if file_store_item.source.startswith(
(settings.REFINERY_DATA_IMPORT_DIR, 's3://')
):
import_file.delay(file_store_item.uuid)
if request.is_ajax():
return HttpResponse(
json.dumps({
'success': 'Data set imported',
'data': {'new_data_set_uuid': dataset_uuid}
}),
content_type='application/json'
)
return HttpResponseRedirect(
reverse(self.success_view_name, args=[dataset_uuid])
)
else:
error = 'Problem parsing ISA-Tab file'
if request.is_ajax():
return HttpResponse(json.dumps({'error': error}),
content_type='application/json')
context = RequestContext(request, {'form': form,
'error': error})
return render_to_response(self.template_name,
context_instance=context)
else: # submitted form is not valid
context = RequestContext(request, {'form': form})
return render_to_response(self.template_name,
context_instance=context)
def import_by_file(self, file):
temp_file_path = os.path.join(get_temp_dir(), file.name)
try:
handle_uploaded_file(file, temp_file_path)
except IOError as e:
error_msg = "Error writing ISA-Tab file to disk"
logger.error(
"%s. IOError: %s, file name: %s, error: %s.",
error_msg,
e.errno,
e.filename,
e.strerror
)
return {
"success": False,
"message": error_msg
}
return {
"success": True,
"message": "File imported.",
"data": {
"temp_file_path": temp_file_path
}
}
def import_by_url(self, url):
# TODO: replace with chain
# http://docs.celeryproject.org/en/latest/userguide/tasks.html#task-synchronous-subtasks
parsed_url = urlparse.urlparse(url)
file_name = parsed_url.path.split('/')[-1]
temp_file_path = os.path.join(get_temp_dir(), file_name)
try:
# TODO: refactor download_file to take file handle instead
# of path
download_file(url, temp_file_path)
except DownloadError as e:
error_msg = "Problem downloading ISA-Tab file from: " + url
logger.error("%s. %s", error_msg, e)
return {
"success": False,
"message": error_msg
}
return {
"success": True,
"message": "File imported.",
"data": {
"temp_file_path": temp_file_path
}
}
def handle_uploaded_file(source_file, target_path):
"""Write contents of an uploaded file object to a file on disk
Raises IOError
:param source_file: uploaded file object
:type source_file: file object
:param target_path: absolute file system path to a temp file
:type target_path: str
"""
with open(target_path, 'wb+') as destination:
for chunk in source_file.chunks():
destination.write(chunk)
class ProcessMetadataTableView(View):
"""Create a new dataset from uploaded metadata table"""
template_name = 'data_set_manager/metadata-table-import.html'
success_view_name = 'data_set'
def get(self, request, *args, **kwargs):
return render(request, self.template_name)
def post(self, request, *args, **kwargs):
# Get required params
try:
metadata_file = request.FILES['file']
title = request.POST.get('title')
data_file_column = request.POST.get('data_file_column')
except (KeyError, ValueError):
error_msg = 'Required parameters are missing'
error = {'error_message': error_msg}
if request.is_ajax():
return HttpResponseBadRequest(
json.dumps({'error': error_msg}), 'application/json'
)
else:
return render(request, self.template_name, error)
try:
source_column_index = request.POST.getlist('source_column_index')
except TypeError as error_msg:
error = {'error_message': error_msg}
if request.is_ajax():
return HttpResponseBadRequest(
# TODO: make sure error_msg is JSON serializable, e.g.:
# TypeError: IndexError('list index out of range',)
# is not JSON serializable
json.dumps({'error': error_msg}), 'application/json'
)
else:
return render(request, self.template_name, error)
else:
if not source_column_index:
error_msg = 'Source columns have not been selected'
error = {'error_message': error_msg}
if request.is_ajax():
return HttpResponseBadRequest(
json.dumps({'error': error_msg}), 'application/json'
)
else:
return render(request, self.template_name, error)
# workaround for breaking change in Angular
# https://github.com/angular/angular.js/commit/7fda214c4f65a6a06b25cf5d5aff013a364e9cef
source_column_index = [
column.replace('string:', '') for column in source_column_index
]
if settings.REFINERY_DEPLOYMENT_PLATFORM == 'aws':
try:
identity_id = request.POST.get('identity_id')
except (KeyError, ValueError):
error_msg = 'identity_id is missing'
error = {'error_message': error_msg}
if request.is_ajax():
return HttpResponseBadRequest(
json.dumps({'error': error_msg}), 'application/json'
)
else:
return render(request, self.template_name, error)
else:
identity_id = None
try:
dataset_uuid = process_metadata_table(
username=request.user.username,
title=title,
metadata_file=metadata_file,
source_columns=source_column_index,
data_file_column=data_file_column,
auxiliary_file_column=request.POST.get('aux_file_column'),
base_path=request.POST.get('base_path', ''),
data_file_permanent=request.POST.get(
'data_file_permanent', False
),
species_column=request.POST.get('species_column'),
genome_build_column=request.POST.get('genome_build_column'),
annotation_column=request.POST.get('annotation_column'),
slug=request.POST.get('slug'),
is_public=request.POST.get('is_public', False),
delimiter=request.POST.get('delimiter'),
custom_delimiter_string=request.POST.get(
'custom_delimiter_string', False
),
identity_id=identity_id
)
except Exception as exc:
logger.error(exc, exc_info=True)
error = {'error_message': repr(exc)}
if request.is_ajax():
return HttpResponseServerError(
json.dumps({'error': repr(exc)}), 'application/json'
)
else:
return render(request, self.template_name, error)
if request.is_ajax():
return HttpResponse(
json.dumps({'new_data_set_uuid': dataset_uuid}),
content_type='application/json'
)
else:
return HttpResponseRedirect(
reverse(self.success_view_name, args=(dataset_uuid,))
)
class CheckDataFilesView(View):
"""Check if given files exist, return list of files that don't exist"""
def post(self, request, *args, **kwargs):
if not request.is_ajax() or not request.body:
return HttpResponseBadRequest()
try:
file_data = json.loads(request.body)
except ValueError:
return HttpResponseBadRequest()
try:
input_file_list = file_data['list']
except KeyError:
return HttpResponseBadRequest()
try:
base_path = file_data['base_path']
except KeyError:
base_path = None
try:
identity_id = file_data['identity_id']
except KeyError:
identity_id = None
bad_file_list = []
translate_file_source = generate_file_source_translator(
username=request.user.username, base_path=base_path,
identity_id=identity_id
)
uploaded_s3_key_list = []
if settings.REFINERY_DEPLOYMENT_PLATFORM == 'aws':
# get a list of all uploaded S3 objects for the user
s3 = boto3.resource('s3')
s3_bucket = s3.Bucket(settings.UPLOAD_BUCKET)
for s3_object in s3_bucket.objects.filter(Prefix=identity_id):
uploaded_s3_key_list.append(s3_object.key)
for input_file_path in input_file_list:
if not isinstance(input_file_path, unicode):
bad_file_list.append(input_file_path)
else:
input_file_path = translate_file_source(input_file_path)
if settings.REFINERY_DEPLOYMENT_PLATFORM == 'aws':
# check if S3 object key exists
bucket_name, key = parse_s3_url(input_file_path)
if key not in uploaded_s3_key_list:
bad_file_list.append(os.path.basename(key))
else: # POSIX file system
if not os.path.exists(input_file_path):
bad_file_list.append(input_file_path)
logger.debug("Checked file path: '%s'", input_file_path)
# prefix output to protect from JSON vulnerability (stripped by
# Angular)
return HttpResponse(")]}',\n" + json.dumps(bad_file_list),
content_type="application/json")
class ChunkedFileUploadView(ChunkedUploadView):
model = ChunkedUpload
field_name = "files[]"
class ChunkedFileUploadCompleteView(ChunkedUploadCompleteView):
model = ChunkedUpload
def delete(self, request):
try:
upload_id = request.GET['upload_id']
except KeyError:
logger.error("Upload ID is missing from deletion request")
return HttpResponseBadRequest(json.dumps({'error': 'KeyError'}),
'application/json')
try:
chunked = ChunkedUpload.objects.get(upload_id=upload_id)
except (ChunkedUpload.DoesNotExist,
ChunkedUpload.MultipleObjectsReturned) as e:
logger.error(
"Error retrieving file upload instance with ID '%s': '%s'",
upload_id, e)
return HttpResponseBadRequest(json.dumps({'error': e}),
'application/json')
chunked.delete()
return HttpResponse(json.dumps({'status': 'Successfully deleted.',
'upload_id': upload_id}),
'application/json')
def on_completion(self, uploaded_file, request):
"""Move file to the user's import directory"""
try:
upload_id = request.POST['upload_id']
except KeyError:
logger.error("Upload ID is missing from file upload request")
return
try:
chunked_upload = ChunkedUpload.objects.get(upload_id=upload_id)
except (ChunkedUpload.DoesNotExist,
ChunkedUpload.MultipleObjectsReturned) as exc:
logger.error(
"Error retrieving file upload instance with ID '%s': '%s'",
upload_id, exc)
return
user_import_dir = get_user_import_dir(request.user)
if not os.path.exists(user_import_dir):
try:
os.mkdir(user_import_dir)
except OSError as exc:
logger.error("Error creating user import directory '%s': %s",
user_import_dir, exc)
else:
logger.info("Created user import directory '%s'",
user_import_dir)
dst = os.path.join(user_import_dir, chunked_upload.filename)
try:
shutil.move(chunked_upload.file.path, dst)
except (shutil.Error, IOError) as exc:
logger.error(
"Error moving uploaded file to user's import directory: %s",
exc)
def get_response_data(self, chunked_upload, request):
message = "You have successfully uploaded {}".format(
chunked_upload.filename)
return {"message": message}
class Assays(APIView):
"""
Return assay object
---
#YAML
GET:
serializer: AssaySerializer
omit_serializer: false
parameters:
- name: uuid
description: Assay uuid
paramType: query
type: string
required: false
- name: study
description: Study uuid
paramType: query
type: string
required: false
...
"""
def get_object(self, uuid):
try:
return Assay.objects.get(uuid=uuid)
except (Assay.DoesNotExist, Assay.MultipleObjectsReturned):
raise Http404
def get_query_set(self, study_uuid):
try:
study_obj = Study.objects.get(
uuid=study_uuid)
return Assay.objects.filter(study=study_obj)
except (Study.DoesNotExist,
Study.MultipleObjectsReturned):
raise Http404
def get(self, request, format=None):
if request.query_params.get('uuid'):
assay = self.get_object(request.query_params.get('uuid'))
serializer = AssaySerializer(assay)
return Response(serializer.data)
elif request.query_params.get('study'):
assays = self.get_query_set(request.query_params.get('study'))
serializer = AssaySerializer(assays, many=True)
return Response(serializer.data)
else:
raise Http404
class AssaysFiles(APIView):
"""
Return solr response. Query requires assay_uuid.
---
#YAML
GET:
parameters_strategy:
form: replace
query: merge
parameters:
- name: uuid
description: Assay uuid
type: string
required: true
paramType: path
- name: is_annotation
description: Metadata
type: boolean
paramType: query
- name: filter_attribute
description: Filters for attributes fields
type: string
paramType: query
- name: include_facet_count
description: Enables facet counts in query response
type: boolean
paramType: query
- name: offset
description: Paginate offset response
type: integer
paramType: query
- name: limit
description: Maximum number of documents returned
type: integer
paramType: query
- name: attributes
description: Set of attributes to return separated by a comma
type: string
paramType: query
- name: facets
description: Specify facet fields separated by a comma
type: string
paramType: query
- name: pivots
description: List of fields to pivot separated by a comma
type: string
paramType: query
- name: sort
description: Order node response with field name asc/desc
type: string
paramType: query
- name: data_set_uuid
description: data set uuid required to check for perms
type: string
paramType: query
...
"""
def get(self, request, uuid, format=None):
params = request.query_params
data_set_uuid = params.get('data_set_uuid', None)
# requires data_set_uuid to check perms
if data_set_uuid:
data_set = get_object_or_404(DataSet, uuid=data_set_uuid)
public_group = ExtendedGroup.objects.public_group()
if request.user.has_perm('core.read_dataset', data_set) or \
'read_dataset' in get_perms(public_group, data_set):
solr_params = generate_solr_params_for_assay(params, uuid)
elif request.user.has_perm('core.read_meta_dataset', data_set) or \
'read_meta_dataset' in get_perms(public_group, data_set):
solr_params = generate_solr_params_for_assay(
params,
uuid,
['REFINERY_DOWNLOAD_URL', 'REFINERY_NAME']
)
else:
message = 'User does not have read permissions.'
return Response(message, status=status.HTTP_401_UNAUTHORIZED)
solr_response = search_solr(solr_params, 'data_set_manager')
solr_response_json = format_solr_response(solr_response)
return Response(solr_response_json)
else:
return Response(
'Requires data set uuid.',
status=status.HTTP_400_BAD_REQUEST
)
class AssaysAttributes(APIView):
"""
AttributeOrder Resource.
Returns/Updates AttributeOrder model queries. Requires assay_uuid.
The model is dynamically created, so users will not create new
attribute_orders.
Updates attribute_model
---
#YAML
GET:
serializer: AttributeOrderSerializer
omit_serializer: false
parameters:
- name: uuid
description: Assay uuid
type: string
paramType: path
required: true
PUT:
parameters_strategy:
form: replace
query: merge
parameters:
- name: uuid
description: Assay uuid used as an identifier
type: string
paramType: path
required: true
- name: solr_field
description: Title of solr field used as an identifier
type: string
paramType: form
required: false
- name: rank
description: Position of the attribute in facet list and table
type: int
paramType: form
required: false
- name: is_exposed
description: Show to non-owner users
type: boolean
paramType: form
- name: is_facet
description: Attribute used as facet
type: boolean
paramType: form
- name: is_active
description: Shown in table by default
type: boolean
paramType: form
- name: id
description: Attribute ID used as an identifier
type: integer
paramType: form
...
"""
def get_objects(self, uuid):
attributes = AttributeOrder.objects.filter(assay__uuid=uuid)
if len(attributes):
return attributes
else:
raise Http404
def get(self, request, uuid, format=None):
attribute_order = self.get_objects(uuid)
serializer = AttributeOrderSerializer(attribute_order, many=True)
owner = get_owner_from_assay(uuid)
request_user = request.user
# add a display name to the attribute object
if owner == request_user:
attributes = serializer.data
# for non-owners, hide non-exposed attributes
else:
attributes = []
for attribute in serializer.data:
if attribute.get('is_exposed'):
attributes.append(attribute)
# Reverse check, so can remove objects from the end
for ind in range(len(attributes) - 1, -1, -1):
if is_field_in_hidden_list(attributes[ind].get('solr_field')):
del attributes[ind]
else:
attributes[ind]['display_name'] = customize_attribute_response(
[attributes[ind].get('solr_field')])[0].get(
'display_name')
# for non-owners need to reorder the ranks
if owner != request_user:
for ind in range(0, len(attributes)):
attributes[ind]['rank'] = ind + 1
return Response(attributes)
def put(self, request, uuid, format=None):
owner = get_owner_from_assay(uuid)
request_user = request.user
if owner == request_user:
solr_field = request.data.get('solr_field', None)
id = request.data.get('id', None)
new_rank = request.data.get('rank', None)
if id:
attribute_order = AttributeOrder.objects.get(
assay__uuid=uuid, id=id)
elif solr_field:
attribute_order = AttributeOrder.objects.get(
assay__uuid=uuid, solr_field=solr_field)
else:
return Response(
'Requires attribute id or solr_field name.',
status=status.HTTP_400_BAD_REQUEST)
# if old attribute order rank == 0, then all ranks need to be set
if attribute_order.rank == 0:
initialize_attribute_order_ranks(attribute_order, new_rank)
# updates all ranks in assay's attribute order
elif new_rank and new_rank != attribute_order.rank:
try:
update_attribute_order_ranks(attribute_order, new_rank)
except Exception as e:
return e
# updates the requested attribute rank with new rank
serializer = AttributeOrderSerializer(attribute_order,
data=request.data,
partial=True)
if serializer.is_valid():
serializer.save()
attributes = serializer.data
attributes['display_name'] = customize_attribute_response(
[attributes.get('solr_field')])[0].get(
'display_name')
return Response(
attributes,
status=status.HTTP_202_ACCEPTED
)
return Response(
serializer.errors,
status=status.HTTP_400_BAD_REQUEST