-
Notifications
You must be signed in to change notification settings - Fork 30
/
models.py
1413 lines (1220 loc) · 64.5 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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from django.conf import settings
from django.contrib.postgres.indexes import HashIndex
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
from django.db import models, IntegrityError, transaction
from django.db.models import F, Q
from pydash import get, compact
from core.common.checksums import ChecksumModel
from core.common.constants import ISO_639_1, LATEST, HEAD, ALL
from core.common.mixins import SourceChildMixin
from core.common.models import VersionedModel, ConceptContainerModel
from core.common.services import PostgresQL
from core.common.tasks import process_hierarchy_for_new_concept, process_hierarchy_for_concept_version, \
process_hierarchy_for_new_parent_concept_version, update_mappings_concept
from core.common.utils import generate_temp_version, drop_version, \
encode_string, decode_string, startswith_temp_version, is_versioned_uri
from core.concepts.constants import CONCEPT_TYPE, LOCALES_FULLY_SPECIFIED, LOCALES_SHORT, LOCALES_SEARCH_INDEX_TERM, \
CONCEPT_WAS_RETIRED, CONCEPT_IS_ALREADY_RETIRED, CONCEPT_IS_ALREADY_NOT_RETIRED, CONCEPT_WAS_UNRETIRED, \
PERSIST_CLONE_ERROR, PERSIST_CLONE_SPECIFY_USER_ERROR, ALREADY_EXISTS, CONCEPT_REGEX, MAX_LOCALES_LIMIT, \
MAX_NAMES_LIMIT, MAX_DESCRIPTIONS_LIMIT
from core.concepts.mixins import ConceptValidationMixin
class AbstractLocalizedText(ChecksumModel):
class Meta:
abstract = True
id = models.BigAutoField(primary_key=True)
external_id = models.TextField(null=True, blank=True)
name = models.TextField()
type = models.TextField(null=True, blank=True)
locale = models.TextField()
locale_preferred = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
CHECKSUM_INCLUSIONS = ['locale', 'locale_preferred']
SMART_CHECKSUM_KEY = None
def to_dict(self):
return {
'external_id': self.external_id,
'name': self.name,
'type': self.type,
'locale': self.locale,
'locale_preferred': self.locale_preferred
}
def clone(self):
return self.__class__(
external_id=self.external_id,
name=self.name,
type=self.type,
locale=self.locale,
locale_preferred=self.locale_preferred
)
@staticmethod
def _build(_):
pass
@classmethod
def build(cls, params):
if not params:
return []
if isinstance(params, list):
return [cls._build(param) for param in params]
return cls._build(params)
@property
def is_fully_specified(self):
return self.type in LOCALES_FULLY_SPECIFIED or self.is_fully_specified_after_clean
@property
def is_fully_specified_after_clean(self): # needed for OpenMRS schema content created from TermBrowser
if not self.type:
return False
_type = self.type.replace(' ', '').replace('-', '').replace('_', '').lower()
return _type == 'fullyspecified'
@property
def is_short(self):
return self.type in LOCALES_SHORT
@property
def is_search_index_term(self):
return self.type in LOCALES_SEARCH_INDEX_TERM
class ConceptDescription(AbstractLocalizedText):
CHECKSUM_INCLUSIONS = AbstractLocalizedText.CHECKSUM_INCLUSIONS + ['description', 'description_type']
concept = models.ForeignKey('concepts.Concept', on_delete=models.CASCADE, related_name='descriptions')
class Meta:
db_table = 'concept_descriptions'
@property
def description_type(self):
return self.type
@property
def description(self):
return self.name
@staticmethod
def _build(params):
_type = params.get('type', None)
description_type = params.get('description_type', None)
if not description_type or description_type == 'ConceptDescription':
description_type = _type
description_name = params.get('description', None) or params.get('name', None)
return ConceptDescription(
**{
**{k: v for k, v in params.items() if k not in ['description', 'name', 'type', 'description_type']},
'type': description_type,
'name': description_name,
}
)
class ConceptName(AbstractLocalizedText):
CHECKSUM_INCLUSIONS = AbstractLocalizedText.CHECKSUM_INCLUSIONS + ['name', 'name_type']
concept = models.ForeignKey(
'concepts.Concept', on_delete=models.CASCADE, related_name='names')
class Meta:
db_table = 'concept_names'
indexes = [
HashIndex(fields=['name']),
models.Index(fields=['locale']),
models.Index(fields=['created_at']),
models.Index(fields=['type']),
]
@property
def name_type(self):
return self.type
@staticmethod
def _build(params):
_type = params.get('type', None)
name_type = params.get('name_type', None)
if not name_type or name_type == 'ConceptName':
name_type = _type
return ConceptName(
**{
**{k: v for k, v in params.items() if k not in ['type', 'name_type']},
'type': name_type
}
)
class HierarchicalConcepts(models.Model):
child = models.ForeignKey('concepts.Concept', related_name='child_parent', on_delete=models.CASCADE)
parent = models.ForeignKey('concepts.Concept', related_name='parent_child', on_delete=models.CASCADE)
class Concept(ConceptValidationMixin, SourceChildMixin, VersionedModel): # pylint: disable=too-many-public-methods
class Meta:
db_table = 'concepts'
unique_together = ('mnemonic', 'version', 'parent')
indexes = [
models.Index(name='concepts_up_pub_latest',
fields=['-updated_at', 'public_access', 'is_latest_version'],
condition=(Q(is_active=True) & Q(retired=False))),
models.Index(name='concepts_head_up_pub_latest', fields=['-updated_at', 'public_access',
'is_latest_version'],
condition=(Q(is_active=True) & Q(retired=False) & Q(id=F('versioned_object_id')))),
models.Index(name='concepts_pub_latest', fields=['public_access', 'is_latest_version'],
condition=(Q(is_active=True) & Q(retired=False))),
models.Index(name='concepts_head_pub_latest', fields=['public_access', 'is_latest_version'],
condition=(Q(is_active=True) & Q(retired=False) & Q(id=F('versioned_object_id')))),
models.Index(name='concepts_latest_parent_pub',
fields=['is_latest_version', 'parent_id', 'public_access'],
condition=(Q(is_active=True) & Q(retired=False))),
models.Index(name='concepts_up_latest', fields=['-updated_at', 'is_latest_version'],
condition=(Q(is_active=True) & Q(retired=False))),
models.Index(name='concepts_head_parent_pub', fields=['parent_id', 'public_access'],
condition=(Q(is_active=True) & Q(retired=False) & Q(id=F('versioned_object_id')))),
models.Index(name='concepts_parent_active', fields=['parent_id', 'id', 'retired'],
condition=(Q(retired=False, id=F('versioned_object_id')))),
models.Index(fields=['uri']),
models.Index(fields=['version']),
] + VersionedModel.Meta.indexes
external_id = models.TextField(null=True, blank=True)
concept_class = models.TextField()
datatype = models.TextField()
comment = models.TextField(null=True, blank=True)
parent = models.ForeignKey('sources.Source', related_name='concepts_set', on_delete=models.CASCADE)
sources = models.ManyToManyField('sources.Source', related_name='concepts')
versioned_object = models.ForeignKey(
'self', related_name='versions_set', null=True, blank=True, on_delete=models.CASCADE
)
parent_concepts = models.ManyToManyField(
'self', through='HierarchicalConcepts', symmetrical=False, related_name='child_concepts'
)
mnemonic = models.CharField(
max_length=255, validators=[RegexValidator(regex=CONCEPT_REGEX)],
)
_counted = models.BooleanField(default=True, null=True, blank=True)
_index = models.BooleanField(default=True)
logo_path = None
name = None
full_name = None
default_locale = None
supported_locales = None
website = None
description = None
OBJECT_TYPE = CONCEPT_TYPE
ALREADY_RETIRED = CONCEPT_IS_ALREADY_RETIRED
ALREADY_NOT_RETIRED = CONCEPT_IS_ALREADY_NOT_RETIRED
WAS_RETIRED = CONCEPT_WAS_RETIRED
WAS_UNRETIRED = CONCEPT_WAS_UNRETIRED
CHECKSUM_INCLUSIONS = [
'extras', 'concept_class', 'datatype', 'retired'
]
# $cascade as hierarchy attributes
cascaded_entries = None
terminal = None
es_fields = {
'id': {'sortable': False, 'filterable': True, 'exact': True},
'id_lowercase': {'sortable': True, 'filterable': False, 'exact': False},
'numeric_id': {'sortable': True, 'filterable': False, 'exact': False},
'name': {'sortable': False, 'filterable': True, 'exact': True},
'_name': {'sortable': True, 'filterable': False, 'exact': False},
'last_update': {'sortable': True, 'filterable': False, 'default': 'desc'},
'updated_by': {'sortable': False, 'filterable': False, 'facet': True},
'is_latest_version': {'sortable': False, 'filterable': True},
'concept_class': {'sortable': True, 'filterable': True, 'facet': True, 'exact': False},
'datatype': {'sortable': True, 'filterable': True, 'facet': True, 'exact': False},
'locale': {'sortable': False, 'filterable': True, 'facet': True, 'exact': False},
'synonyms': {'sortable': False, 'filterable': True, 'facet': False, 'exact': True},
'retired': {'sortable': False, 'filterable': True, 'facet': True},
'source': {'sortable': True, 'filterable': True, 'facet': True, 'exact': False},
'collection': {'sortable': False, 'filterable': True, 'facet': True},
'collection_url': {'sortable': False, 'filterable': True, 'facet': True},
'collection_owner_url': {'sortable': False, 'filterable': False, 'facet': True},
'owner': {'sortable': True, 'filterable': True, 'facet': True, 'exact': False},
'owner_type': {'sortable': False, 'filterable': True, 'facet': True, 'exact': False},
'external_id': {'sortable': False, 'filterable': True, 'facet': False, 'exact': True},
'name_types': {'sortable': False, 'filterable': True, 'facet': True},
'description_types': {'sortable': False, 'filterable': True, 'facet': True},
'same_as_map_codes': {'sortable': False, 'filterable': True, 'facet': False, 'exact': True},
'other_map_codes': {'sortable': False, 'filterable': True, 'facet': False, 'exact': True},
}
def get_standard_checksum_fields(self):
return {
'concept_class': self.concept_class,
'datatype': self.datatype,
'names': [name.get_checksum_fields() for name in self.names.filter()],
'extras': self.extras,
}
def get_smart_checksum_fields(self):
return {
'concept_class': self.concept_class,
'datatype': self.datatype,
'names': [name.get_checksum_fields() for name in self.names.filter() if name.is_fully_specified],
'retired': self.retired,
}
@staticmethod
def get_search_document():
from core.concepts.documents import ConceptDocument
return ConceptDocument
@property
def concept(self): # for url kwargs
return self.mnemonic
@staticmethod
def get_resource_url_kwarg():
return 'concept'
@staticmethod
def get_version_url_kwarg():
return 'concept_version'
@property
def display_name(self):
return get(self.preferred_locale, 'name')
@property
def display_locale(self):
return get(self.preferred_locale, 'locale')
@property
def preferred_locale(self):
try:
return self.__get_parent_default_locale_name() or self.__get_parent_supported_locale_name() or \
self.__get_system_default_locale() or self.__get_preferred_locale() or \
self.__get_last_created_locale()
except: # pylint: disable=bare-except
pass
return None
def __get_system_default_locale(self):
system_default_locale = settings.DEFAULT_LOCALE
return get(
self.__names_qs({'locale': system_default_locale, 'locale_preferred': True}, 'created_at', 'desc'), '0'
) or get(
self.__names_qs({'locale': system_default_locale}, 'created_at', 'desc'), '0'
)
def __get_parent_default_locale_name(self):
parent_default_locale = self.parent.default_locale
return get(
self.__names_qs({'locale': parent_default_locale, 'locale_preferred': True}, 'created_at', 'desc'), '0'
) or get(
self.__names_qs({'locale': parent_default_locale}, 'created_at', 'desc'), '0'
)
def __get_parent_supported_locale_name(self):
parent_supported_locales = self.parent.supported_locales
return get(
self.__names_qs(
{'locale__in': parent_supported_locales, 'locale_preferred': True}, 'created_at', 'desc'
), '0'
) or get(
self.__names_qs({'locale__in': parent_supported_locales}, 'created_at', 'desc'), '0'
)
def __get_last_created_locale(self):
return get(self.__names_qs({}, 'created_at', 'desc'), '0')
def __get_preferred_locale(self):
return get(self.__names_qs({'locale_preferred': True}, 'created_at', 'desc'), '0')
def __names_qs(self, filters, order_by=None, order='desc'):
if getattr(self, '_prefetched_objects_cache', None) and \
'names' in self._prefetched_objects_cache: # pragma: no cover
return self.__names_from_prefetched_object_cache(filters, order_by, order)
return self.__names_from_db(filters, order_by, order)
def __names_from_db(self, filters, order_by=None, order='desc'):
names = self.names.filter(
**filters
)
if order_by:
if order:
order_by = '-' + order_by if order.lower() == 'desc' else order_by
names = names.order_by(order_by)
return names
def __names_from_prefetched_object_cache(self, filters, order_by=None, order='desc'): # pragma: no cover
def is_eligible(name):
return all(get(name, key) == value for key, value in filters.items())
names = list(filter(is_eligible, self.names.all()))
if order_by:
names = sorted(names, key=lambda name: get(name, order_by), reverse=order.lower() == 'desc')
return names
@property
def default_name_locales(self):
return self.get_default_locales(self.names)
@property
def default_description_locales(self):
return self.get_default_locales(self.descriptions)
@staticmethod
def get_default_locales(locales):
return locales.filter(locale=settings.DEFAULT_LOCALE)
@property
def names_for_default_locale(self):
return list(self.default_name_locales.values_list('name', flat=True))
@property
def descriptions_for_default_locale(self):
return list(self.default_description_locales.values_list('name', flat=True))
@property
def iso_639_1_locale(self):
return get(self.__names_qs({'type': ISO_639_1}), '0.name')
@property
def custom_validation_schema(self):
return get(self, 'parent.custom_validation_schema')
@property
def all_names(self):
return list(self.names.values_list('name', flat=True))
@property
def saved_unsaved_descriptions(self):
unsaved_descriptions = get(self, 'cloned_descriptions', [])
if self.id:
return compact([*list(self.descriptions.all()), *unsaved_descriptions])
return unsaved_descriptions
@property
def saved_unsaved_names(self):
unsaved_names = get(self, 'cloned_names', [])
if self.id:
return compact([*list(self.names.all()), *unsaved_names])
return unsaved_names
@classmethod
def get_base_queryset(cls, params): # pylint: disable=too-many-branches,too-many-locals,too-many-statements
queryset = cls.objects.filter(is_active=True)
user = params.get('user', None)
org = params.get('org', None)
collection = params.get('collection', None)
source = params.get('source', None)
container_version = params.get('version', None)
concept = params.get('concept', None)
concept_version = params.get('concept_version', None)
latest_released_version = None
is_latest_released = container_version == LATEST
if is_latest_released:
filters = {'user__username': user, 'organization__mnemonic': org}
if source:
from core.sources.models import Source
latest_released_version = Source.find_latest_released_version_by(
{**filters, 'mnemonic': source})
elif collection:
from core.collections.models import Collection
latest_released_version = Collection.find_latest_released_version_by(
{**filters, 'mnemonic': collection})
if not latest_released_version:
return cls.objects.none()
if collection:
queryset = queryset.filter(
cls.get_filter_by_container_criterion(
'expansion_set__collection_version', collection, org, user, container_version,
is_latest_released, latest_released_version,
)
)
if source:
queryset = queryset.filter(
cls.get_filter_by_container_criterion(
'sources', source, org, user, container_version,
is_latest_released, latest_released_version, 'parent'
)
)
if concept:
queryset = queryset.filter(mnemonic__in=cls.get_mnemonic_variations_for_filter(concept))
if concept_version:
queryset = queryset.filter(version=concept_version)
return cls.apply_attribute_based_filters(queryset, params)
@staticmethod
def get_mnemonic_variations_for_filter(mnemonic):
return [
mnemonic, encode_string(mnemonic, safe=' '), encode_string(mnemonic, safe='+'),
encode_string(mnemonic, safe='+%'), encode_string(mnemonic, safe='% +'),
decode_string(mnemonic), decode_string(mnemonic, False)
]
def clone(self):
concept_version = Concept(
mnemonic=self.mnemonic,
version=generate_temp_version(),
public_access=self.public_access,
external_id=self.external_id,
concept_class=self.concept_class,
datatype=self.datatype,
retired=self.retired,
released=self.released,
extras=self.extras or {},
parent=self.parent,
is_latest_version=self.is_latest_version,
parent_id=self.parent_id,
versioned_object_id=self.versioned_object_id,
_index=self._index
)
concept_version.cloned_names = self.__clone_name_locales()
concept_version.cloned_descriptions = self.__clone_description_locales()
concept_version._parent_concepts = self.parent_concepts.all() # pylint: disable=protected-access
return concept_version
@classmethod
def version_for_concept(cls, concept, version_label, parent_version=None):
version = concept.clone()
version.version = version_label
version.created_by_id = concept.created_by_id
version.updated_by_id = concept.updated_by_id
if parent_version:
version.parent = parent_version
version.released = False
return version
@classmethod
def create_initial_version(cls, concept, **kwargs):
initial_version = cls.version_for_concept(concept, generate_temp_version())
initial_version.comment = concept.comment
initial_version.save(**kwargs)
initial_version.version = initial_version.id
initial_version.released = True
initial_version.is_latest_version = True
initial_version.save()
return initial_version
@classmethod
def create_new_version_for(
cls, instance, data, user, create_parent_version=True, add_prev_version_children=True,
): # pylint: disable=too-many-arguments
instance.id = None # Clear id so it is persisted as a new object
instance.version = data.get('version', None)
instance.concept_class = data.get('concept_class', instance.concept_class)
instance.datatype = data.get('datatype', instance.datatype)
instance.extras = data.get('extras', instance.extras)
instance.external_id = data.get('external_id', instance.external_id)
instance.comment = data.get('update_comment') or data.get('comment')
instance.retired = data.get('retired', instance.retired)
new_names = ConceptName.build(data.get('names', []))
new_descriptions = ConceptDescription.build(data.get('descriptions', []))
has_parent_concept_uris_attr = 'parent_concept_urls' in data
parent_concept_uris = data.pop('parent_concept_urls', None)
instance.cloned_names = compact(new_names)
instance.cloned_descriptions = compact(new_descriptions)
if not parent_concept_uris and has_parent_concept_uris_attr:
parent_concept_uris = []
return cls.persist_clone(instance, user, create_parent_version, parent_concept_uris, add_prev_version_children)
def set_parent_concepts_from_uris(self, create_parent_version=True):
parent_concepts = get(self, '_parent_concepts', None)
if create_parent_version:
for parent in parent_concepts:
current_latest_version = parent.get_latest_version()
parent_clone = parent.clone()
Concept.create_new_version_for(
parent_clone,
{
'names': [name.to_dict() for name in parent.names.all()],
'descriptions': [desc.to_dict() for desc in parent.descriptions.all()],
'parent_concept_urls': parent.parent_concept_urls
},
self.created_by,
create_parent_version=False
)
new_latest_version = parent.get_latest_version()
for uri in current_latest_version.child_concept_urls:
Concept.objects.filter(
uri=uri
).first().get_latest_version().parent_concepts.add(new_latest_version)
if parent_concepts:
self.parent_concepts.set([parent.get_latest_version() for parent in parent_concepts])
self._parent_concepts = None
def create_new_versions_for_removed_parents(self, uris):
if uris:
concepts = Concept.objects.filter(uri__in=uris)
child_versioned_object_uri = drop_version(self.uri)
for concept in concepts:
current_latest_version = concept.get_latest_version()
Concept.create_new_version_for(
concept.clone(),
{
'names': [name.to_dict() for name in concept.names.all()],
'descriptions': [desc.to_dict() for desc in concept.descriptions.all()],
'parent_concept_urls': concept.parent_concept_urls
},
concept.created_by,
create_parent_version=False,
add_prev_version_children=False
)
new_latest_version = concept.get_latest_version()
for uri in current_latest_version.child_concept_urls:
if uri != child_versioned_object_uri:
child = Concept.objects.filter(uri=uri).first().get_latest_version()
child.parent_concepts.add(new_latest_version)
def set_locales(self, locales, locale_klass):
if not self.id:
return # pragma: no cover
for locale in locales:
new_locale = locale.clone() if isinstance(locale, locale_klass) else locale_klass.build(locale)
new_locale.concept_id = self.id
new_locale.save()
new_locale.set_checksums()
def remove_locales(self):
self.names.all().delete()
self.descriptions.all().delete()
def __clone_name_locales(self):
return self.__clone_locales(self.names)
def __clone_description_locales(self):
return self.__clone_locales(self.descriptions)
@staticmethod
def __clone_locales(locales):
return [locale.clone() for locale in locales.all()]
def is_existing_in_parent(self):
return self.parent.concepts_set.filter(mnemonic__exact=self.mnemonic).exists()
def save_cloned(self):
try:
names = self.cloned_names
descriptions = self.cloned_descriptions
parent = self.parent
self.name = self.mnemonic = self.version = generate_temp_version()
self.is_latest_version = False
self.public_access = parent.public_access
self.errors = {}
self.save()
if self.id:
next_valid_seq = parent.concept_mnemonic_next # returns str of int or None
if parent.is_sequential_concepts_mnemonic:
try:
available_next = int(parent.get_max_concept_mnemonic())
if available_next and available_next >= int(next_valid_seq):
PostgresQL.update_seq(parent.concepts_mnemonic_seq_name, available_next)
next_valid_seq = parent.concept_mnemonic_next
except: # pylint: disable=bare-except
pass
self.name = self.mnemonic = next_valid_seq or str(self.id)
self.external_id = parent.concept_external_id_next
self.versioned_object_id = self.id
self.version = str(self.id)
self.save()
self.full_clean()
self.set_locales(names, ConceptName)
self.set_locales(descriptions, ConceptDescription)
initial_version = Concept.create_initial_version(self)
initial_version.set_locales(names, ConceptName)
initial_version.set_locales(descriptions, ConceptDescription)
initial_version.sources.set([parent])
self.sources.set([parent])
self.set_checksums()
except ValidationError as ex:
if self.id:
self.delete()
self.errors.update(ex.message_dict)
except IntegrityError as ex:
if self.id:
self.delete()
self.errors.update({'__all__': ex.args})
@classmethod
def persist_new(cls, data, user=None, create_initial_version=True, create_parent_version=True): # pylint: disable=too-many-statements,too-many-branches
names = data.pop('names', []) or []
descriptions = data.pop('descriptions', []) or []
parent_concept_uris = data.pop('parent_concept_urls', None)
concept = Concept(**data)
temp_version = generate_temp_version()
concept.version = temp_version
if user:
concept.created_by = concept.updated_by = user
concept.errors = {}
concept.mnemonic = concept.mnemonic or concept.version
if concept.is_existing_in_parent():
concept.errors = {'__all__': [ALREADY_EXISTS]}
return concept
try:
concept.validate_locales_limit(names, descriptions)
concept.save()
concept.versioned_object_id = concept.id
concept.version = str(concept.id)
concept.set_locales(names, ConceptName)
concept.set_locales(descriptions, ConceptDescription)
parent = concept.parent
if startswith_temp_version(concept.mnemonic):
next_valid_seq = parent.concept_mnemonic_next # returns str of int or None
if parent.is_sequential_concepts_mnemonic:
try:
available_next = int(parent.get_max_concept_mnemonic())
if available_next and available_next >= int(next_valid_seq):
PostgresQL.update_seq(parent.concepts_mnemonic_seq_name, available_next)
next_valid_seq = parent.concept_mnemonic_next
except: # pylint: disable=bare-except
pass
concept.name = concept.mnemonic = next_valid_seq or str(concept.id)
if not concept.external_id:
concept.external_id = parent.concept_external_id_next
concept.is_latest_version = not create_initial_version
concept.public_access = parent.public_access
concept.save()
concept.full_clean()
initial_version = None
if create_initial_version:
initial_version = cls.create_initial_version(concept)
if initial_version.id:
initial_version.set_locales(names, ConceptName)
initial_version.set_locales(descriptions, ConceptDescription)
initial_version.sources.set([parent])
concept.sources.set([parent])
if get(settings, 'TEST_MODE', False):
update_mappings_concept(concept.id)
else:
update_mappings_concept.delay(concept.id)
if parent_concept_uris:
if get(settings, 'TEST_MODE', False):
process_hierarchy_for_new_concept(
concept.id, get(initial_version, 'id'), parent_concept_uris, create_parent_version)
else:
process_hierarchy_for_new_concept.apply_async(
(concept.id, get(initial_version, 'id'), parent_concept_uris, create_parent_version),
queue='concurrent'
)
if create_initial_version and concept._counted is True:
parent.update_concepts_count()
concept.set_checksums()
except ValidationError as ex:
if concept.id:
concept.delete()
concept.errors.update(ex.message_dict)
except IntegrityError as ex:
if concept.id:
concept.delete()
concept.errors.update({'__all__': ex.args})
return concept
def update_versioned_object(self):
concept = self.versioned_object
concept.extras = self.extras
concept.remove_locales()
concept.set_locales(self.names.all(), ConceptName)
concept.set_locales(self.descriptions.all(), ConceptDescription)
concept.parent_concepts.set(self.parent_concepts.all())
concept.concept_class = self.concept_class
concept.datatype = self.datatype
concept.retired = self.retired
concept.external_id = self.external_id or concept.external_id
concept.updated_by_id = self.updated_by_id
concept.save()
@classmethod
def persist_clone(
cls, obj, user=None, create_parent_version=True, parent_concept_uris=None, add_prev_version_children=True,
**kwargs
): # pylint: disable=too-many-statements,too-many-branches,too-many-arguments
errors = {}
if not user:
errors['version_created_by'] = PERSIST_CLONE_SPECIFY_USER_ERROR
return errors
obj.created_by = user
obj.updated_by = user
obj.version = obj.version or generate_temp_version()
parent = obj.parent
persisted = False
versioned_object = obj.versioned_object
prev_latest_version = versioned_object.versions.exclude(id=obj.id).filter(is_latest_version=True).first()
try:
with transaction.atomic():
cls.validate_locales_limit(obj.cloned_names, obj.cloned_descriptions)
cls.pause_indexing()
obj.is_latest_version = True
obj.save(**kwargs)
if obj.id:
obj.version = str(obj.id)
obj.save()
obj.set_locales(obj.cloned_names, ConceptName)
obj.set_locales(obj.cloned_descriptions, ConceptDescription)
obj.cloned_names = []
obj.cloned_descriptions = []
obj.clean() # clean here to validate locales that can only be saved after obj is saved
obj.update_versioned_object()
if prev_latest_version:
prev_latest_version._index = obj._index # pylint: disable=protected-access
prev_latest_version.is_latest_version = False
prev_latest_version.save(update_fields=['is_latest_version', '_index'])
prev_latest_version.sources.remove(parent)
if add_prev_version_children:
if get(settings, 'TEST_MODE', False):
process_hierarchy_for_new_parent_concept_version(prev_latest_version.id, obj.id)
else:
process_hierarchy_for_new_parent_concept_version.apply_async(
(prev_latest_version.id, obj.id),
queue='concurrent'
)
obj.sources.set([parent])
obj.set_checksums()
versioned_object.set_checksums()
persisted = True
cls.resume_indexing()
if get(settings, 'TEST_MODE', False):
process_hierarchy_for_concept_version(
obj.id, get(prev_latest_version, 'id'), parent_concept_uris, create_parent_version)
else:
process_hierarchy_for_concept_version.apply_async(
(obj.id, get(prev_latest_version, 'id'), parent_concept_uris, create_parent_version),
queue='concurrent'
)
def index_all():
if obj._index: # pylint: disable=protected-access
if prev_latest_version:
prev_latest_version.index()
obj.index()
transaction.on_commit(index_all)
except ValidationError as err:
if get(err, 'error_dict'):
errors.update(err.message_dict)
finally:
cls.resume_indexing()
if not persisted:
if prev_latest_version:
prev_latest_version._index = True # pylint: disable=protected-access
prev_latest_version.is_latest_version = True
prev_latest_version.save(update_fields=['is_latest_version', '_index'])
prev_latest_version.sources.add(parent)
if obj.id:
obj.remove_locales()
obj.delete()
errors['non_field_errors'] = [PERSIST_CLONE_ERROR]
return errors
@staticmethod
def validate_locales_limit(names, descriptions):
if len(names) > MAX_LOCALES_LIMIT:
raise ValidationError({'names': [MAX_NAMES_LIMIT]})
if len(descriptions) > MAX_LOCALES_LIMIT:
raise ValidationError({'descriptions': [MAX_DESCRIPTIONS_LIMIT]})
def get_unidirectional_mappings_for_collection(self, collection_url, collection_version=HEAD):
from core.mappings.models import Mapping
return Mapping.objects.filter(
from_concept__uri__icontains=drop_version(self.uri),
expansion_set__collection_version__uri__icontains=collection_url,
expansion_set__collection_version__version=collection_version
)
def get_indirect_mappings_for_collection(self, collection_url, collection_version=HEAD):
from core.mappings.models import Mapping
return Mapping.objects.filter(
to_concept__uri__icontains=drop_version(self.uri),
expansion_set__collection_version__uri__icontains=collection_url,
expansion_set__collection_version__version=collection_version
)
def get_unidirectional_mappings(self):
return self.__get_mappings_from_relation('mappings_from')
def get_indirect_mappings(self):
return self.__get_mappings_from_relation('mappings_to')
def __get_mappings_from_relation(self, relation_manager, is_latest=False):
key = 'from_concept_id__in' if relation_manager == 'mappings_from' else 'to_concept_id__in'
filters = {key: list(set(compact([self.id, self.versioned_object_id, get(self.get_latest_version(), 'id')])))}
from core.mappings.models import Mapping
mappings = Mapping.objects.filter(parent_id=self.parent_id, **filters)
if is_latest:
return mappings.filter(is_latest_version=True)
return mappings.filter(id=F('versioned_object_id'))
def get_bidirectional_mappings(self):
return self.get_unidirectional_mappings() | self.get_indirect_mappings()
def get_bidirectional_mappings_for_collection(self, collection_url, collection_version=HEAD):
queryset = self.get_unidirectional_mappings_for_collection(
collection_url, collection_version
) | self.get_indirect_mappings_for_collection(
collection_url, collection_version
)
return queryset.distinct()
@staticmethod
def get_latest_versions_for_queryset(concepts_qs):
"""Takes any concepts queryset and returns queryset of latest_version of each of those concepts"""
if concepts_qs is None or not concepts_qs.exists():
return Concept.objects.none()
criteria_fields = list(concepts_qs.values('parent_id', 'mnemonic'))
criterion = [models.Q(**attrs, is_latest_version=True) for attrs in criteria_fields]
query = criterion.pop()
for criteria in criterion:
query |= criteria
return Concept.objects.filter(query)
def update_mappings(self):
# Updates mappings where mapping.to_concept or mapping.from_concepts matches concept's mnemonic and parent
from core.mappings.models import Mapping
parent_uris = self.parent.identity_uris
Mapping.objects.filter(
to_concept_code=self.mnemonic, to_source_url__in=parent_uris, to_concept__isnull=True
).update(to_concept=self)
Mapping.objects.filter(
from_concept_code=self.mnemonic, from_source_url__in=parent_uris, from_concept__isnull=True
).update(from_concept=self)
@property
def parent_concept_urls(self):
return self.get_hierarchy_concept_urls('parent_concepts')
@property
def child_concept_urls(self):
return self.get_hierarchy_concept_urls('child_concepts')
def get_hierarchy_concept_urls(self, relation, versioned=False):
queryset = get(self, relation).all()
if self.is_latest_version:
queryset |= get(self.versioned_object, relation).all()
if self.is_versioned_object:
latest_version = self.get_latest_version()
if latest_version:
queryset |= get(latest_version, relation).all()
uris = queryset.values_list('uri', flat=True)
if versioned:
return self.__format_hierarchy_versioned_uris(uris)
return self.__format_hierarchy_uris(uris)
def get_hierarchy_queryset(self, relation, repo_version, filters=None):
filters = filters or {}
queryset = Concept.objects.filter(uri__in=self.get_hierarchy_concept_urls(relation, not repo_version.is_head))
return queryset.filter(**filters)
def child_concept_queryset(self):
urls = self.child_concept_urls
if urls:
return Concept.objects.filter(uri__in=urls)
return Concept.objects.none()
@property
def children_concepts_count(self):
return len(self.child_concept_urls)
@property
def has_children(self):
result = self.child_concepts.exists()
if not result and self.is_latest_version:
result = self.versioned_object.child_concepts.exists()
elif not result and self.is_versioned_object:
result = self.get_latest_version().child_concepts.exists()
return result
@property
def parent_concepts_count(self):
return len(self.parent_concept_urls)
def parent_concept_queryset(self):
urls = self.parent_concept_urls
if urls:
return Concept.objects.filter(uri__in=urls)
return Concept.objects.none()
@staticmethod
def __format_hierarchy_uris(uris):
return list({drop_version(uri) for uri in uris})
@staticmethod
def __format_hierarchy_versioned_uris(uris):
return list({uri for uri in uris if is_versioned_uri(uri)})
def get_hierarchy_path(self):
result = []
parent_concept = self.parent_concepts.first()
while parent_concept is not None:
result.append(drop_version(parent_concept.uri))
parent_concept = parent_concept.parent_concepts.first()
result.reverse()
return result
@staticmethod
def __get_omit_from_version(omit_if_exists_in):