Skip to content

Commit

Permalink
OpenConceptLab/ocl_issues#1746 | Concepts/Mappings standard checksums…
Browse files Browse the repository at this point in the history
… | added external_id, retired and descriptions
  • Loading branch information
snyaggarwal committed Feb 1, 2024
1 parent 5ba8181 commit d4511ae
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
9 changes: 6 additions & 3 deletions core/common/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ def test_post_200_concept(self, checksum_generate_mock):

self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, 'checksum')
checksum_generate_mock.assert_called_once_with({'concept_class': 'foobar', 'names': []})
checksum_generate_mock.assert_called_once_with({'concept_class': 'foobar', 'names': [], 'descriptions': []})

@patch('core.common.checksums.Checksum.generate')
def test_post_200_mapping_standard(self, checksum_generate_mock):
Expand All @@ -1224,6 +1224,7 @@ def test_post_200_mapping_standard(self, checksum_generate_mock):
'from_concept_name': 'fooName',
'to_concept_name': 'barName',
'retired': False,
'external_id': 'EX123',
'extras': {
'foo': 'bar'
}
Expand Down Expand Up @@ -1259,15 +1260,17 @@ def test_post_200_mapping_standard(self, checksum_generate_mock):
'to_concept_code': 'bar',
'from_concept_name': 'fooName',
'to_concept_name': 'barName',
'extras': {'foo': 'bar'}
'extras': {'foo': 'bar'},
'external_id': 'EX123'
}),
call({
'map_type': 'foobarbara',
'from_concept_code': 'foobara',
'to_concept_code': 'barbara',
'from_concept_name': 'foobaraName',
'to_concept_name': 'barbaraName',
'extras': {'foo': 'barbara'}
'extras': {'foo': 'barbara'},
'retired': True
}),
call(['checksum1', 'checksum2'])
]
Expand Down
39 changes: 26 additions & 13 deletions core/concepts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def is_search_index_term(self):


class ConceptDescription(AbstractLocalizedText):
CHECKSUM_INCLUSIONS = AbstractLocalizedText.CHECKSUM_INCLUSIONS + ['description', 'description_type']
CHECKSUM_INCLUSIONS = AbstractLocalizedText.CHECKSUM_INCLUSIONS + ['locale', 'description', 'description_type']

concept = models.ForeignKey('concepts.Concept', on_delete=models.CASCADE, related_name='descriptions')

Expand Down Expand Up @@ -278,32 +278,45 @@ def get_standard_checksum_fields(self):
def get_smart_checksum_fields(self):
return self.get_smart_checksum_fields_for_resource(self)

@staticmethod
def _locales_for_checksums(data, relation, fields, predicate_func):
locales = get(data, relation).filter() if isinstance(data, Concept) else get(data, relation, [])
return [{field: get(locale, field) for field in fields} for locale in locales if predicate_func(locale)]

@staticmethod
def get_standard_checksum_fields_for_resource(data):
names = data.names.filter() if isinstance(data, Concept) else get(data, 'names', [])
return {
'concept_class': get(data, 'concept_class'),
'datatype': get(data, 'datatype'),
'names': [
{
field: get(name, field) for field in ConceptName.CHECKSUM_INCLUSIONS
} for name in names
],
'retired': get(data, 'retired'),
'external_id': get(data, 'external_id'),
'extras': get(data, 'extras'),
'names': Concept._locales_for_checksums(
data,
'names',
ConceptName.CHECKSUM_INCLUSIONS,
lambda _: True
),
'descriptions': Concept._locales_for_checksums(
data,
'descriptions',
ConceptDescription.CHECKSUM_INCLUSIONS,
lambda _: True
),
}

@staticmethod
def get_smart_checksum_fields_for_resource(data):
names = data.names.filter() if isinstance(data, Concept) else get(data, 'names', [])
return {
'concept_class': get(data, 'concept_class'),
'datatype': get(data, 'datatype'),
'names': [
{
field: get(name, field) for field in ConceptName.CHECKSUM_INCLUSIONS
} for name in names if ConceptName.is_fully_specified_type(get(name, 'name_type'))
],
'retired': get(data, 'retired'),
'names': Concept._locales_for_checksums(
data,
'names',
ConceptName.CHECKSUM_INCLUSIONS,
lambda locale: ConceptName.is_fully_specified_type(get(locale, 'name_type'))
),
}

@staticmethod
Expand Down
7 changes: 2 additions & 5 deletions core/mappings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,9 @@ def get_smart_checksum_fields(self):
@staticmethod
def get_standard_checksum_fields_for_resource(data):
return {
'map_type': get(data, 'map_type'),
'from_concept_code': get(data, 'from_concept_code'),
'to_concept_code': get(data, 'to_concept_code'),
'from_concept_name': get(data, 'from_concept_name'),
'to_concept_name': get(data, 'to_concept_name'),
**Mapping.get_smart_checksum_fields_for_resource(data),
'extras': get(data, 'extras'),
'external_id': get(data, 'external_id'),
}

@staticmethod
Expand Down

0 comments on commit d4511ae

Please sign in to comment.