-
Notifications
You must be signed in to change notification settings - Fork 28
/
serializers.py
415 lines (338 loc) · 17.5 KB
/
serializers.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
from django.core.validators import RegexValidator
from pydash import get
from rest_framework.fields import CharField, ChoiceField, ListField, IntegerField, DateTimeField, JSONField, \
BooleanField, SerializerMethodField
from rest_framework.relations import PrimaryKeyRelatedField
from rest_framework.serializers import ModelSerializer
from core.client_configs.serializers import ClientConfigSerializer
from core.collections.constants import INCLUDE_REFERENCES_PARAM
from core.collections.models import Collection, CollectionReference, Expansion
from core.common.constants import HEAD, DEFAULT_ACCESS_TYPE, NAMESPACE_REGEX, ACCESS_TYPE_CHOICES, INCLUDE_SUMMARY, \
INCLUDE_CLIENT_CONFIGS, INVALID_EXPANSION_URL
from core.orgs.models import Organization
from core.settings import DEFAULT_LOCALE
from core.users.models import UserProfile
class CollectionListSerializer(ModelSerializer):
short_code = CharField(source='mnemonic')
owner = CharField(source='parent_resource')
owner_type = CharField(source='parent_resource_type')
owner_url = CharField(source='parent_url')
id = CharField(source='mnemonic')
class Meta:
model = Collection
fields = (
'short_code', 'name', 'url', 'owner', 'owner_type', 'owner_url', 'version', 'created_at', 'id',
'collection_type', 'updated_at', 'canonical_url', 'autoexpand_head'
)
class CollectionVersionListSerializer(ModelSerializer):
short_code = CharField(source='mnemonic')
owner = CharField(source='parent_resource')
owner_type = CharField(source='parent_resource_type')
owner_url = CharField(source='parent_url')
id = CharField(source='version')
version_url = CharField(source='uri')
url = CharField(source='versioned_object_url')
previous_version_url = CharField(source='prev_version_uri')
autoexpand = SerializerMethodField()
expansion_url = CharField(source='expansion_uri', read_only=True)
class Meta:
model = Collection
fields = (
'short_code', 'name', 'url', 'owner', 'owner_type', 'owner_url', 'version', 'created_at', 'id',
'collection_type', 'updated_at', 'canonical_url', 'released', 'retired', 'version_url',
'previous_version_url', 'autoexpand', 'expansion_url',
)
@staticmethod
def get_autoexpand(obj):
return obj.autoexpand_head if obj.is_head else obj.autoexpand
class CollectionCreateOrUpdateSerializer(ModelSerializer):
class Meta:
model = Collection
lookup_field = 'mnemonic'
fields = (
'__all__'
)
def prepare_object(self, validated_data, instance=None):
collection = instance if instance else Collection()
collection.version = validated_data.get('version', collection.version) or HEAD
collection.mnemonic = validated_data.get(self.Meta.lookup_field, collection.mnemonic)
collection.public_access = validated_data.get('public_access', collection.public_access or DEFAULT_ACCESS_TYPE)
collection.default_locale = validated_data.get('default_locale', collection.default_locale or DEFAULT_LOCALE)
supported_locales = validated_data.get('supported_locales')
if not supported_locales:
supported_locales = collection.supported_locales
if supported_locales and isinstance(supported_locales, str):
supported_locales = supported_locales.split(',')
collection.supported_locales = supported_locales or [collection.default_locale]
for attr in [
'experimental', 'locked_date', 'text', 'revision_date', 'immutable', 'copyright', 'purpose',
'jurisdiction', 'contact', 'publisher', 'identifier', 'canonical_url', 'retired', 'released',
'organization', 'user', 'organization_id', 'user_id', 'external_id', 'extras', 'preferred_source',
'repository_type', 'custom_resources_linked_source', 'website', 'custom_validation_schema',
'collection_type', 'description', 'name', 'meta',
]:
setattr(collection, attr, validated_data.get(attr, get(collection, attr)))
collection.full_name = validated_data.get('full_name', collection.full_name) or collection.name
collection.autoexpand_head = validated_data.get('autoexpand_head', collection.autoexpand_head)
collection.autoexpand = validated_data.get('autoexpand', collection.autoexpand)
collection.expansion_uri = validated_data.get('expansion_uri', collection.expansion_uri)
if collection.id and collection.expansion_uri and not collection.expansions.filter(
uri=collection.expansion_uri).exists():
self._errors.update({'expansion_url': INVALID_EXPANSION_URL})
return collection
def update(self, instance, validated_data):
original_schema = instance.custom_validation_schema
collection = self.prepare_object(validated_data, instance)
if self._errors:
return collection
user = self.context['request'].user
errors = Collection.persist_changes(collection, user, original_schema)
self._errors.update(errors)
return collection
class CollectionCreateSerializer(CollectionCreateOrUpdateSerializer):
type = CharField(source='resource_type', read_only=True)
uuid = CharField(source='id', read_only=True)
id = CharField(required=True, validators=[RegexValidator(regex=NAMESPACE_REGEX)], source='mnemonic')
short_code = CharField(source='mnemonic', read_only=True)
name = CharField(required=True)
full_name = CharField(required=False)
description = CharField(required=False, allow_blank=True)
text = CharField(required=False, allow_blank=True)
collection_type = CharField(required=False)
custom_validation_schema = CharField(required=False, allow_blank=True, allow_null=True)
public_access = ChoiceField(required=False, choices=ACCESS_TYPE_CHOICES)
default_locale = CharField(required=False, allow_blank=True)
supported_locales = ListField(required=False, allow_empty=True)
website = CharField(required=False, allow_blank=True)
url = CharField(read_only=True)
canonical_url = CharField(required=False, allow_null=True, allow_blank=True)
custom_resources_linked_source = CharField(required=False, allow_null=True, allow_blank=True)
repository_type = CharField(required=False, allow_null=True, allow_blank=True)
preferred_source = CharField(required=False, allow_null=True, allow_blank=True)
versions_url = CharField(read_only=True)
concepts_url = CharField(read_only=True)
mappings_url = CharField(read_only=True)
owner = CharField(source='parent_resource', read_only=True)
owner_type = CharField(source='parent_resource_type', read_only=True)
owner_url = CharField(source='parent_url', read_only=True)
versions = IntegerField(source='num_versions', read_only=True)
created_on = DateTimeField(source='created_at', read_only=True)
updated_on = DateTimeField(source='updated_at', read_only=True)
created_by = CharField(source='owner', read_only=True)
updated_by = CharField(read_only=True)
extras = JSONField(required=False, allow_null=True)
external_id = CharField(required=False, allow_blank=True)
user_id = PrimaryKeyRelatedField(required=False, queryset=UserProfile.objects.all(), allow_null=True)
organization_id = PrimaryKeyRelatedField(required=False, queryset=Organization.objects.all(), allow_null=True)
version = CharField(default=HEAD)
identifier = JSONField(required=False, allow_null=True)
contact = JSONField(required=False, allow_null=True)
jurisdiction = JSONField(required=False, allow_null=True)
meta = JSONField(required=False, allow_null=True)
publisher = CharField(required=False, allow_null=True, allow_blank=True)
purpose = CharField(required=False, allow_null=True, allow_blank=True)
copyright = CharField(required=False, allow_null=True, allow_blank=True)
experimental = BooleanField(required=False, allow_null=True, default=None)
locked_date = DateTimeField(required=False, allow_null=True)
autoexpand_head = BooleanField(required=False, default=True)
autoexpand = BooleanField(required=False, default=True)
def create(self, validated_data):
collection = self.prepare_object(validated_data)
user = self.context['request'].user
errors = Collection.persist_new(collection, user)
self._errors.update(errors)
return collection
def create_version(self, validated_data):
collection = self.prepare_object(validated_data)
user = self.context['request'].user
errors = Collection.persist_new_version(collection, user)
self._errors.update(errors)
return collection
class ExpansionSummarySerializer(ModelSerializer):
concepts = IntegerField(source='active_concepts', read_only=True)
mappings = IntegerField(source='active_mappings', read_only=True)
class Meta:
model = Expansion
fields = ('concepts', 'mappings')
class ExpansionSummaryDetailSerializer(ExpansionSummarySerializer):
class Meta:
model = Expansion
fields = ExpansionSummarySerializer.Meta.fields + ('id', 'mnemonic')
class CollectionSummarySerializer(ModelSerializer):
versions = IntegerField(source='num_versions')
class Meta:
model = Collection
fields = ('active_mappings', 'active_concepts', 'versions')
class CollectionSummaryDetailSerializer(CollectionSummarySerializer):
uuid = CharField(source='id')
id = CharField(source='mnemonic')
class Meta:
model = Collection
fields = (
*CollectionSummarySerializer.Meta.fields, 'id', 'uuid',
)
class CollectionVersionSummarySerializer(ModelSerializer):
class Meta:
model = Collection
fields = ('active_mappings', 'active_concepts')
class CollectionVersionSummaryDetailSerializer(CollectionVersionSummarySerializer):
uuid = CharField(source='id')
id = CharField(source='version')
class Meta:
model = Collection
fields = (
*CollectionVersionSummarySerializer.Meta.fields, 'id', 'uuid',
)
class CollectionDetailSerializer(CollectionCreateOrUpdateSerializer):
type = CharField(source='resource_type')
uuid = CharField(source='id')
id = CharField(source='mnemonic')
short_code = CharField(source='mnemonic')
owner = CharField(source='parent_resource')
owner_type = CharField(source='parent_resource_type')
owner_url = CharField(source='parent_url')
created_on = DateTimeField(source='created_at')
updated_on = DateTimeField(source='updated_at')
supported_locales = ListField(required=False, allow_empty=True)
created_by = CharField(read_only=True, source='created_by.username')
updated_by = CharField(read_only=True, source='updated_by.username')
references = SerializerMethodField()
summary = SerializerMethodField()
client_configs = SerializerMethodField()
class Meta:
model = Collection
lookup_field = 'mnemonic'
fields = (
'type', 'uuid', 'id', 'short_code', 'name', 'full_name', 'description', 'collection_type',
'custom_validation_schema', 'public_access', 'default_locale', 'supported_locales', 'website',
'url', 'owner', 'owner_type', 'owner_url',
'created_on', 'updated_on', 'created_by', 'updated_by', 'extras', 'external_id', 'versions_url',
'version', 'concepts_url', 'mappings_url',
'custom_resources_linked_source', 'repository_type', 'preferred_source', 'references',
'canonical_url', 'identifier', 'publisher', 'contact', 'jurisdiction', 'purpose', 'copyright', 'meta',
'immutable', 'revision_date', 'logo_url', 'summary', 'text', 'client_configs',
'experimental', 'locked_date', 'internal_reference_id', 'autoexpand_head'
)
def __init__(self, *args, **kwargs):
params = get(kwargs, 'context.request.query_params')
self.query_params = params.dict() if params else dict()
self.include_summary = self.query_params.get(INCLUDE_SUMMARY) in ['true', True]
self.include_client_configs = self.query_params.get(INCLUDE_CLIENT_CONFIGS) in ['true', True]
try:
if not self.include_summary:
self.fields.pop('summary', None)
if not self.include_client_configs:
self.fields.pop('client_configs', None)
except: # pylint: disable=bare-except
pass
super().__init__(*args, **kwargs)
def get_summary(self, obj):
summary = None
if self.include_summary:
summary = CollectionSummarySerializer(obj).data
return summary
def get_client_configs(self, obj):
if self.include_client_configs:
return ClientConfigSerializer(obj.client_configs.filter(is_active=True), many=True).data
return None
def get_references(self, obj):
if self.context.get(INCLUDE_REFERENCES_PARAM, False):
return CollectionReferenceSerializer(obj.references.all(), many=True).data
return []
class CollectionVersionDetailSerializer(CollectionCreateOrUpdateSerializer):
type = CharField(source='resource_version_type')
uuid = CharField(source='id')
id = CharField(source='version')
short_code = CharField(source='mnemonic')
owner = CharField(source='parent_resource')
owner_type = CharField(source='parent_resource_type')
owner_url = CharField(source='parent_url')
created_on = DateTimeField(source='created_at')
updated_on = DateTimeField(source='updated_at')
supported_locales = ListField(required=False, allow_empty=True)
is_processing = BooleanField(read_only=True)
released = BooleanField(default=False)
version_url = CharField(source='uri')
url = CharField(source='versioned_object_url')
previous_version_url = CharField(source='prev_version_uri')
created_by = CharField(read_only=True, source='created_by.username')
updated_by = CharField(read_only=True, source='updated_by.username')
summary = SerializerMethodField()
autoexpand = SerializerMethodField()
expansion_url = CharField(source='expansion_uri', allow_null=True, allow_blank=True)
class Meta:
model = Collection
lookup_field = 'mnemonic'
fields = (
'type', 'uuid', 'id', 'short_code', 'name', 'full_name', 'description', 'collection_type',
'custom_validation_schema', 'public_access', 'default_locale', 'supported_locales', 'website',
'url', 'owner', 'owner_type', 'owner_url', 'version_url', 'previous_version_url',
'created_on', 'updated_on', 'created_by', 'updated_by', 'extras', 'external_id', 'version',
'version', 'concepts_url', 'mappings_url', 'is_processing', 'released', 'retired',
'canonical_url', 'identifier', 'publisher', 'contact', 'jurisdiction', 'purpose', 'copyright', 'meta',
'immutable', 'revision_date', 'summary', 'text', 'experimental', 'locked_date', 'internal_reference_id',
'autoexpand', 'expansion_url'
)
def __init__(self, *args, **kwargs):
params = get(kwargs, 'context.request.query_params')
self.include_summary = False
if params:
self.query_params = params.dict()
self.include_summary = self.query_params.get(INCLUDE_SUMMARY) in ['true', True]
try:
if not self.include_summary:
self.fields.pop('summary', None)
except: # pylint: disable=bare-except
pass
super().__init__(*args, **kwargs)
def get_summary(self, obj):
summary = None
if self.include_summary:
summary = CollectionVersionSummarySerializer(obj).data
return summary
@staticmethod
def get_autoexpand(obj):
return obj.autoexpand_head if obj.is_head else obj.autoexpand
class CollectionReferenceSerializer(ModelSerializer):
reference_type = CharField(read_only=True)
class Meta:
model = CollectionReference
fields = ('expression', 'reference_type', 'id', 'last_resolved_at')
class CollectionReferenceDetailSerializer(CollectionReferenceSerializer):
class Meta:
model = CollectionReference
fields = (
*CollectionReferenceSerializer.Meta.fields, 'last_resolved_at', 'created_at', 'updated_at',
'internal_reference_id'
)
class CollectionVersionExportSerializer(CollectionVersionDetailSerializer):
collection = JSONField(source='snapshot')
class Meta:
model = Collection
fields = (
*CollectionVersionDetailSerializer.Meta.fields, 'collection'
)
class ExpansionSerializer(ModelSerializer):
summary = SerializerMethodField()
url = CharField(source='uri', read_only=True)
parameters = JSONField()
class Meta:
model = Expansion
fields = ('mnemonic', 'id', 'parameters', 'canonical_url', 'url', 'summary')
def __init__(self, *args, **kwargs):
params = get(kwargs, 'context.request.query_params')
self.include_summary = False
if params:
self.query_params = params.dict()
self.include_summary = self.query_params.get(INCLUDE_SUMMARY) in ['true', True]
try:
if not self.include_summary:
self.fields.pop('summary', None)
except: # pylint: disable=bare-except
pass
super().__init__(*args, **kwargs)
def get_summary(self, obj):
summary = None
if self.include_summary:
summary = ExpansionSummarySerializer(obj).data
return summary