-
Notifications
You must be signed in to change notification settings - Fork 30
/
custom_validators.py
47 lines (38 loc) · 1.93 KB
/
custom_validators.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
from django.core.exceptions import ValidationError
from pydash import get
from .constants import OPENMRS_SINGLE_MAPPING_BETWEEN_TWO_CONCEPTS, OPENMRS_INVALID_MAPTYPE, \
OPENMRS_EXTERNAL_ID_LENGTH, OPENMRS_MAPPING_EXTERNAL_ID_ERROR
class OpenMRSMappingValidator:
def __init__(self, mapping):
self.mapping = mapping
def validate(self):
if self.mapping.retired:
return
self.should_have_valid_external_id()
self.pair_must_be_unique()
self.lookup_attributes_should_be_valid()
def should_have_valid_external_id(self):
if len(get(self.mapping, 'external_id') or '') > OPENMRS_EXTERNAL_ID_LENGTH:
raise ValidationError({'external_id': [OPENMRS_MAPPING_EXTERNAL_ID_ERROR]})
def pair_must_be_unique(self):
from .models import Mapping
queryset = Mapping.objects.filter(
parent=self.mapping.parent, is_active=True, retired=False,
from_source_url=self.mapping.from_source_url, from_concept_code=self.mapping.from_concept_code,
to_source_url=self.mapping.to_source_url, to_concept_code=self.mapping.to_concept_code,
is_latest_version=True
)
if self.mapping.versioned_object_id:
queryset = queryset.exclude(versioned_object_id=self.mapping.versioned_object_id)
if queryset.exists():
raise ValidationError(OPENMRS_SINGLE_MAPPING_BETWEEN_TWO_CONCEPTS)
def lookup_attributes_should_be_valid(self):
from core.concepts.models import Concept
from core.sources.models import Source
parent = Source.objects.filter(uri='/orgs/OCL/sources/MapTypes/').first()
if not Concept.objects.filter(
parent_id=parent.id,
is_latest_version=True, retired=False, is_active=True,
names__name=self.mapping.map_type or 'None',
).exists():
raise ValidationError({'map_type': [OPENMRS_INVALID_MAPTYPE]})