-
Notifications
You must be signed in to change notification settings - Fork 32
/
serializers.py
287 lines (242 loc) · 11.2 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
import logging
from collections import OrderedDict
from rest_framework import serializers
from rest_framework.fields import CharField, SerializerMethodField, \
DateTimeField
from core.common.fhir_helpers import delete_empty_fields
from core.concept_maps.constants import RESOURCE_TYPE
from core.common.constants import HEAD
from core.common.serializers import StatusField, IdentifierSerializer
from core.mappings.constants import SAME_AS
from core.mappings.models import Mapping
from core.mappings.serializers import MappingDetailSerializer
from core.orgs.models import Organization
from core.parameters.serializers import ParametersSerializer
from core.sources.models import Source
from core.sources.serializers import SourceCreateOrUpdateSerializer
from core.users.models import UserProfile
logger = logging.getLogger('oclapi')
class ConceptMapGroupField(serializers.Field):
def to_internal_value(self, data):
mappings = []
for group in data:
for element in group.get('element', []):
for target in element.get('target', []):
map_type = target.get('equivalence')
if map_type == 'equivalent':
map_type = SAME_AS
mapping = {
'from_source_url': IdentifierSerializer.convert_fhir_url_to_ocl_uri(group.get('source'),
'sources'),
'to_source_url': IdentifierSerializer.convert_fhir_url_to_ocl_uri(group.get('target'),
'sources'),
'from_concept_code': element.get('code'),
'to_concept_code': target.get('code'),
'map_type': map_type
}
mappings.append(mapping)
return {'mappings': mappings}
def to_representation(self, value):
# limit to 1000 mappings by default
# TODO: support graphQL to go around the limit
limit = self.get_limit()
mappings = value.get_mappings_queryset().filter(retired=False).order_by('id')[:limit]
groups = {}
for mapping in mappings:
key = (mapping.from_source_url or '') + (mapping.to_source_url or '')
group = groups.get(key)
if not group:
if mapping.from_source and mapping.from_source.canonical_url:
from_url = mapping.from_source.canonical_url
else:
from_url = IdentifierSerializer.convert_ocl_uri_to_fhir_url(mapping.from_source_url, RESOURCE_TYPE)
if mapping.to_source and mapping.to_source.canonical_url:
to_url = mapping.to_source.canonical_url
else:
to_url = IdentifierSerializer.convert_ocl_uri_to_fhir_url(mapping.to_source_url, RESOURCE_TYPE)
group = {
'source': from_url,
'target': to_url
}
groups.update({key: group})
elements = group.get('element')
if not elements:
elements = []
group.update({'element': elements})
element = None
for candidate in elements:
if candidate.get('code') is mapping.from_concept_code:
element = candidate
break
if not element:
element = {
'code': mapping.from_concept_code,
'target': []
}
elements.append(element)
targets = element.get('target')
relationship = mapping.map_type
if mapping.map_type == SAME_AS:
relationship = 'equivalent'
targets.append({
'code': mapping.to_concept_code,
'equivalence': relationship
})
return [*groups.values()]
def get_limit(self):
if self.context.get('has_many', False):
limit = 25
else:
limit = 1000
return limit
class ConceptMapDetailSerializer(serializers.ModelSerializer):
resourceType = SerializerMethodField(method_name='get_resource_type')
id = CharField(source='mnemonic')
url = CharField(source='canonical_url', required=False)
title = CharField(source='full_name', required=False)
status = StatusField(source='*')
language = CharField(source='default_locale', required=False)
meta = SerializerMethodField()
identifier = IdentifierSerializer(many=True, required=False)
date = DateTimeField(source='revision_date', required=False)
group = ConceptMapGroupField(source='*', required=False)
class Meta:
model = Source
fields = ('resourceType', 'url', 'title', 'status', 'id', 'language', 'meta',
'version', 'identifier', 'contact', 'jurisdiction', 'name', 'description', 'publisher', 'purpose',
'copyright', 'date', 'experimental', 'group')
def __new__(cls, *args, **kwargs):
if kwargs.get('many', False):
context = kwargs.get('context', {})
context.update({'has_many': True})
kwargs.update({'context': context})
return super().__new__(cls, *args, **kwargs)
@staticmethod
def get_resource_type(_):
return RESOURCE_TYPE
@staticmethod
def get_meta(obj):
return {'lastUpdated': DateTimeField().to_representation(obj.updated_at)}
def to_representation(self, instance):
try:
rep = super().to_representation(instance)
delete_empty_fields(rep)
IdentifierSerializer.include_ocl_identifier(instance.uri, RESOURCE_TYPE, rep)
except (Exception, ):
msg = f'Failed to represent "{instance.uri}" as {RESOURCE_TYPE}'
logger.exception(msg)
return {
'resourceType': 'OperationOutcome',
'issue': [{
'severity': 'error',
'details': msg
}]
}
# Remove fields with 'None' value
return OrderedDict([(key, rep[key]) for key in rep if rep[key] is not None])
def create(self, validated_data):
mappings = validated_data.pop('mappings', [])
uri = self.context['request'].path + validated_data['mnemonic']
ident = IdentifierSerializer.include_ocl_identifier(uri, RESOURCE_TYPE, validated_data)
source = SourceCreateOrUpdateSerializer().prepare_object(validated_data)
if ident['owner_type'] == 'orgs':
owner = Organization.objects.filter(mnemonic=ident['owner_id']).first()
else:
owner = UserProfile.objects.filter(username=ident['owner_id']).first()
source.set_parent(owner)
source.source_type = 'ConceptMap'
user = self.context['request'].user
version = source.version # remember version if set
source.version = HEAD
errors = Source.persist_new(source, user)
if errors:
self._errors.update(errors)
return source
for mapping in mappings:
mapping.update({'parent_id': source.id})
mapping_serializer = MappingDetailSerializer(data=mapping)
mapping_serializer.is_valid(raise_exception=True)
Mapping.persist_new(mapping_serializer.validated_data, user)
# Create new version
source.version = '0.1' if version == HEAD else version
source.id = None # pylint: disable=invalid-name
errors = Source.persist_new_version(source, user)
self._errors.update(errors)
return source
@staticmethod
def is_mapping_same(first, second):
if not isinstance(first, dict):
first = vars(first)
if not isinstance(second, dict):
second = vars(second)
return first.get('from_source_url', None) == second.get('from_source_url', None) and \
first.get('to_source_url', None) == second.get('to_source_url', None) and \
first.get('from_concept_code', None) == second.get('from_concept_code', None) and \
first.get('to_concept_code', None) == second.get('to_concept_code', None) and \
first.get('map_type', None) == second.get('map_type', None)
def update(self, instance, validated_data):
mappings = validated_data.pop('mappings', [])
source = SourceCreateOrUpdateSerializer().prepare_object(validated_data, instance)
# Preserve version specific values
source_version = source.version
source_released = source.released
user = self.context['request'].user
# Update HEAD first
# Determine existing source ID
source_head = source.head
source.id = source_head.id
source.version = HEAD
source.released = False # HEAD must never be released
source.custom_validation_schema = source_head.custom_validation_schema
errors = Source.persist_changes(source, user, None)
if errors:
self._errors.update(errors)
return source
# Retire mapping if it does not exist in HEAD
for mapping in source.mappings.filter(retired=False):
found = False
for new_mapping in mappings:
if ConceptMapDetailSerializer.is_mapping_same(mapping, new_mapping):
found = True
if not found:
mapping.retire(user, 'Deleted from ConceptMap resource')
source.refresh_from_db()
# Add a new mapping if it does not exist in HEAD
for new_mapping in mappings:
found = False
for mapping in source.mappings.filter(retired=False):
if ConceptMapDetailSerializer.is_mapping_same(mapping, new_mapping):
found = True
if not found:
new_mapping.update({'parent_id': source.id})
new_mapping_serializer = MappingDetailSerializer(data=new_mapping)
new_mapping_serializer.is_valid(raise_exception=True)
Mapping.persist_new(new_mapping_serializer.validated_data, user)
existing_source_version = source.versions.filter(version=source_version)
if existing_source_version:
existing_source_version.delete()
source.id = None
source.version = source_version
source.released = source_released
errors = Source.persist_new_version(source, user)
self._errors.update(errors)
return source
class ConceptMapParametersSerializer(ParametersSerializer):
def update(self, instance, validated_data):
pass
def create(self, validated_data):
pass
allowed_input_parameters = {
'url': 'valueUri',
'conceptMapVersion': 'valueString',
'code': 'valueCode',
'system': 'valueUri',
'version': 'valueString',
'source': 'valueUri',
'coding': 'valueCoding',
'codeableConcept': 'valueCodeableConcept',
'target': 'valueUri',
'targetsystem': 'valueUri',
# TODO: dependency?
'reverse': 'valueBoolean'
}