Skip to content

Commit

Permalink
OpenConceptLab/ocl_issues#1682 | Auto assign sort order based on max …
Browse files Browse the repository at this point in the history
…prev sort order
  • Loading branch information
snyaggarwal committed Dec 15, 2023
1 parent b75e4a5 commit 5fe2866
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions core/mappings/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ def clean(self):
if self.parent.is_openmrs_schema:
custom_validator = OpenMRSMappingValidator(self)
custom_validator.validate()
self.sort_weight = self.get_next_sort_weight() if self.sort_weight is None else self.sort_weight
except Source.DoesNotExist as ex:
raise ValidationError("There's no Source") from ex
19 changes: 18 additions & 1 deletion core/mappings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
from django.db import models, IntegrityError, transaction
from django.db.models import Q, F
from django.db.models import Q, F, Max
from pydash import get

from core.common.constants import NAMESPACE_REGEX, LATEST
Expand Down Expand Up @@ -45,6 +45,11 @@ class Meta:
condition=(Q(is_active=True) & Q(retired=False))),
models.Index(name="mappings_map_type_like", fields=["map_type"], opclasses=["text_pattern_ops"]),
models.Index(fields=['uri']),
models.Index(
name='mappings_sort_weight_next',
fields=['from_concept_id', 'sort_weight', 'parent_id', 'map_type'],
condition=Q(sort_weight__isnull=False, id=F('versioned_object_id'), retired=False)
),
] + VersionedModel.Meta.indexes
parent = models.ForeignKey('sources.Source', related_name='mappings_set', on_delete=models.CASCADE)
map_type = models.TextField()
Expand Down Expand Up @@ -449,6 +454,18 @@ def index_from_concept(self):
'concepts', {'versioned_object_id': self.from_concept.versioned_object_id}
)

def get_next_sort_weight(self):
if not self.sort_weight and self.from_concept_id and self.map_type:
max_sort_weight = self.from_concept.get_unidirectional_mappings().filter(
map_type=self.map_type, retired=False
).aggregate(
max_sort_weight=Max('sort_weight')
)['max_sort_weight']
if max_sort_weight is not None:
max_sort_weight += 1
return max_sort_weight
return None

@classmethod
def persist_new(cls, data, user):
related_fields = ['from_concept_url', 'to_concept_url', 'to_source_url', 'from_source_url']
Expand Down

0 comments on commit 5fe2866

Please sign in to comment.