-
Notifications
You must be signed in to change notification settings - Fork 28
/
models.py
726 lines (595 loc) · 27 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
from django.conf import settings
from django.core.exceptions import ValidationError
from django.db import models, transaction
from django.db.models import UniqueConstraint
from django.utils import timezone
from pydash import get
from core.collections.constants import (
COLLECTION_TYPE, CONCEPTS_EXPRESSIONS,
MAPPINGS_EXPRESSIONS,
REFERENCE_ALREADY_EXISTS, CONCEPT_FULLY_SPECIFIED_NAME_UNIQUE_PER_COLLECTION_AND_LOCALE,
CONCEPT_PREFERRED_NAME_UNIQUE_PER_COLLECTION_AND_LOCALE, ALL_SYMBOL, COLLECTION_VERSION_TYPE)
from core.collections.utils import is_concept, is_mapping
from core.common.constants import (
DEFAULT_REPOSITORY_TYPE, ACCESS_TYPE_VIEW, ACCESS_TYPE_EDIT,
ACCESS_TYPE_NONE, SEARCH_PARAM)
from core.common.models import ConceptContainerModel, BaseResourceModel
from core.common.tasks import seed_children_to_expansion, batch_index_resources, index_expansion_concepts, \
index_expansion_mappings
from core.common.utils import is_valid_uri, drop_version, to_owner_uri, generate_temp_version, api_get
from core.concepts.constants import LOCALES_FULLY_SPECIFIED
from core.concepts.models import Concept
from core.mappings.models import Mapping
class Collection(ConceptContainerModel):
OBJECT_TYPE = COLLECTION_TYPE
OBJECT_VERSION_TYPE = COLLECTION_VERSION_TYPE
es_fields = {
'collection_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},
'external_id': {'sortable': False, 'filterable': True, 'facet': False, 'exact': False},
}
class Meta:
db_table = 'collections'
constraints = [
UniqueConstraint(
fields=['mnemonic', 'version', 'organization'],
name="org_collection_unique",
condition=models.Q(user=None),
),
UniqueConstraint(
fields=['mnemonic', 'version', 'user'],
name="user_collection_unique",
condition=models.Q(organization=None),
)
]
indexes = [] + ConceptContainerModel.Meta.indexes
collection_type = models.TextField(blank=True)
preferred_source = models.TextField(blank=True)
repository_type = models.TextField(default=DEFAULT_REPOSITORY_TYPE, blank=True)
custom_resources_linked_source = models.TextField(blank=True)
concepts = models.ManyToManyField('concepts.Concept', blank=True, related_name='collection_set')
mappings = models.ManyToManyField('mappings.Mapping', blank=True, related_name='collection_set')
references = models.ManyToManyField('collections.CollectionReference', blank=True, related_name='collections')
immutable = models.BooleanField(null=True, blank=True, default=None)
locked_date = models.DateTimeField(null=True, blank=True)
autoexpand_head = models.BooleanField(default=True, null=True)
autoexpand = models.BooleanField(default=True, null=True)
expansion_uri = models.TextField(null=True, blank=True)
def set_active_concepts(self):
if self.expansion_uri:
self.active_concepts = self.expansion.concepts.filter(retired=False, is_active=True).count()
def set_active_mappings(self):
if self.expansion_uri:
self.active_mappings = self.expansion.mappings.filter(retired=False, is_active=True).count()
@staticmethod
def get_search_document():
from core.collections.documents import CollectionDocument
return CollectionDocument
@classmethod
def get_base_queryset(cls, params):
collection = params.pop('collection', None)
contains_uri = params.pop('contains', None)
include_references = params.pop('include_references', None) in [True, 'true']
queryset = super().get_base_queryset(params)
if collection:
queryset = queryset.filter(cls.get_exact_or_criteria('mnemonic', collection))
if contains_uri:
queryset = queryset.filter(
references__expression=contains_uri, public_access__in=[ACCESS_TYPE_EDIT, ACCESS_TYPE_VIEW]
)
if include_references:
queryset = queryset.prefetch_related('references')
return queryset
@property
def collection(self):
return self.mnemonic
@staticmethod
def get_resource_url_kwarg():
return 'collection'
def update_version_data(self, head):
super().update_version_data(head)
self.collection_type = head.collection_type
self.preferred_source = head.preferred_source
self.repository_type = head.repository_type
self.custom_resources_linked_source = head.custom_resources_linked_source
self.immutable = head.immutable
self.locked_date = head.locked_date
@property
def should_auto_expand(self):
if self.is_head:
return self.autoexpand_head
return self.autoexpand
def get_concepts(self, start=None, end=None):
""" Use for efficient iteration over paginated concepts. Note that any filter will be applied only to concepts
from the given range. If you need to filter on all concepts, use get_concepts() without args.
"""
concepts = Concept.objects.none()
if self.expansion_uri:
concepts = self.expansion.concepts.all()
if start and end:
concepts = concepts[start:end]
return concepts
def fill_data_from_reference(self, reference):
self.references.add(reference)
if self.should_auto_expand:
if reference.concepts:
self.expansion.concepts.add(*reference.concepts)
if reference.mappings:
self.expansion.mappings.add(*reference.mappings)
self.save() # update counts
def validate(self, reference):
if self.should_auto_expand:
reference.full_clean()
else:
reference.last_resolved_at = None
if reference.without_version in [reference.without_version for reference in self.references.all()]:
raise ValidationError({reference.expression: [REFERENCE_ALREADY_EXISTS]})
if self.is_openmrs_schema and self.expansion_uri:
if reference.concepts is None or reference.concepts.count() == 0:
return
concept = reference.concepts[0]
self.check_concept_uniqueness_in_collection_and_locale_by_name_attribute(
concept, attribute='type__in', value=LOCALES_FULLY_SPECIFIED,
error_message=CONCEPT_FULLY_SPECIFIED_NAME_UNIQUE_PER_COLLECTION_AND_LOCALE
)
self.check_concept_uniqueness_in_collection_and_locale_by_name_attribute(
concept, attribute='locale_preferred', value=True,
error_message=CONCEPT_PREFERRED_NAME_UNIQUE_PER_COLLECTION_AND_LOCALE
)
def check_concept_uniqueness_in_collection_and_locale_by_name_attribute(
self, concept, attribute, value, error_message
):
other_concepts_in_collection = self.expansion.concepts
if not other_concepts_in_collection.exists():
return
matching_names_in_concept = {}
names = concept.names.filter(**{attribute: value})
for name in names:
validation_error = dict(names=[error_message])
# making sure names in the submitted concept meet the same rule
name_key = name.locale + name.name
if name_key in matching_names_in_concept:
raise ValidationError(validation_error)
matching_names_in_concept[name_key] = True
if other_concepts_in_collection.filter(
names__name=name.name, names__locale=name.locale, **{f"names__{attribute}": value}
).exists():
raise ValidationError(validation_error)
@staticmethod
def get_source_from_uri(uri):
from core.sources.models import Source
return Source.objects.filter(uri=uri).first()
@transaction.atomic
def add_expressions(self, data, user, cascade_mappings=False, cascade_to_concepts=False):
expressions = data.get('expressions', [])
concept_expressions = data.get('concepts', [])
mapping_expressions = data.get('mappings', [])
source = None
source_uri = data.get('uri')
if source_uri:
source = self.get_source_from_uri(source_uri)
if source:
can_view_all_content = source.can_view_all_content(user)
def get_child_expressions(queryset):
if source.is_head:
queryset = queryset.filter(is_latest_version=True)
if not can_view_all_content:
queryset = queryset.filter(public_access=ACCESS_TYPE_NONE)
return list(queryset.values_list('uri', flat=True))
if concept_expressions == ALL_SYMBOL:
expressions.extend(get_child_expressions(source.concepts))
if mapping_expressions == ALL_SYMBOL:
expressions.extend(get_child_expressions(source.mappings))
else:
expressions.extend(concept_expressions)
expressions.extend(mapping_expressions)
if cascade_mappings or cascade_to_concepts:
expressions += self.get_all_related_uris(expressions, cascade_to_concepts)
return self.add_references(expressions, user)
def add_references(self, expressions, user=None): # pylint: disable=too-many-locals,too-many-branches,too-many-statements # Fixme: Sny
errors = {}
collection_version = self.head
new_expressions = set(expressions)
new_versionless_expressions = {drop_version(expression): expression for expression in new_expressions}
for reference in collection_version.references.all():
existing_versionless_expression = reference.without_version
if existing_versionless_expression in new_versionless_expressions:
existing_expression = new_versionless_expressions[existing_versionless_expression]
new_expressions.discard(existing_expression)
errors[existing_expression] = [REFERENCE_ALREADY_EXISTS]
added_references = []
for expression in new_expressions:
ref = CollectionReference(expression=expression)
try:
if self.autoexpand_head:
ref.clean()
else:
ref.last_resolved_at = None
ref.save()
except Exception as ex:
errors[expression] = ex.messages if hasattr(ex, 'messages') else ex
continue
added = False
if ref.concepts:
if self.is_openmrs_schema:
for concept in ref.concepts:
try:
self.check_concept_uniqueness_in_collection_and_locale_by_name_attribute(
concept, attribute='type__in', value=LOCALES_FULLY_SPECIFIED,
error_message=CONCEPT_FULLY_SPECIFIED_NAME_UNIQUE_PER_COLLECTION_AND_LOCALE
)
self.check_concept_uniqueness_in_collection_and_locale_by_name_attribute(
concept, attribute='locale_preferred', value=True,
error_message=CONCEPT_PREFERRED_NAME_UNIQUE_PER_COLLECTION_AND_LOCALE
)
except Exception as ex:
errors[expression] = ex.messages if hasattr(ex, 'messages') else ex
continue
collection_version.expansion.concepts.add(concept)
concept.index()
added = True
else:
collection_version.expansion.add_references(ref)
added = True
if ref.mappings:
collection_version.expansion.add_references(ref)
added = True
if not added and ref.id:
added = True
if added:
collection_version.references.add(ref)
self.references.add(ref)
added_references.append(ref)
if user and user.is_authenticated:
collection_version.updated_by = user
self.updated_by = user
collection_version.save()
collection_version.update_children_counts()
if collection_version.id != self.id:
self.save()
return added_references, errors
@classmethod
def persist_changes(cls, obj, updated_by, original_schema, **kwargs):
col_reference = kwargs.pop('col_reference', False)
errors = super().persist_changes(obj, updated_by, original_schema, **kwargs)
if col_reference and not errors:
obj.fill_data_from_reference(col_reference)
return errors
def seed_references(self):
head = self.head
if head:
references = CollectionReference.objects.bulk_create(
[CollectionReference(expression=ref.expression) for ref in head.references.all()]
)
self.references.set(references)
@staticmethod
def is_validation_necessary():
return False
@property
def expansions_count(self):
return self.expansions.count()
def delete_references(self, expressions):
if self.expansion_uri:
self.expansion.delete_expressions(expressions)
self.references.set(self.references.exclude(expression__in=expressions))
@staticmethod
def __get_children_from_expressions(expressions):
concepts = Concept.objects.filter(uri__in=expressions)
mappings = Mapping.objects.filter(uri__in=expressions)
return concepts, mappings
def get_all_related_uris(self, expressions, cascade_to_concepts=False):
all_related_mappings = []
unversioned_mappings = []
concept_expressions = []
for expression in expressions:
if is_mapping(expression):
unversioned_mappings.append(drop_version(expression))
elif is_concept(expression):
concept_expressions.append(expression)
for concept_expression in concept_expressions:
ref = CollectionReference(expression=concept_expression)
try:
self.validate(ref)
all_related_mappings += ref.get_related_uris(unversioned_mappings, cascade_to_concepts)
except: # pylint: disable=bare-except
continue
return all_related_mappings
def get_cascaded_mapping_uris_from_concept_expressions(self, expressions):
mapping_uris = []
if not self.expansion_uri:
return mapping_uris
for expression in expressions:
if is_concept(expression):
mapping_uris += list(
self.expansion.mappings.filter(
from_concept__uri__icontains=drop_version(expression)).values_list('uri', flat=True)
)
return mapping_uris
def cascade_children_to_expansion(self, expansion_data=None, index=True): # pylint: disable=arguments-differ
if not expansion_data:
expansion_data = {}
should_auto_expand = self.should_auto_expand
if should_auto_expand and not self.expansions.exists():
expansion_data['mnemonic'] = f'autoexpand-{self.version}'
expansion = Expansion.persist(index=index, **expansion_data, collection_version=self)
if should_auto_expand and not self.expansion_uri:
self.expansion_uri = expansion.uri
self.save()
return expansion
def index_children(self):
if self.expansion_uri:
from core.concepts.documents import ConceptDocument
from core.mappings.documents import MappingDocument
self.batch_index(self.expansion.concepts, ConceptDocument)
self.batch_index(self.expansion.mappings, MappingDocument)
@property
def expansion(self):
if self.expansion_uri:
return self.expansions.filter(uri=self.expansion_uri).first()
return None
@property
def active_references(self):
return self.references.count()
@property
def last_concept_update(self):
updated_at = None
if self.expansion_uri and self.expansion.concepts.exists():
updated_at = self.expansion.concepts.latest('updated_at').updated_at
return updated_at
@property
def last_mapping_update(self):
updated_at = None
if self.expansion_uri and self.expansion.mappings.exists():
updated_at = self.expansion.mappings.latest('updated_at').updated_at
return updated_at
@property
def expansions_url(self):
return self.uri + 'expansions/'
class CollectionReference(models.Model):
class Meta:
db_table = 'collection_references'
concepts = None
mappings = None
original_expression = None
id = models.BigAutoField(primary_key=True)
expression = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
last_resolved_at = models.DateTimeField(default=timezone.now, null=True)
@staticmethod
def diff(ctx, _from):
prev_expressions = map(lambda r: r.expression, _from)
return filter(lambda ref: ref.expression not in prev_expressions, ctx)
@property
def without_version(self):
return drop_version(self.expression)
@property
def is_valid_expression(self):
return is_valid_uri(self.expression) and self.expression.count('/') >= 7
@property
def reference_type(self):
reference = None
if is_concept(self.expression):
reference = CONCEPTS_EXPRESSIONS
if is_mapping(self.expression):
reference = MAPPINGS_EXPRESSIONS
return reference
def fetch_concepts(self, user):
if self.should_fetch_from_api:
return Concept.objects.filter(uri__in=self.fetch_uris(user))
return self.get_concepts()
def fetch_mappings(self, user):
if self.should_fetch_from_api:
return Mapping.objects.filter(uri__in=self.fetch_uris(user))
return self.get_mappings()
def fetch_uris(self, user):
data = api_get(self.expression, user)
if not isinstance(data, list):
data = [data]
return [obj['version_url'] for obj in data]
def get_concepts(self):
return Concept.from_uri_queryset(self.expression)
def get_mappings(self):
return Mapping.from_uri_queryset(self.expression)
def clean(self):
self.original_expression = str(self.expression)
self.create_entities_from_expressions()
is_resolved = bool((self.mappings and self.mappings.count()) or (self.concepts and self.concepts.count()))
if not is_resolved:
self.last_resolved_at = None
@property
def should_fetch_from_api(self):
return SEARCH_PARAM in get(self.expression.split('?'), '1', '')
@property
def is_concept(self):
return is_concept(self.expression)
@property
def is_mapping(self):
return is_mapping(self.expression)
def create_entities_from_expressions(self):
if self.is_concept:
self.concepts = self.get_concepts()
elif self.is_mapping:
self.mappings = self.get_mappings()
if self.concepts and self.concepts.exists():
self.expression = self.concepts.first().uri
elif self.mappings and self.mappings.exists():
self.expression = self.mappings.first().uri
def get_related_uris(self, exclude_mapping_uris, cascade_to_concepts=False):
uris = []
concepts = self.get_concepts()
if concepts.exists():
for concept in concepts:
mapping_queryset = concept.get_unidirectional_mappings().exclude(uri__in=exclude_mapping_uris)
uris = list(mapping_queryset.values_list('uri', flat=True))
if cascade_to_concepts:
to_concepts_queryset = mapping_queryset.filter(to_concept__parent_id=concept.parent_id)
uris += list(to_concepts_queryset.values_list('to_concept__uri', flat=True))
return uris
def default_expansion_parameters():
return {
"filter": "",
"date": "",
"count": 0,
"offset": 0,
"includeDesignations": True,
"activeOnly": False,
"includeDefinition": False,
"excludeNested": True,
"excludeNotForUI": True,
"excludePostCoordinated": True,
"exclude - system": "",
"system - version": "",
"check - system - version": "",
"force - system - version": ""
}
class Expansion(BaseResourceModel):
class Meta:
db_table = 'collection_expansions'
indexes = [] + BaseResourceModel.Meta.indexes
parameters = models.JSONField(default=default_expansion_parameters)
canonical_url = models.URLField(null=True, blank=True)
text = models.TextField(null=True, blank=True)
concepts = models.ManyToManyField('concepts.Concept', blank=True, related_name='expansion_set')
mappings = models.ManyToManyField('mappings.Mapping', blank=True, related_name='expansion_set')
collection_version = models.ForeignKey(
'collections.Collection', related_name='expansions', on_delete=models.CASCADE)
@staticmethod
def default_parameters():
return default_expansion_parameters()
@staticmethod
def get_resource_url_kwarg():
return 'expansion'
@staticmethod
def get_url_kwarg():
return 'expansion'
@property
def is_default(self):
return self.uri == self.collection_version.expansion_uri
@property
def expansion(self):
return self.mnemonic
@property
def active_concepts(self):
return self.concepts.count()
@property
def active_mappings(self):
return self.mappings.count()
@property
def owner_url(self):
return to_owner_uri(self.uri)
def apply_parameters(self, queryset):
parameters = ExpansionParameters(self.parameters)
return parameters.apply(queryset)
def index_concepts(self):
if self.concepts.exists():
if get(settings, 'TEST_MODE', False):
index_expansion_concepts(self.id)
else:
index_expansion_concepts.apply_async((self.id, ), queue='indexing')
def index_mappings(self):
if self.mappings.exists():
if get(settings, 'TEST_MODE', False):
index_expansion_mappings(self.id)
else:
index_expansion_mappings.apply_async((self.id, ), queue='indexing')
def index_all(self):
self.index_concepts()
self.index_mappings()
def delete_expressions(self, expressions):
self.concepts.set(self.concepts.exclude(uri__in=expressions))
self.mappings.set(self.mappings.exclude(uri__in=expressions))
if not get(settings, 'TEST_MODE', False):
batch_index_resources.apply_async(('concept', dict(uri__in=expressions)), queue='indexing')
batch_index_resources.apply_async(('mapping', dict(uri__in=expressions)), queue='indexing')
def add_references(self, references, index=True):
if isinstance(references, CollectionReference):
refs = [references]
elif isinstance(references, list):
refs = references
else:
refs = references.all()
index_concepts = False
index_mappings = False
for reference in refs:
if reference.is_concept:
concepts = reference.fetch_concepts(self.created_by)
if concepts.exists():
index_concepts = True
self.concepts.add(*self.apply_parameters(concepts))
elif reference.is_mapping:
mappings = reference.fetch_mappings(self.created_by)
if mappings.exists():
index_mappings = True
self.mappings.add(*self.apply_parameters(mappings))
if index:
if index_concepts:
self.index_concepts()
if index_mappings:
self.index_mappings()
def seed_children(self, index=True):
return self.add_references(self.collection_version.references, index)
def calculate_uri(self):
version = self.collection_version
if version.is_head:
return self.collection_version.uri + f'HEAD/expansions/{self.mnemonic}/'
return self.collection_version.uri + f'expansions/{self.mnemonic}/'
def clean(self):
if not self.parameters:
self.parameters = default_expansion_parameters()
super().clean()
@classmethod
def persist(cls, index, **kwargs):
expansion = cls(**kwargs)
temp_version = not bool(expansion.mnemonic)
if temp_version:
expansion.mnemonic = generate_temp_version()
expansion.clean()
expansion.full_clean()
expansion.save()
if temp_version and expansion.id:
expansion.mnemonic = expansion.id
expansion.save()
if get(settings, 'TEST_MODE', False):
seed_children_to_expansion(expansion.id, index)
else:
seed_children_to_expansion.delay(expansion.id, index)
return expansion
class ExpansionParameters:
ACTIVE = 'activeOnly'
def __init__(self, parameters):
self.parameters = parameters
self.parameter_classes = {}
self.filters = {}
self.to_parameter_classes()
self.get_filters()
def to_parameter_classes(self):
for parameter, value in self.parameters.items():
if parameter == self.ACTIVE:
self.parameter_classes[parameter] = ExpansionActiveParameter(value=value)
def apply(self, queryset):
queryset = queryset.filter(**self.filters)
return queryset
def get_filters(self):
for _, klass in self.parameter_classes.items():
if klass.can_apply_filters:
self.filters = {**self.filters, **klass.filters}
class ExpansionParameter:
can_apply_filters = True
def __init__(self, value):
self.value = value
class ExpansionActiveParameter(ExpansionParameter):
default_filters = dict(is_active=True, retired=False)
@property
def filters(self):
if self.value is True:
return self.default_filters
return {}