-
Notifications
You must be signed in to change notification settings - Fork 31
/
serializers.py
119 lines (101 loc) · 5.66 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
from pydash import get
from rest_framework.fields import CharField, JSONField, IntegerField, DateTimeField
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
from core.concepts.serializers import ConceptListSerializer, ConceptDetailSerializer
from core.mappings.models import Mapping
from core.sources.serializers import SourceListSerializer, SourceDetailSerializer
class MappingListSerializer(ModelSerializer):
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)
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'
)
def __init__(self, *args, **kwargs):
params = get(kwargs, 'context.request.query_params')
self.query_params = params.dict() if params else dict()
self.include_from_source = False
self.include_to_source = False
self.include_sources = False
self.include_from_concept = False
self.include_to_concept = False
self.include_concepts = False
self.include_concepts = self.query_params.get(MAPPING_LOOKUP_CONCEPTS) 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_sources = self.query_params.get(MAPPING_LOOKUP_SOURCES) in ['true', True]
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]
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')
super().__init__(*args, **kwargs)
class MappingVersionListSerializer(MappingListSerializer):
previous_version_url = CharField(read_only=True, source='prev_version_uri')
class Meta:
model = Mapping
fields = MappingListSerializer.Meta.fields + ('previous_version_url',)
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)
map_type = CharField(required=True)
to_concept_url = CharField(required=False)
from_concept_url = CharField(required=False)
previous_version_url = CharField(read_only=True, source='prev_version_uri')
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', 'previous_version_url',
)
extra_kwargs = {'parent_id': {'write_only': True}}
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