-
Notifications
You must be signed in to change notification settings - Fork 32
/
serializers.py
226 lines (191 loc) · 10.6 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
from pydash import get
from rest_framework.fields import CharField, JSONField, IntegerField, DateTimeField, ListField, SerializerMethodField
from rest_framework.serializers import ModelSerializer
from core.common.constants import MAPPING_LOOKUP_CONCEPTS, MAPPING_LOOKUP_SOURCES, MAPPING_LOOKUP_FROM_CONCEPT, \
MAPPING_LOOKUP_TO_CONCEPT, MAPPING_LOOKUP_FROM_SOURCE, MAPPING_LOOKUP_TO_SOURCE, INCLUDE_EXTRAS_PARAM, \
INCLUDE_SOURCE_VERSIONS, INCLUDE_COLLECTION_VERSIONS, INCLUDE_VERBOSE_REFERENCES
from core.common.fields import EncodedDecodedCharField
from core.concepts.serializers import ConceptListSerializer, ConceptDetailSerializer
from core.mappings.models import Mapping
from core.sources.serializers import SourceListSerializer, SourceDetailSerializer
class MappingListSerializer(ModelSerializer):
type = CharField(source='resource_type', read_only=True)
id = CharField(source='mnemonic', required=False)
uuid = CharField(source='id', read_only=True)
source = CharField(source='parent_resource', read_only=True)
owner = CharField(source='owner_name', read_only=True)
update_comment = CharField(source='comment', required=False, allow_null=True, allow_blank=True)
url = CharField(required=False, source='versioned_object_url')
version = CharField(read_only=True)
version_created_on = DateTimeField(source='created_at', read_only=True)
from_concept = ConceptListSerializer()
to_concept = ConceptListSerializer()
from_source = SourceListSerializer()
to_source = SourceListSerializer()
from_concept_name_resolved = CharField(source='from_concept.display_name', read_only=True)
to_concept_name_resolved = CharField(source='to_concept.display_name', read_only=True)
to_concept_code = EncodedDecodedCharField(required=False)
from_concept_code = EncodedDecodedCharField(required=False)
references = SerializerMethodField()
class Meta:
model = Mapping
fields = (
'external_id', 'retired', 'map_type', 'source', 'owner', 'owner_type',
'from_concept_code', 'from_concept_name', 'from_concept_url',
'to_concept_code', 'to_concept_name', 'to_concept_url',
'from_source_owner', 'from_source_owner_type', 'from_source_url', 'from_source_name',
'to_source_owner', 'to_source_owner_type', 'to_source_url', 'to_source_name',
'url', 'version', 'id', 'versioned_object_id', 'versioned_object_url',
'is_latest_version', 'update_comment', 'version_url', 'uuid', 'version_created_on',
'from_source_version', 'to_source_version', 'from_concept', 'to_concept', 'from_source', 'to_source',
'from_concept_name_resolved', 'to_concept_name_resolved', 'extras', 'type', 'references'
)
def __init__(self, *args, **kwargs):
request = get(kwargs, 'context.request')
params = get(request, 'query_params')
self.query_params = params.dict() if params else {}
self.include_from_source = self.query_params.get(MAPPING_LOOKUP_FROM_SOURCE) in ['true', True]
self.include_to_source = self.query_params.get(MAPPING_LOOKUP_TO_SOURCE) in ['true', True]
self.include_sources = self.query_params.get(MAPPING_LOOKUP_SOURCES) in ['true', True]
self.include_from_concept = self.query_params.get(MAPPING_LOOKUP_FROM_CONCEPT) in ['true', True]
self.include_to_concept = self.query_params.get(MAPPING_LOOKUP_TO_CONCEPT) in ['true', True]
self.include_concepts = self.query_params.get(MAPPING_LOOKUP_CONCEPTS) in ['true', True]
self.include_extras = self.query_params.get(INCLUDE_EXTRAS_PARAM) in ['true', True]
self.include_verbose_references = self.query_params.get(INCLUDE_VERBOSE_REFERENCES) in ['true', True]
if not self.include_concepts:
if not self.include_from_concept:
self.fields.pop('from_concept')
if not self.include_to_concept:
self.fields.pop('to_concept')
if not self.include_sources:
if not self.include_from_source:
self.fields.pop('from_source')
if not self.include_to_source:
self.fields.pop('to_source')
if not self.include_extras and self.__class__.__name__ in [
'MappingListSerializer', 'MappingVersionListSerializer'
]:
self.fields.pop('extras', None)
if not get(request, 'instance'):
self.fields.pop('references', None)
super().__init__(*args, **kwargs)
def get_references(self, obj):
collection = get(self, 'context.request.instance')
if collection:
if self.include_verbose_references:
from core.collections.serializers import CollectionReferenceSerializer
return CollectionReferenceSerializer(obj.collection_references(collection), many=True).data
return obj.collection_references_uris(collection)
return None
class MappingVersionListSerializer(MappingListSerializer):
previous_version_url = CharField(read_only=True, source='prev_version_uri')
source_versions = ListField(read_only=True)
collection_versions = ListField(read_only=True)
class Meta:
model = Mapping
fields = MappingListSerializer.Meta.fields + (
'previous_version_url', 'source_versions', 'collection_versions',
)
def __init__(self, *args, **kwargs):
params = get(kwargs, 'context.request.query_params')
self.query_params = params.dict() if params else {}
self.include_source_versions = self.query_params.get(INCLUDE_SOURCE_VERSIONS) in ['true', True]
self.include_collection_versions = self.query_params.get(INCLUDE_COLLECTION_VERSIONS) in ['true', True]
try:
if not self.include_source_versions:
self.fields.pop('source_versions', None)
if not self.include_collection_versions:
self.fields.pop('collection_versions', None)
except: # pylint: disable=bare-except
pass
super().__init__(*args, **kwargs)
class MappingMinimalSerializer(ModelSerializer):
uuid = CharField(source='id', read_only=True)
id = CharField(source='mnemonic', read_only=True)
type = CharField(source='resource_type', read_only=True)
url = CharField(source='uri', read_only=True)
to_concept_code = EncodedDecodedCharField()
target_concept_code = EncodedDecodedCharField(source='to_concept_code')
target_concept_name = SerializerMethodField()
target_concept_url = CharField(source='to_concept_url')
target_source_name = CharField(source='to_source_name', allow_blank=True, allow_null=True)
target_source_owner = CharField(source='to_source_owner', allow_blank=True, allow_null=True)
class Meta:
model = Mapping
fields = (
'uuid', 'id', 'type', 'map_type', 'url', 'version_url', 'to_concept_code', 'to_concept_url',
'target_concept_code', 'target_concept_url', 'target_source_owner', 'target_source_name',
'target_concept_name', 'retired'
)
@staticmethod
def get_target_concept_name(obj):
name = obj.to_concept_name
if not name and obj.parent_id != get(obj, 'to_concept.parent_id'):
# only returns for source different than self
name = get(obj, 'to_concept.display_name')
return name
class MappingReverseMinimalSerializer(ModelSerializer):
uuid = CharField(source='id', read_only=True)
id = CharField(source='mnemonic', read_only=True)
type = CharField(source='resource_type', read_only=True)
url = CharField(source='uri', read_only=True)
from_concept_code = EncodedDecodedCharField()
target_concept_code = EncodedDecodedCharField(source='from_concept_code')
target_concept_name = SerializerMethodField()
target_concept_url = CharField(source='from_concept_url')
target_source_name = CharField(source='from_source_name', allow_blank=True, allow_null=True)
target_source_owner = CharField(source='from_source_owner', allow_blank=True, allow_null=True)
class Meta:
model = Mapping
fields = (
'uuid', 'id', 'type', 'map_type', 'url', 'version_url', 'from_concept_code', 'from_concept_url',
'target_concept_code', 'target_concept_url', 'target_source_owner', 'target_source_name',
'target_concept_name', 'retired'
)
@staticmethod
def get_target_concept_name(obj):
name = obj.from_concept_name
if not name and obj.parent_id != get(obj, 'from_concept.parent_id'):
# only returns for source different than self
name = get(obj, 'from_concept.display_name')
return name
class MappingDetailSerializer(MappingListSerializer):
type = CharField(source='resource_type', read_only=True)
uuid = CharField(source='id', read_only=True)
extras = JSONField(required=False, allow_null=True)
created_by = CharField(source='created_by.username', read_only=True)
updated_by = CharField(source='created_by.username', read_only=True)
parent_id = IntegerField(required=True, write_only=True)
map_type = CharField(required=True)
to_concept_url = CharField(required=False)
from_concept_url = CharField(required=False)
from_concept = ConceptDetailSerializer()
to_concept = ConceptDetailSerializer()
from_source = SourceDetailSerializer()
to_source = SourceDetailSerializer()
created_on = DateTimeField(source='created_at', read_only=True)
updated_on = DateTimeField(source='updated_at', read_only=True)
class Meta:
model = Mapping
fields = MappingListSerializer.Meta.fields + (
'type', 'uuid', 'extras', 'created_on', 'updated_on', 'created_by',
'updated_by', 'parent_id', 'public_can_view',)
def create(self, validated_data):
mapping = Mapping.persist_new(data=validated_data, user=self.context.get('request').user)
if mapping.errors:
self._errors.update(mapping.errors)
return mapping
def update(self, instance, validated_data):
errors = Mapping.create_new_version_for(instance, validated_data, self.context.get('request').user)
if errors:
self._errors.update(errors)
return instance
class MappingVersionDetailSerializer(MappingDetailSerializer):
previous_version_url = CharField(read_only=True, source='prev_version_uri')
source_versions = ListField(read_only=True)
collection_versions = ListField(read_only=True)
class Meta:
model = Mapping
fields = MappingDetailSerializer.Meta.fields + (
'previous_version_url', 'source_versions', 'collection_versions',
)