Skip to content

Commit

Permalink
OpenConceptLab/ocl_issues#1674 | reafactoring and adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
snyaggarwal committed Oct 23, 2023
1 parent 3c33bf1 commit 5bcc7ec
Show file tree
Hide file tree
Showing 13 changed files with 405 additions and 150 deletions.
36 changes: 22 additions & 14 deletions core/collections/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,32 @@ class Meta:
expansion_uri = models.TextField(null=True, blank=True)

def get_standard_checksum_fields(self):
return self.get_standard_checksum_fields_for_resource(self)

def get_smart_checksum_fields(self):
return self.get_smart_checksum_fields_for_resource(self)

@staticmethod
def get_standard_checksum_fields_for_resource(data):
return {
'collection_type': self.collection_type,
'canonical_url': self.canonical_url,
'custom_validation_schema': self.custom_validation_schema,
'default_locale': self.default_locale,
'supported_locales': self.supported_locales,
'website': self.website,
'extras': self.extras,
'collection_type': get(data, 'collection_type'),
'canonical_url': get(data, 'canonical_url'),
'custom_validation_schema': get(data, 'custom_validation_schema'),
'default_locale': get(data, 'default_locale'),
'supported_locales': get(data, 'supported_locales'),
'website': get(data, 'website'),
'extras': get(data, 'extras'),
}

def get_smart_checksum_fields(self):
@staticmethod
def get_smart_checksum_fields_for_resource(data):
return {
'collection_type': self.collection_type,
'canonical_url': self.canonical_url,
'custom_validation_schema': self.custom_validation_schema,
'default_locale': self.default_locale,
'released': self.released,
'retired': self.retired,
'collection_type': get(data, 'collection_type'),
'canonical_url': get(data, 'canonical_url'),
'custom_validation_schema': get(data, 'custom_validation_schema'),
'default_locale': get(data, 'default_locale'),
'released': get(data, 'released'),
'retired': get(data, 'retired'),
}

def set_active_concepts(self):
Expand Down
75 changes: 33 additions & 42 deletions core/common/checksums.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,18 @@ class Meta:
STANDARD_CHECKSUM_KEY = 'standard'
SMART_CHECKSUM_KEY = 'smart'

def get_checksums(self, standard=False, queue=False, recalculate=False):
def get_checksums(self, queue=False, recalculate=False):
_checksums = None
if Toggle.get('CHECKSUMS_TOGGLE'):
if not recalculate and self.checksums and self.has_checksums(standard):
return self.checksums
if queue:
if not recalculate and self.checksums and self.has_all_checksums():
_checksums = self.checksums
elif queue:
self.queue_checksum_calculation()
return self.checksums or {}
if standard:
self.set_standard_checksums()
_checksums = self.checksums or {}
else:
self.set_checksums()

return self.checksums
return None
_checksums = self.checksums
return _checksums

def queue_checksum_calculation(self):
from core.common.tasks import calculate_checksums
Expand All @@ -44,14 +42,6 @@ def queue_checksum_calculation(self):
else:
calculate_checksums.delay(self.__class__.__name__, self.id)

def set_specific_checksums(self, checksum_type, checksum):
self.checksums = self.checksums or {}
self.checksums[checksum_type] = checksum
self.save(update_fields=['checksums'])

def has_checksums(self, standard=False):
return self.has_standard_checksum() if standard else self.has_all_checksums()

def has_all_checksums(self):
return self.has_standard_checksum() and self.has_smart_checksum()

Expand All @@ -66,21 +56,17 @@ def set_checksums(self):
self.checksums = self._calculate_checksums()
self.save(update_fields=['checksums'])

def set_standard_checksums(self):
if Toggle.get('CHECKSUMS_TOGGLE'):
self.checksums = self.get_standard_checksums()
self.save(update_fields=['checksums'])

@property
def checksum(self):
"""Returns the checksum of the model instance or standard only checksum."""
_checksum = None
if Toggle.get('CHECKSUMS_TOGGLE'):
if get(self, f'checksums.{self.STANDARD_CHECKSUM_KEY}'):
return self.checksums[self.STANDARD_CHECKSUM_KEY]
self.get_checksums()

return self.checksums.get(self.STANDARD_CHECKSUM_KEY)
return None
_checksum = self.checksums[self.STANDARD_CHECKSUM_KEY]
else:
self.get_checksums()
_checksum = self.checksums.get(self.STANDARD_CHECKSUM_KEY)
return _checksum

def get_checksum_fields(self):
return {field: getattr(self, field) for field in self.CHECKSUM_INCLUSIONS}
Expand All @@ -91,28 +77,31 @@ def get_standard_checksum_fields(self):
def get_smart_checksum_fields(self):
return {}

def get_standard_checksums(self):
if Toggle.get('CHECKSUMS_TOGGLE'):
checksums = self.checksums or {}
if self.STANDARD_CHECKSUM_KEY:
checksums[self.STANDARD_CHECKSUM_KEY] = self._calculate_standard_checksum()
return checksums
return None

def get_all_checksums(self):
checksums = None
if Toggle.get('CHECKSUMS_TOGGLE'):
checksums = {}
if self.STANDARD_CHECKSUM_KEY:
checksums[self.STANDARD_CHECKSUM_KEY] = self._calculate_standard_checksum()
if self.SMART_CHECKSUM_KEY:
checksums[self.SMART_CHECKSUM_KEY] = self._calculate_smart_checksum()
return checksums
return None
return checksums

@staticmethod
def generate_checksum(data):
return Checksum.generate(ChecksumModel._cleanup(data))

@staticmethod
def generate_checksum_from_many(data):
checksums = [
Checksum.generate(ChecksumModel._cleanup(_data)) for _data in data
] if isinstance(data, list) else [
Checksum.generate(ChecksumModel._cleanup(data))
]
if len(checksums) == 1:
return checksums[0]
return Checksum.generate(checksums)

def _calculate_standard_checksum(self):
fields = self.get_standard_checksum_fields()
return None if fields is None else self.generate_checksum(fields)
Expand All @@ -123,16 +112,18 @@ def _calculate_smart_checksum(self):

@staticmethod
def _cleanup(fields):
result = fields
if isinstance(fields, dict):
new_fields = {}
result = {}
for key, value in fields.items():
if value is None:
continue
if key in ['is_active', 'retired'] and not value:
continue
new_fields[key] = value
return new_fields
return fields
if key in ['extras'] and not value:
continue
result[key] = value
return result

def _calculate_checksums(self):
return self.get_all_checksums()
Expand Down
8 changes: 3 additions & 5 deletions core/common/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ def checksums(self):
smart.append(get(result.checksums, 'smart'))
standard = compact(standard)
smart = compact(smart)
return Checksum.generate(standard) if standard else None, Checksum.generate(smart) if smart else None
standard = Checksum.generate(standard) if len(standard) > 1 else get(standard, '0')
smart = Checksum.generate(smart) if len(smart) > 1 else get(smart, '0')
return standard, smart


class ListWithHeadersMixin(ListModelMixin):
Expand Down Expand Up @@ -474,10 +476,6 @@ class Meta:
def is_strictly_equal(instance1, instance2):
return instance1.get_checksums() == instance2.get_checksums()

@staticmethod
def is_equal(instance1, instance2):
return instance1.get_standard_checksums() == instance2.get_standard_checksums()

@staticmethod
def apply_user_criteria(queryset, user):
queryset = queryset.exclude(
Expand Down
9 changes: 9 additions & 0 deletions core/common/swagger_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@
'resource', openapi.IN_PATH, type=openapi.TYPE_STRING,
enum=['mappings', 'concepts', 'sources', 'orgs', 'users', 'collections']
)
all_resource_query_param = openapi.Parameter(
'resource',
openapi.IN_QUERY,
description="Resource type to generate checksum",
type=openapi.TYPE_STRING,
default='concept_version',
enum=['concept_version', 'mapping_version', 'source_version', 'collection_version', 'org', 'user'],
required=True
)
parallel_threads_param = openapi.Parameter(
'parallel', openapi.IN_FORM, description="Parallel threads count (default: 5, max: 10)", type=openapi.TYPE_INTEGER
)
Expand Down
Loading

0 comments on commit 5bcc7ec

Please sign in to comment.