-
Notifications
You must be signed in to change notification settings - Fork 30
/
models.py
325 lines (269 loc) · 13.6 KB
/
models.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
import uuid
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models import UniqueConstraint, F
from pydash import compact
from core.common.models import ConceptContainerModel
from core.common.services import PostgresQL
from core.common.validators import validate_non_negative
from core.concepts.models import LocalizedText
from core.sources.constants import SOURCE_TYPE, SOURCE_VERSION_TYPE, HIERARCHY_ROOT_MUST_BELONG_TO_SAME_SOURCE, \
HIERARCHY_MEANINGS, AUTO_ID_CHOICES, AUTO_ID_SEQUENTIAL, AUTO_ID_UUID
class Source(ConceptContainerModel):
DEFAULT_AUTO_ID_START_FROM = 1
es_fields = {
'source_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': False, '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, 'facet': True},
'canonical_url': {'sortable': True, 'filterable': True},
'experimental': {'sortable': False, 'filterable': False, 'facet': False},
'hierarchy_meaning': {'sortable': False, 'filterable': True, 'facet': True},
'external_id': {'sortable': False, 'filterable': True, 'facet': False, 'exact': False},
}
class Meta:
db_table = 'sources'
constraints = [
UniqueConstraint(
fields=['mnemonic', 'version', 'organization'],
name="org_source_unique",
condition=models.Q(user=None),
),
UniqueConstraint(
fields=['mnemonic', 'version', 'user'],
name="user_source_unique",
condition=models.Q(organization=None),
)
]
indexes = [] + ConceptContainerModel.Meta.indexes
# + index on UPPER(mnemonic) in custom migration 0022
source_type = models.TextField(blank=True, null=True)
content_type = models.TextField(blank=True, null=True)
collection_reference = models.CharField(null=True, blank=True, max_length=100)
hierarchy_meaning = models.CharField(null=True, blank=True, max_length=50, choices=HIERARCHY_MEANINGS)
case_sensitive = models.BooleanField(null=True, blank=True, default=None)
compositional = models.BooleanField(null=True, blank=True, default=None)
version_needed = models.BooleanField(null=True, blank=True, default=None)
hierarchy_root = models.ForeignKey('concepts.Concept', null=True, blank=True, on_delete=models.SET_NULL)
# auto-id
autoid_concept_mnemonic = models.CharField(null=True, blank=True, choices=AUTO_ID_CHOICES, max_length=10)
autoid_concept_external_id = models.CharField(null=True, blank=True, choices=AUTO_ID_CHOICES, max_length=10)
autoid_mapping_mnemonic = models.CharField(
null=True, blank=True, choices=AUTO_ID_CHOICES, max_length=10, default=AUTO_ID_SEQUENTIAL)
autoid_mapping_external_id = models.CharField(null=True, blank=True, choices=AUTO_ID_CHOICES, max_length=10)
autoid_concept_mnemonic_start_from = models.IntegerField(default=1, validators=[validate_non_negative])
autoid_concept_external_id_start_from = models.IntegerField(default=1, validators=[validate_non_negative])
autoid_mapping_mnemonic_start_from = models.IntegerField(default=1, validators=[validate_non_negative])
autoid_mapping_external_id_start_from = models.IntegerField(default=1, validators=[validate_non_negative])
OBJECT_TYPE = SOURCE_TYPE
OBJECT_VERSION_TYPE = SOURCE_VERSION_TYPE
@property
def is_sequential_concept_mnemonic(self):
return self.autoid_concept_mnemonic == AUTO_ID_SEQUENTIAL
@property
def is_sequential_concept_external_id(self):
return self.autoid_concept_external_id == AUTO_ID_SEQUENTIAL
@property
def is_sequential_mapping_mnemonic(self):
return self.autoid_mapping_mnemonic == AUTO_ID_SEQUENTIAL
@property
def is_sequential_mapping_external_id(self):
return self.autoid_mapping_external_id == AUTO_ID_SEQUENTIAL
@property
def concept_mnemonic_next(self):
return self.get_resource_next_attr_id(self.autoid_concept_mnemonic, self.concepts_mnemonic_seq_name)
@property
def concept_external_id_next(self):
return self.get_resource_next_attr_id(self.autoid_concept_external_id, self.concepts_external_id_seq_name)
@property
def mapping_mnemonic_next(self):
return self.get_resource_next_attr_id(self.autoid_mapping_mnemonic, self.mappings_mnemonic_seq_name)
@property
def mapping_external_id_next(self):
return self.get_resource_next_attr_id(self.autoid_mapping_external_id, self.mappings_external_id_seq_name)
@staticmethod
def get_resource_next_attr_id(attr_type, seq):
if attr_type == AUTO_ID_UUID:
return uuid.uuid4()
if attr_type == AUTO_ID_SEQUENTIAL:
return str(PostgresQL.next_value(seq))
return None
@staticmethod
def get_search_document():
from core.sources.documents import SourceDocument
return SourceDocument
@classmethod
def get_base_queryset(cls, params):
source = params.pop('source', None)
queryset = super().get_base_queryset(params)
if source:
queryset = queryset.filter(cls.get_exact_or_criteria('mnemonic', source))
return queryset
@staticmethod
def get_resource_url_kwarg():
return 'source'
@property
def source(self):
return self.mnemonic
def update_version_data(self, head):
super().update_version_data(head)
self.source_type = head.source_type
self.content_type = head.content_type
self.collection_reference = head.collection_reference
self.hierarchy_meaning = head.hierarchy_meaning
self.hierarchy_root_id = head.hierarchy_root_id
self.case_sensitive = head.case_sensitive
self.compositional = head.compositional
self.version_needed = head.version_needed
def get_concept_name_locales(self):
return LocalizedText.objects.filter(name_locales__in=self.get_active_concepts())
def is_validation_necessary(self):
origin_source = self.get_latest_version()
if origin_source.custom_validation_schema == self.custom_validation_schema:
return False
return self.custom_validation_schema is not None and self.active_concepts
def update_mappings(self):
from core.mappings.models import Mapping
uris = compact([self.uri, self.canonical_url])
for mapping in Mapping.objects.filter(to_source__isnull=True, to_source_url__in=uris):
mapping.to_source = self
mapping.save()
for mapping in Mapping.objects.filter(from_source__isnull=True, from_source_url__in=uris):
mapping.from_source = self
mapping.save()
def is_hierarchy_root_belonging_to_self(self):
hierarchy_root = self.hierarchy_root
if self.is_head:
return hierarchy_root.parent_id == self.id
return hierarchy_root.parent_id == self.head.id
def clean(self):
super().clean()
if self.hierarchy_root_id and not self.is_hierarchy_root_belonging_to_self():
raise ValidationError({'hierarchy_root': [HIERARCHY_ROOT_MUST_BELONG_TO_SAME_SOURCE]})
def get_parentless_concepts(self):
return self.concepts.filter(parent_concepts__isnull=True, id=F('versioned_object_id'))
def hierarchy(self, offset=0, limit=100):
from core.concepts.serializers import ConceptHierarchySerializer
hierarchy_root = None
if offset == 0:
hierarchy_root = self.hierarchy_root
parent_less_children = self.get_parentless_concepts()
if hierarchy_root:
parent_less_children = parent_less_children.exclude(mnemonic=hierarchy_root.mnemonic)
total_count = parent_less_children.count()
adjusted_limit = limit
if hierarchy_root:
adjusted_limit -= 1
parent_less_children = parent_less_children.order_by('mnemonic')[offset:adjusted_limit+offset]
children = []
if parent_less_children.exists():
children = ConceptHierarchySerializer(parent_less_children, many=True).data
if hierarchy_root:
children.append({**ConceptHierarchySerializer(hierarchy_root).data, 'root': True})
return dict(
id=self.mnemonic,
children=children,
count=total_count + (1 if hierarchy_root else 0),
offset=offset,
limit=limit
)
def set_active_concepts(self):
queryset = self.concepts.filter(retired=False, is_active=True)
if self.is_head:
queryset = queryset.filter(id=F('versioned_object_id'))
self.active_concepts = queryset.count()
def set_active_mappings(self):
queryset = self.mappings.filter(retired=False, is_active=True)
if self.is_head:
queryset = queryset.filter(id=F('versioned_object_id'))
self.active_mappings = queryset.count()
def seed_concepts(self, index=True):
head = self.head
if head:
self.concepts.set(head.concepts.filter(is_latest_version=True))
if index:
from core.concepts.documents import ConceptDocument
self.batch_index(self.concepts, ConceptDocument)
def seed_mappings(self, index=True):
head = self.head
if head:
self.mappings.set(head.mappings.filter(is_latest_version=True))
if index:
from core.mappings.documents import MappingDocument
self.batch_index(self.mappings, MappingDocument)
def index_children(self):
from core.concepts.documents import ConceptDocument
from core.mappings.documents import MappingDocument
self.batch_index(self.concepts, ConceptDocument)
self.batch_index(self.mappings, MappingDocument)
def __get_resource_db_sequence_prefix(self):
return self.uri.replace('/', '_').replace('-', '_').replace('.', '_').replace('@', '_')
@property
def concepts_mnemonic_seq_name(self):
prefix = self.__get_resource_db_sequence_prefix()
return f"{prefix}_concepts_mnemonic_seq"
@property
def concepts_external_id_seq_name(self):
prefix = self.__get_resource_db_sequence_prefix()
return f"{prefix}_concepts_external_id_seq"
@property
def mappings_mnemonic_seq_name(self):
prefix = self.__get_resource_db_sequence_prefix()
return f"{prefix}_mappings_mnemonic_seq"
@property
def mappings_external_id_seq_name(self):
prefix = self.__get_resource_db_sequence_prefix()
return f"{prefix}_mappings_external_id_seq"
def save(
self, force_insert=False, force_update=False, using=None, update_fields=None
):
is_new = not self.id
super().save(force_insert=force_insert, force_update=force_update, using=using, update_fields=update_fields)
if self.id and self.is_head:
self.create_sequences() if is_new else self.update_sequences() # pylint: disable=expression-not-assigned
def update_sequences(self):
def should_update(is_seq, start_from):
return is_seq and start_from and start_from != 1
if should_update(self.is_sequential_mapping_mnemonic, self.autoid_mapping_mnemonic_start_from):
PostgresQL.update_seq(
self.mappings_mnemonic_seq_name, self.autoid_mapping_mnemonic_start_from
)
if should_update(self.is_sequential_mapping_external_id, self.autoid_mapping_external_id_start_from):
PostgresQL.update_seq(
self.mappings_external_id_seq_name, self.autoid_mapping_external_id_start_from
)
if should_update(self.is_sequential_concept_mnemonic, self.autoid_concept_mnemonic_start_from):
PostgresQL.update_seq(
self.concepts_mnemonic_seq_name, self.autoid_concept_mnemonic_start_from
)
if should_update(self.is_sequential_concept_external_id, self.autoid_concept_external_id_start_from):
PostgresQL.update_seq(
self.concepts_external_id_seq_name, self.autoid_concept_external_id_start_from
)
def create_sequences(self):
if self.is_sequential_concept_mnemonic:
PostgresQL.create_seq(
self.concepts_mnemonic_seq_name, 'sources.uri', 0, self.autoid_concept_mnemonic_start_from
)
if self.is_sequential_mapping_mnemonic:
PostgresQL.create_seq(
self.mappings_mnemonic_seq_name, 'sources.uri', 0, self.autoid_mapping_mnemonic_start_from
)
if self.is_sequential_concept_external_id:
PostgresQL.create_seq(
self.concepts_external_id_seq_name, 'sources.uri', 0, self.autoid_concept_external_id_start_from
)
if self.is_sequential_mapping_external_id:
PostgresQL.create_seq(
self.mappings_external_id_seq_name, 'sources.uri', 0, self.autoid_mapping_external_id_start_from
)
def post_delete_actions(self):
if self.is_head:
PostgresQL.drop_seq(self.concepts_mnemonic_seq_name)
PostgresQL.drop_seq(self.concepts_external_id_seq_name)
PostgresQL.drop_seq(self.mappings_mnemonic_seq_name)
PostgresQL.drop_seq(self.mappings_external_id_seq_name)