def must_have_unique_fully_specified_name_for_same_source_and_locale(self): from concepts.models import Concept, ConceptVersion fully_specified_names_in_concept = dict() self_id = getattr(self.concept, "versioned_object_id", None) for name in [n for n in self.concept.names if n.is_fully_specified]: # Concept preferred_name should be unique for same source and locale. validation_error = {'names': [ message_with_name_details(OPENMRS_FULLY_SPECIFIED_NAME_UNIQUE_PER_SOURCE_LOCALE, name)]} # making sure names in the submitted concept meet the same rule name_key = name.locale + name.name if name_key in fully_specified_names_in_concept: raise ValidationError(validation_error) fully_specified_names_in_concept[name_key] = True other_concepts_in_source = list(Concept.objects \ .filter(parent_id=self.concept.parent_source.id, is_active=True, retired=False) \ .exclude(id=self_id) \ .values_list('id', flat=True)) if len(other_concepts_in_source) < 1: continue same_name_and_locale = {'versioned_object_id': {'$in': other_concepts_in_source}, 'names': {'$elemMatch': {'name': name.name, 'locale': name.locale}}, 'is_latest_version': True} if ConceptVersion.objects.raw_query(same_name_and_locale).count() > 0: raise ValidationError(validation_error)
def preferred_name_should_be_unique_for_source_and_locale(self): from concepts.models import Concept, ConceptVersion # Concept preferred_name should be unique for same source and locale. preferred_names_dict = dict() self_id = getattr(self.concept, 'versioned_object_id', getattr(self.concept, 'id', None)) preferred_names_list = [n for n in self.concept.names if n.locale_preferred] for name in preferred_names_list: validation_error = { 'names': [message_with_name_details(OPENMRS_PREFERRED_NAME_UNIQUE_PER_SOURCE_LOCALE, name)]} # making sure names in the submitted concept meet the same rule name_key = name.locale + name.name if name_key in preferred_names_dict: raise ValidationError(validation_error) preferred_names_dict[name_key] = True other_concepts_in_source = list(Concept.objects .filter(parent_id=self.concept.parent_source.id, is_active=True, retired=False) .exclude(id=self_id) .values_list('id', flat=True)) if len(other_concepts_in_source) < 1: continue same_name_and_locale = {'versioned_object_id': {'$in': other_concepts_in_source}, 'names': {'$elemMatch': {'name': name.name, 'locale': name.locale, 'type': {'$nin': ['Short', 'SHORT']}}}, 'is_latest_version': True} if ConceptVersion.objects.raw_query(same_name_and_locale).count() > 0: raise ValidationError(validation_error)
def short_name_cannot_be_marked_as_locale_preferred(self): short_preferred_names_in_concept = filter( lambda name: (name.is_short or name.is_search_index_term) and name.locale_preferred, self.concept.names) if len(short_preferred_names_in_concept) > 0: raise ValidationError({ 'names': [message_with_name_details(OPENMRS_SHORT_NAME_CANNOT_BE_PREFERRED, short_preferred_names_in_concept[0])] })
def short_name_cannot_be_marked_as_locale_preferred(self, concept): short_preferred_names_in_concept = filter( lambda name: (name.is_short or name.is_search_index_term) and name.locale_preferred, concept.names) if len(short_preferred_names_in_concept) > 0: raise ValidationError({ 'names': [message_with_name_details(OPENMRS_SHORT_NAME_CANNOT_BE_PREFERRED, short_preferred_names_in_concept[0])] })
def attribute_should_be_unique_for_source_and_locale(self, concept, attribute, error_message): self_id = getattr(concept, 'versioned_object_id', getattr(concept, 'id', None)) names = [n for n in concept.names if getattr(n, attribute)] for name in names: if self.no_other_record_has_same_name(name, self_id): continue raise ValidationError({ 'names': [message_with_name_details(error_message, name)]})
def name_type_should_be_valid_attribute(self, org): if not self.concept.names: return for name in self.concept.names: if name.type in ['FULLY_SPECIFIED', 'SHORT']: continue if self.is_attribute_valid(name.type, org, 'NameTypes', 'NameType'): continue raise ValidationError({'names': [message_with_name_details(OPENMRS_NAME_TYPE, name)]})
def no_more_than_one_short_name_per_locale(self): short_names_per_locale = dict() for name in self.concept.names: if not name.is_short: continue if name.locale in short_names_per_locale: raise ValidationError( {'names': [message_with_name_details(OPENMRS_NO_MORE_THAN_ONE_SHORT_NAME_PER_LOCALE, name)]}) short_names_per_locale[name.locale] = True
def only_one_fully_specified_name_per_locale(self): fully_specified_names_per_locale = dict() for name in self.concept.names: if not name.is_fully_specified: continue if name.locale in fully_specified_names_per_locale: raise ValidationError( {'names': [message_with_name_details(OPENMRS_ONE_FULLY_SPECIFIED_NAME_PER_LOCALE, name)]}) fully_specified_names_per_locale[name.locale] = True
def name_type_should_be_valid_attribute(self, concept): if not concept.names: return for name in concept.names: if name.type in ['FULLY_SPECIFIED', 'SHORT']: continue if (name.type or 'None') in self.reference_values['NameTypes']: continue raise ValidationError({'names': [message_with_name_details(OPENMRS_NAME_TYPE, name)]})
def only_one_fully_specified_name_per_locale(self, concept): fully_specified_names_per_locale = dict() for name in concept.names: if not name.is_fully_specified: continue if name.locale in fully_specified_names_per_locale: raise ValidationError( {'names': [message_with_name_details(OPENMRS_ONE_FULLY_SPECIFIED_NAME_PER_LOCALE, name)]}) fully_specified_names_per_locale[name.locale] = True
def no_more_than_one_short_name_per_locale(self, concept): short_names_per_locale = dict() for name in concept.names: if not name.is_short: continue if name.locale in short_names_per_locale: raise ValidationError( {'names': [message_with_name_details(OPENMRS_NO_MORE_THAN_ONE_SHORT_NAME_PER_LOCALE, name)]}) short_names_per_locale[name.locale] = True
def must_have_exactly_one_preferred_name(self): preferred_name_locales_in_concept = dict() for name in self.concept.names: if not name.locale_preferred: continue if name.locale in preferred_name_locales_in_concept: raise ValidationError({ 'names': [message_with_name_details(OPENMRS_MUST_HAVE_EXACTLY_ONE_PREFERRED_NAME, name)] }) preferred_name_locales_in_concept[name.locale] = True
def must_have_exactly_one_preferred_name(self, concept): preferred_name_locales_in_concept = dict() for name in concept.names: if not name.locale_preferred: continue if name.locale in preferred_name_locales_in_concept: raise ValidationError({ 'names': [message_with_name_details(OPENMRS_MUST_HAVE_EXACTLY_ONE_PREFERRED_NAME, name)] }) preferred_name_locales_in_concept[name.locale] = True
def must_have_unique_fully_specified_name_for_same_source_and_locale(self): from concepts.models import Concept, ConceptVersion fully_specified_names_in_concept = dict() self_id = getattr(self.concept, "versioned_object_id", None) for name in [n for n in self.concept.names if n.is_fully_specified]: # Concept preferred_name should be unique for same source and locale. validation_error = { 'names': [ message_with_name_details( OPENMRS_FULLY_SPECIFIED_NAME_UNIQUE_PER_SOURCE_LOCALE, name) ] } # making sure names in the submitted concept meet the same rule name_key = name.locale + name.name if name_key in fully_specified_names_in_concept: raise ValidationError(validation_error) fully_specified_names_in_concept[name_key] = True other_concepts_in_source = list(Concept.objects \ .filter(parent_id=self.concept.parent_source.id, is_active=True, retired=False) \ .exclude(id=self_id) \ .values_list('id', flat=True)) if len(other_concepts_in_source) < 1: continue same_name_and_locale = { 'versioned_object_id': { '$in': other_concepts_in_source }, 'names': { '$elemMatch': { 'name': name.name, 'locale': name.locale } }, 'is_latest_version': True } if ConceptVersion.objects.raw_query( same_name_and_locale).count() > 0: raise ValidationError(validation_error)