Skip to content

Commit

Permalink
refactored reference add
Browse files Browse the repository at this point in the history
  • Loading branch information
snyaggarwal committed Mar 10, 2021
1 parent 2de62f6 commit b22d7c5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
18 changes: 10 additions & 8 deletions core/collections/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
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, CUSTOM_VALIDATION_SCHEMA_OPENMRS, ACCESS_TYPE_VIEW, ACCESS_TYPE_EDIT
DEFAULT_REPOSITORY_TYPE, ACCESS_TYPE_VIEW, ACCESS_TYPE_EDIT
)
from core.common.models import ConceptContainerModel
from core.common.utils import reverse_resource, is_valid_uri, drop_version
Expand Down Expand Up @@ -122,7 +122,7 @@ def validate(self, reference):
if reference.without_version in [reference.without_version for reference in self.references.all()]:
raise ValidationError({reference.expression: [REFERENCE_ALREADY_EXISTS]})

if self.custom_validation_schema == CUSTOM_VALIDATION_SCHEMA_OPENMRS:
if self.is_openmrs_schema:
if reference.concepts and reference.concepts.count() == 0:
return

Expand Down Expand Up @@ -225,8 +225,8 @@ def add_references_in_bulk(self, expressions, user=None): # pylint: disable=too

added = False
if ref.concepts:
for concept in ref.concepts:
if self.custom_validation_schema == CUSTOM_VALIDATION_SCHEMA_OPENMRS:
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,
Expand All @@ -239,12 +239,14 @@ def add_references_in_bulk(self, expressions, user=None): # pylint: disable=too
except Exception as ex:
errors[expression] = ex.messages if hasattr(ex, 'messages') else ex
continue
collection_version.add_concept(concept)
collection_version.add_concept(concept)
added = True
else:
collection_version.concepts.add(*ref.concepts.all())
added = True
if ref.mappings:
for mapping in ref.mappings:
collection_version.add_mapping(mapping)
added = True
collection_version.mappings.add(*ref.mappings.all())
added = True

if added:
collection_version.references.add(ref)
Expand Down
6 changes: 5 additions & 1 deletion core/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
ACCESS_TYPE_CHOICES, DEFAULT_ACCESS_TYPE, NAMESPACE_REGEX,
ACCESS_TYPE_VIEW, ACCESS_TYPE_EDIT, SUPER_ADMIN_USER_ID,
HEAD, PERSIST_NEW_ERROR_MESSAGE, SOURCE_PARENT_CANNOT_BE_NONE, PARENT_RESOURCE_CANNOT_BE_NONE,
CREATOR_CANNOT_BE_NONE, CANNOT_DELETE_ONLY_VERSION)
CREATOR_CANNOT_BE_NONE, CANNOT_DELETE_ONLY_VERSION, CUSTOM_VALIDATION_SCHEMA_OPENMRS)
from .tasks import handle_save, handle_m2m_changed, seed_children


Expand Down Expand Up @@ -344,6 +344,10 @@ class ConceptContainerModel(VersionedModel):
class Meta:
abstract = True

@property
def is_openmrs_schema(self):
return self.custom_validation_schema == CUSTOM_VALIDATION_SCHEMA_OPENMRS

@property
def active_concepts(self):
return self.concepts.filter(retired=False, is_active=True).distinct('versioned_object_id').count()
Expand Down
3 changes: 1 addition & 2 deletions core/mappings/mixins.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.conf import settings
from django.core.exceptions import ValidationError

from core.common.constants import CUSTOM_VALIDATION_SCHEMA_OPENMRS
from core.sources.models import Source
from .constants import (
MUST_SPECIFY_TO_CONCEPT_OR_TO_SOURCE,
Expand Down Expand Up @@ -35,7 +34,7 @@ def clean(self):
if settings.DISABLE_VALIDATION:
return
try:
if self.parent.custom_validation_schema == CUSTOM_VALIDATION_SCHEMA_OPENMRS:
if self.parent.is_openmrs_schema:
custom_validator = OpenMRSMappingValidator(self)
custom_validator.validate()
except Source.DoesNotExist:
Expand Down

0 comments on commit b22d7c5

Please sign in to comment.