-
Notifications
You must be signed in to change notification settings - Fork 27
/
mixins.py
41 lines (36 loc) · 1.59 KB
/
mixins.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
from django.conf import settings
from django.core.exceptions import ValidationError
from core.sources.models import Source
from .constants import (
MUST_SPECIFY_TO_CONCEPT_OR_TO_SOURCE,
MUST_SPECIFY_FROM_CONCEPT, TO_SOURCE_UNIQUE_ATTRIBUTES_ERROR_MESSAGE
)
from .custom_validators import OpenMRSMappingValidator
class MappingValidationMixin:
def clean(self):
from .models import Mapping
errors = []
if not self.from_concept_code:
errors.append(MUST_SPECIFY_FROM_CONCEPT)
if not self.to_concept_code:
errors.append(MUST_SPECIFY_TO_CONCEPT_OR_TO_SOURCE)
if Mapping.objects.exclude(
versioned_object_id=self.versioned_object_id
).filter(
parent_id=self.parent_id, map_type=self.map_type,
from_concept_code=self.from_concept_code, to_concept_code=self.to_concept_code,
to_source_url=self.to_source_url, from_source_url=self.from_source_url,
is_latest_version=True, retired=False
).exists():
errors.append(TO_SOURCE_UNIQUE_ATTRIBUTES_ERROR_MESSAGE)
if errors:
raise ValidationError(' '.join(errors))
if settings.DISABLE_VALIDATION:
return
try:
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