-
Notifications
You must be signed in to change notification settings - Fork 32
/
views.py
595 lines (464 loc) · 23.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
import logging
from celery_once import AlreadyQueued
from django.core.exceptions import ValidationError
from django.db import IntegrityError
from django.http import Http404
from django.shortcuts import get_object_or_404
from drf_yasg.utils import swagger_auto_schema
from pydash import get
from rest_framework import status, mixins
from rest_framework.generics import (
RetrieveAPIView, DestroyAPIView, UpdateAPIView, ListAPIView, RetrieveUpdateDestroyAPIView
)
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from core.collections.constants import (
INCLUDE_REFERENCES_PARAM, HEAD_OF_CONCEPT_ADDED_TO_COLLECTION,
HEAD_OF_MAPPING_ADDED_TO_COLLECTION, CONCEPT_ADDED_TO_COLLECTION_FMT, MAPPING_ADDED_TO_COLLECTION_FMT,
DELETE_FAILURE, DELETE_SUCCESS, NO_MATCH, VERSION_ALREADY_EXISTS,
SOURCE_MAPPINGS
)
from core.collections.documents import CollectionDocument
from core.collections.models import Collection, CollectionReference
from core.collections.search import CollectionSearch
from core.collections.serializers import (
CollectionDetailSerializer, CollectionListSerializer,
CollectionCreateSerializer, CollectionReferenceSerializer, CollectionVersionDetailSerializer,
CollectionVersionListSerializer)
from core.collections.utils import is_concept, is_version_specified
from core.common.constants import (
HEAD, RELEASED_PARAM, PROCESSING_PARAM, OK_MESSAGE, NOT_FOUND, MUST_SPECIFY_EXTRA_PARAM_IN_BODY
)
from core.common.mixins import (
ConceptDictionaryCreateMixin, ListWithHeadersMixin, ConceptDictionaryUpdateMixin,
ConceptContainerExportMixin,
ConceptContainerProcessingMixin)
from core.common.permissions import (
CanViewConceptDictionary, CanEditConceptDictionary, HasAccessToVersionedObject,
CanViewConceptDictionaryVersion
)
from core.common.swagger_parameters import q_param, compress_header, page_param, verbose_param, exact_match_param, \
include_facets_header, sort_asc_param, sort_desc_param, updated_since_param, include_retired_param, limit_param
from core.common.tasks import add_references, export_collection
from core.common.utils import compact_dict_by_values, parse_boolean_query_param
from core.common.views import BaseAPIView, BaseLogoView
logger = logging.getLogger('oclapi')
class CollectionBaseView(BaseAPIView):
lookup_field = 'collection'
pk_field = 'mnemonic'
model = Collection
permission_classes = (CanViewConceptDictionary,)
queryset = Collection.objects.filter(is_active=True)
def set_parent_resource(self):
from core.orgs.models import Organization
from core.users.models import UserProfile
org = self.kwargs.get('org', None)
user = self.kwargs.get('user', None)
if not user and self.user_is_self:
user = self.request.user.username
parent_resource = None
if org:
parent_resource = Organization.objects.filter(mnemonic=org).first()
if user:
parent_resource = UserProfile.objects.filter(username=user).first()
self.kwargs['parent_resource'] = self.parent_resource = parent_resource
def get_serializer_context(self):
context = super().get_serializer_context()
context.update({'request': self.request, INCLUDE_REFERENCES_PARAM: self.should_include_references()})
return context
@staticmethod
def get_detail_serializer(obj):
return CollectionDetailSerializer(obj)
def get_filter_params(self, default_version_to_head=True):
query_params = self.request.query_params.dict()
version = query_params.get('version', None) or self.kwargs.get('version', None)
if not version and default_version_to_head:
version = HEAD
kwargs = self.kwargs.copy()
if self.user_is_self and self.request.user.is_authenticated:
kwargs['user'] = self.request.user.username
return {
**query_params.copy(), **kwargs,
'version': version, 'include_references': self.should_include_references()
}
def get_queryset(self):
queryset = Collection.get_base_queryset(
compact_dict_by_values(self.get_filter_params())
).select_related(
'user', 'organization'
)
if self.is_verbose():
queryset = queryset.select_related('created_by', 'updated_by')
return queryset
def should_include_references(self):
return self.request.query_params.get(INCLUDE_REFERENCES_PARAM, 'false').lower() == 'true'
class CollectionVersionBaseView(CollectionBaseView):
def get_filter_params(self, default_version_to_head=False):
return super().get_filter_params(default_version_to_head)
class CollectionListView(CollectionBaseView, ConceptDictionaryCreateMixin, ListWithHeadersMixin):
serializer_class = CollectionListSerializer
is_searchable = True
es_fields = {
'collection_type': {'sortable': True, 'filterable': True, 'facet': True, 'exact': True},
'mnemonic': {'sortable': True, 'filterable': True, 'exact': True},
'name': {'sortable': True, 'filterable': True, 'exact': True},
'last_update': {'sortable': True, 'filterable': True, 'default': 'desc'},
'locale': {'sortable': False, 'filterable': True, 'facet': True},
'owner': {'sortable': True, 'filterable': True, 'facet': True, 'exact': True},
'owner_type': {'sortable': False, 'filterable': True, 'facet': True},
'custom_validation_schema': {'sortable': False, 'filterable': True},
'canonical_url': {'sortable': True, 'filterable': True},
}
document_model = CollectionDocument
facet_class = CollectionSearch
default_filters = dict(is_active=True, version=HEAD)
def get_serializer_class(self):
if self.request.method == 'GET' and self.is_verbose():
return CollectionDetailSerializer
if self.request.method == 'POST':
return CollectionCreateSerializer
return CollectionListSerializer
@swagger_auto_schema(
manual_parameters=[
q_param, limit_param, sort_desc_param, sort_asc_param, exact_match_param, page_param, verbose_param,
include_retired_param, updated_since_param, include_facets_header, compress_header
]
)
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def get_csv_rows(self, queryset=None): # pragma: no cover
if not queryset:
queryset = self.get_queryset()
values = queryset.values('mnemonic', 'name', 'full_name', 'collection_type', 'description', 'default_locale',
'supported_locales', 'website', 'external_id', 'updated_at', 'updated_by', 'uri')
for value in values:
value['Owner'] = Collection.objects.get(uri=value['uri']).parent.mnemonic
value['Collection ID'] = value.pop('mnemonic')
value['Collection Name'] = value.pop('name')
value['Collection Full Name'] = value.pop('full_name')
value['Collection Type'] = value.pop('collection_type')
value['Description'] = value.pop('description')
value['Default Locale'] = value.pop('default_locale')
value['Supported Locales'] = ",".join(value.pop('supported_locales'))
value['Website'] = value.pop('website')
value['External ID'] = value.pop('external_id')
value['Last Updated'] = value.pop('updated_at')
value['Updated By'] = value.pop('updated_by')
value['URI'] = value.pop('uri')
values.field_names.extend([
'Owner', 'Collection ID', 'Collection Name', 'Collection Full Name', 'Collection Type', 'Description',
'Default Locale', 'Supported Locales', 'Website', 'External ID', 'Last Updated', 'Updated By', 'URI'
])
del values.field_names[0:12]
return values
class CollectionLogoView(CollectionBaseView, BaseLogoView):
serializer_class = CollectionDetailSerializer
def get_permissions(self):
if self.request.method in ['GET', 'HEAD']:
return [CanViewConceptDictionary()]
return [IsAuthenticated(), CanEditConceptDictionary()]
class CollectionRetrieveUpdateDestroyView(CollectionBaseView, ConceptDictionaryUpdateMixin):
serializer_class = CollectionDetailSerializer
def get_object(self, queryset=None):
return self.get_queryset().filter(is_active=True).order_by('-created_at').first()
def get_permissions(self):
if self.request.method in ['GET', 'HEAD']:
return [CanViewConceptDictionary()]
return [IsAuthenticated(), CanEditConceptDictionary()]
def delete(self, request, *args, **kwargs): # pylint: disable=unused-argument
collection = self.get_object()
try:
collection.delete()
except Exception as ex:
return Response({'detail': get(ex, 'messages', [DELETE_FAILURE])}, status=status.HTTP_400_BAD_REQUEST)
return Response({'detail': DELETE_SUCCESS}, status=status.HTTP_204_NO_CONTENT)
def get(self, request, *args, **kwargs): # pylint: disable=unused-argument
instance = self.get_object()
if not instance:
return Response(status=status.HTTP_404_NOT_FOUND)
serializer = self.get_serializer(instance)
return Response(serializer.data)
class CollectionReferencesView(
CollectionBaseView, ConceptDictionaryUpdateMixin, RetrieveAPIView, DestroyAPIView, ListWithHeadersMixin
):
serializer_class = CollectionDetailSerializer
def get_permissions(self):
if self.request.method in ['GET', 'HEAD']:
return [CanViewConceptDictionary()]
return [CanEditConceptDictionary()]
def get_object(self, queryset=None):
instance = super().get_queryset().filter(is_active=True).order_by('-created_at').first()
if not instance:
raise Http404(NO_MATCH)
return instance
def get_queryset(self):
search_query = self.request.query_params.get('q', '')
sort = self.request.query_params.get('search_sort', 'ASC')
if sort == 'ASC':
sort = ''
else:
sort = '-'
instance = self.get_object()
queryset = instance.references
if search_query:
queryset = queryset.filter(expression__icontains=search_query).order_by(sort + 'expression')
return queryset.all()
def retrieve(self, request, *args, **kwargs):
self.serializer_class = CollectionReferenceSerializer
return self.list(request, *args, **kwargs)
def destroy(self, request, *args, **kwargs):
instance = self.get_object()
expressions = request.data.get("references") or request.data.get("expressions")
if not expressions:
return Response(status=status.HTTP_400_BAD_REQUEST)
if expressions == '*':
expressions = list(instance.references.values_list('expression', flat=True))
if self.should_cascade_mappings():
expressions += self.get_related_mappings_with_version_information(instance, expressions)
instance.delete_references(expressions)
return Response({'message': OK_MESSAGE}, status=status.HTTP_204_NO_CONTENT)
def update(self, request, *args, **kwargs): # pylint: disable=too-many-locals,unused-argument # Fixme: Sny
collection = self.get_object()
cascade_mappings = self.should_cascade_mappings()
data = request.data.get('data')
concept_expressions = data.get('concepts', [])
mapping_expressions = data.get('mappings', [])
expressions = data.get('expressions', [])
adding_all = mapping_expressions == '*' or concept_expressions == '*'
if adding_all:
add_references.delay(self.request.user, data, collection, self.get_host_url(), cascade_mappings)
return Response([], status=status.HTTP_202_ACCEPTED)
(added_references, errors) = collection.add_expressions(
data, self.get_host_url(), request.user, cascade_mappings
)
all_expressions = expressions + concept_expressions + mapping_expressions
added_expressions = [reference.expression for reference in added_references]
added_original_expressions = set(
[reference.original_expression for reference in added_references] + all_expressions
)
response = []
for expression in added_original_expressions:
response_item = self.create_response_item(added_expressions, errors, expression)
if response_item:
response.append(response_item)
return Response(response, status=status.HTTP_200_OK)
def should_cascade_mappings(self):
return self.request.query_params.get('cascade', '').lower() == SOURCE_MAPPINGS
def create_response_item(self, added_expressions, errors, expression):
adding_expression_failed = len(errors) > 0 and expression in errors
if adding_expression_failed:
return self.create_error_message(errors, expression)
return self.create_success_message(added_expressions, expression)
def create_success_message(self, added_expressions, expression):
message = self.select_update_message(expression)
references = list(filter(lambda expr: expr.startswith(expression), added_expressions))
if len(references) < 1:
return None
return {
'added': True,
'expression': references[0],
'message': message
}
@staticmethod
def create_error_message(errors, expression):
error_message = errors.get(expression, {})
return {
'added': False,
'expression': expression,
'message': error_message
}
def select_update_message(self, expression):
adding_head_version = not is_version_specified(expression)
expression_parts = expression.split('/')
resource_type = expression_parts[5]
if adding_head_version:
return self.adding_to_head_message_by_type(resource_type)
resource_name = expression_parts[6]
return self.version_added_message_by_type(resource_name, self.parent_resource.name, resource_type)
@staticmethod
def adding_to_head_message_by_type(resource_type):
if resource_type == 'concepts':
return HEAD_OF_CONCEPT_ADDED_TO_COLLECTION
return HEAD_OF_MAPPING_ADDED_TO_COLLECTION
@staticmethod
def version_added_message_by_type(resource_name, collection_name, resource_type):
if resource_type == 'concepts':
return CONCEPT_ADDED_TO_COLLECTION_FMT.format(resource_name, collection_name)
return MAPPING_ADDED_TO_COLLECTION_FMT.format(resource_name, collection_name)
def get_related_mappings_with_version_information(self, instance, expressions):
related_mappings = []
for expression in expressions:
if is_concept(expression):
concepts = CollectionReference.get_concept_heads_from_expression(expression)
for concept in concepts:
related_mappings += concept.get_unidirectional_mappings()
return self.get_version_information_of_related_mappings(instance, related_mappings)
@staticmethod
def get_version_information_of_related_mappings(instance, related_mappings):
return list(
instance.references.filter(
expression__in=[mapping.url for mapping in related_mappings]
).values_list('expression', flat=True)
)
class CollectionVersionReferencesView(CollectionVersionBaseView, ListWithHeadersMixin):
serializer_class = CollectionReferenceSerializer
def get(self, request, *args, **kwargs):
query_params = self.request.query_params
search_query = query_params.get('q', '')
sort = query_params.get('search_sort', 'ASC')
object_version = self.get_queryset().first().head
references = object_version.references.filter(expression__icontains=search_query)
self.object_list = references if sort == 'ASC' else list(reversed(references))
return self.list(request, *args, **kwargs)
class CollectionVersionListView(CollectionVersionBaseView, mixins.CreateModelMixin, ListWithHeadersMixin):
released_filter = None
processing_filter = None
default_qs_sort_attr = 'created_at'
def get_serializer_class(self):
if self.request.method in ['GET', 'HEAD']:
return CollectionVersionDetailSerializer if self.is_verbose() else CollectionListSerializer
if self.request.method == 'POST':
return CollectionCreateSerializer
return CollectionVersionListSerializer
def get(self, request, *args, **kwargs):
self.released_filter = parse_boolean_query_param(request, RELEASED_PARAM, self.released_filter)
self.processing_filter = parse_boolean_query_param(request, PROCESSING_PARAM, self.processing_filter)
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
def create(self, request, *args, **kwargs):
head_object = self.get_queryset().first()
version = request.data.pop('id', None)
payload = {
"mnemonic": head_object.mnemonic, "id": head_object.mnemonic, "name": head_object.name, **request.data,
"organization_id": head_object.organization_id, "user_id": head_object.user_id,
'version': version
}
serializer = self.get_serializer(data=payload)
if serializer.is_valid():
try:
instance = serializer.create_version(payload)
if serializer.is_valid():
serializer = CollectionDetailSerializer(instance, context={'request': request})
data = serializer.data
version_id = data.get('uuid')
export_collection.delay(version_id)
return Response(data, status=status.HTTP_201_CREATED)
except IntegrityError as ex:
return Response(
dict(
error=str(ex), detail=VERSION_ALREADY_EXISTS.format(version)
),
status=status.HTTP_409_CONFLICT
)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get_queryset(self):
queryset = super().get_queryset()
if self.released_filter is not None:
queryset = queryset.filter(released=self.released_filter)
return queryset.order_by('-created_at')
class CollectionLatestVersionRetrieveUpdateView(CollectionVersionBaseView, RetrieveAPIView, UpdateAPIView):
serializer_class = CollectionVersionDetailSerializer
permission_classes = (CanViewConceptDictionaryVersion,)
def get_filter_params(self, default_version_to_head=False):
params = super().get_filter_params(default_version_to_head)
params['is_latest'] = True
return params
def get_object(self, queryset=None):
obj = get_object_or_404(self.get_queryset(), released=True)
self.check_object_permissions(self.request, obj)
return obj
def update(self, request, *args, **kwargs):
self.object = self.get_object()
head = self.object.head
if not head:
return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)
serializer = self.get_serializer(self.object, data=request.data, partial=True)
if serializer.is_valid():
self.object = serializer.save(force_update=True)
if serializer.is_valid():
serializer = CollectionVersionDetailSerializer(self.object, context={'request': request})
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class CollectionVersionRetrieveUpdateDestroyView(CollectionBaseView, RetrieveAPIView, UpdateAPIView):
permission_classes = (HasAccessToVersionedObject,)
serializer_class = CollectionVersionDetailSerializer
def get_object(self, queryset=None):
return get_object_or_404(self.get_queryset())
def update(self, request, *args, **kwargs):
self.object = self.get_object()
head = self.object.head
if not head:
return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)
serializer = self.get_serializer(self.object, data=request.data, partial=True)
if serializer.is_valid():
self.object = serializer.save(force_update=True)
if serializer.is_valid():
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, _, **kwargs): # pylint: disable=unused-argument
instance = self.get_object()
try:
instance.delete()
except ValidationError as ex:
return Response(ex.message_dict, status=status.HTTP_400_BAD_REQUEST)
return Response(status=status.HTTP_204_NO_CONTENT)
class CollectionExtrasBaseView(CollectionBaseView):
def get_object(self, queryset=None):
return get_object_or_404(self.get_queryset(), version=HEAD)
class CollectionExtrasView(CollectionExtrasBaseView, ListAPIView):
serializer_class = CollectionDetailSerializer
def list(self, request, *args, **kwargs):
return Response(get(self.get_object(), 'extras', {}))
class CollectionExtraRetrieveUpdateDestroyView(CollectionExtrasBaseView, RetrieveUpdateDestroyAPIView):
serializer_class = CollectionDetailSerializer
def retrieve(self, request, *args, **kwargs):
key = kwargs.get('extra')
instance = self.get_object()
extras = get(instance, 'extras', {})
if key in extras:
return Response({key: extras[key]})
return Response(dict(detail=NOT_FOUND), status=status.HTTP_404_NOT_FOUND)
def update(self, request, **kwargs):
key = kwargs.get('extra')
value = request.data.get(key)
if not value:
return Response([MUST_SPECIFY_EXTRA_PARAM_IN_BODY.format(key)], status=status.HTTP_400_BAD_REQUEST)
instance = self.get_object()
instance.extras = get(instance, 'extras', {})
instance.extras[key] = value
instance.comment = 'Updated extras: %s=%s.' % (key, value)
head = instance.get_head()
head.extras = get(head, 'extras', {})
head.extras.update(instance.extras)
instance.save()
head.save()
return Response({key: value})
def delete(self, request, *args, **kwargs):
key = kwargs.get('extra')
instance = self.get_object()
instance.extras = get(instance, 'extras', {})
if key in instance.extras:
del instance.extras[key]
instance.comment = 'Deleted extra %s.' % key
head = instance.get_head()
head.extras = get(head, 'extras', {})
del head.extras[key]
instance.save()
head.save()
return Response(status=status.HTTP_204_NO_CONTENT)
return Response(dict(detail=NOT_FOUND), status=status.HTTP_404_NOT_FOUND)
class CollectionVersionProcessingView(CollectionBaseView, ConceptContainerProcessingMixin):
serializer_class = CollectionVersionDetailSerializer
resource = 'collection'
class CollectionVersionExportView(ConceptContainerExportMixin, CollectionVersionBaseView):
entity = 'Collection'
permission_classes = (CanViewConceptDictionary,)
serializer_class = CollectionVersionDetailSerializer
def handle_export_version(self):
version = self.get_object()
try:
export_collection.delay(version.id)
return status.HTTP_202_ACCEPTED
except AlreadyQueued:
return status.HTTP_409_CONFLICT