Skip to content

Commit

Permalink
OpenConceptLab/ocl_issues#1674 | Adding checksums calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
snyaggarwal committed Oct 18, 2023
1 parent 2edfd29 commit 79c376a
Show file tree
Hide file tree
Showing 17 changed files with 52 additions and 65 deletions.
15 changes: 0 additions & 15 deletions core/collections/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ class CollectionVersionListSerializer(ModelSerializer):
previous_version_url = CharField(source='prev_version_uri')
autoexpand = BooleanField(source='should_auto_expand')
expansion_url = CharField(source='expansion_uri', read_only=True)
checksums = SerializerMethodField()

class Meta:
model = Collection
Expand All @@ -110,10 +109,6 @@ class Meta:
'previous_version_url', 'autoexpand', 'expansion_url', 'checksums'
)

@staticmethod
def get_checksums(obj):
return obj.get_checksums(queue=True)


class CollectionCreateOrUpdateSerializer(ModelSerializer):
canonical_url = CharField(allow_blank=True, allow_null=True, required=False)
Expand Down Expand Up @@ -358,7 +353,6 @@ class CollectionDetailSerializer(CollectionCreateOrUpdateSerializer, AbstractRep
summary = SerializerMethodField()
client_configs = SerializerMethodField()
expansion_url = CharField(source='expansion_uri', read_only=True, allow_null=True, allow_blank=True)
checksums = SerializerMethodField()

class Meta:
model = Collection
Expand Down Expand Up @@ -413,10 +407,6 @@ def to_representation(self, instance): # used to be to_native
ret.update({"supported_locales": instance.get_supported_locales()})
return ret

@staticmethod
def get_checksums(obj):
return obj.get_checksums(queue=True)


class CollectionVersionDetailSerializer(CollectionCreateOrUpdateSerializer, AbstractRepoResourcesSerializer):
type = CharField(source='resource_version_type')
Expand All @@ -439,7 +429,6 @@ class CollectionVersionDetailSerializer(CollectionCreateOrUpdateSerializer, Abst
summary = SerializerMethodField()
autoexpand = SerializerMethodField()
expansion_url = CharField(source='expansion_uri', allow_null=True, allow_blank=True)
checksums = SerializerMethodField()

class Meta:
model = Collection
Expand Down Expand Up @@ -482,10 +471,6 @@ def get_summary(self, obj):
def get_autoexpand(obj):
return obj.should_auto_expand

@staticmethod
def get_checksums(obj):
return obj.get_checksums(queue=True)


class CollectionReferenceSerializer(ModelSerializer):
reference_type = CharField(read_only=True)
Expand Down
6 changes: 5 additions & 1 deletion core/common/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,23 @@ def create_user(self, claims):
# 'email': 'inactive@user.com'
# }
from core.users.models import UserProfile
return UserProfile.objects.create_user(
user = UserProfile.objects.create_user(
claims.get('preferred_username'),
email=claims.get('email'),
first_name=claims.get('given_name'),
last_name=claims.get('family_name'),
verified=claims.get('email_verified')
)
if user.id:
user.set_checksums()
return user

def update_user(self, user, claims):
user.first_name = claims.get('given_name') or user.first_name
user.last_name = claims.get('family_name') or user.last_name
user.email = claims.get('email') or user.email
user.save()
user.set_checksums()
return user

def filter_users_by_claims(self, claims):
Expand Down
32 changes: 25 additions & 7 deletions core/common/checksums.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class Meta:
CHECKSUM_INCLUSIONS = []
STANDARD_CHECKSUM_KEY = 'standard'
SMART_CHECKSUM_KEY = 'smart'
CHECKSUM_TYPES = {STANDARD_CHECKSUM_KEY}
STANDARD_CHECKSUM_TYPES = {STANDARD_CHECKSUM_KEY}

def get_checksums(self, standard=False, queue=False, recalculate=False):
if Toggle.get('CHECKSUMS_TOGGLE'):
Expand Down Expand Up @@ -52,13 +50,20 @@ def set_specific_checksums(self, checksum_type, checksum):
self.save(update_fields=['checksums'])

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

def has_all_checksums(self):
return set(self.checksums.keys()) - set(self.CHECKSUM_TYPES) == set()
return self.has_standard_checksum() and self.has_smart_checksum()

def has_standard_checksums(self):
return set(self.checksums.keys()) - set(self.STANDARD_CHECKSUM_TYPES) == set()
def has_standard_checksum(self):
if self.STANDARD_CHECKSUM_KEY:
return self.STANDARD_CHECKSUM_KEY in self.checksums
return True

def has_smart_checksum(self):
if self.SMART_CHECKSUM_KEY:
return self.SMART_CHECKSUM_KEY in self.checksums
return True

def set_checksums(self):
if Toggle.get('CHECKSUMS_TOGGLE'):
Expand All @@ -70,6 +75,11 @@ def set_standard_checksums(self):
self.checksums = self.get_standard_checksums()
self.save(update_fields=['checksums'])

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

@property
def checksum(self):
"""Returns the checksum of the model instance or standard only checksum."""
Expand All @@ -92,12 +102,20 @@ def get_smart_checksum_fields(self):

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

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

def get_all_checksums(self):
if Toggle.get('CHECKSUMS_TOGGLE'):
checksums = {}
Expand Down
3 changes: 1 addition & 2 deletions core/common/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ def update(self, request):
if serializer.is_valid():
self.object = serializer.save(**save_kwargs)
if serializer.is_valid():
self.object.get_checksums(recalculate=True)
serializer = self.get_detail_serializer(self.object)
return Response(serializer.data, status=success_status_code)

Expand Down Expand Up @@ -454,8 +455,6 @@ class SourceChildMixin(ChecksumModel):
class Meta:
abstract = True

CHECKSUM_TYPES = {'standard', 'smart'}

@staticmethod
def is_strictly_equal(instance1, instance2):
return instance1.get_checksums() == instance2.get_checksums()
Expand Down
3 changes: 3 additions & 0 deletions core/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,9 @@ def persist_new_version(cls, obj, user=None, **kwargs):
obj.update_version_data(head)
obj.save(**kwargs)

if obj.id:
obj.get_checksums(recalculate=True)

is_test_mode = get(settings, 'TEST_MODE', False)
if is_test_mode or sync:
seed_children_to_new_version(obj.resource_type.lower(), obj.id, not is_test_mode, sync)
Expand Down
2 changes: 2 additions & 0 deletions core/common/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,7 @@ def update(self, request, **kwargs): # pylint: disable=arguments-differ
instance.extras[key] = value
instance.comment = f'Updated extras: {key}={value}.'
instance.save()
instance.set_checksums()
return Response({key: value})

def delete(self, request, *args, **kwargs):
Expand All @@ -992,6 +993,7 @@ def delete(self, request, *args, **kwargs):
del instance.extras[key]
instance.comment = f'Deleted extra {key}.'
instance.save()
instance.set_checksums()
return Response(status=status.HTTP_204_NO_CONTENT)

return Response({'detail': NOT_FOUND}, status=status.HTTP_404_NOT_FOUND)
Expand Down
1 change: 1 addition & 0 deletions core/concepts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ def update_versioned_object(self):
concept.external_id = self.external_id or concept.external_id
concept.updated_by_id = self.updated_by_id
concept.save()
concept.set_checksums()

@classmethod
def persist_clone(
Expand Down
15 changes: 0 additions & 15 deletions core/concepts/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ class ConceptVersionListSerializer(ConceptListSerializer):
previous_version_url = CharField(read_only=True, source='prev_version_uri')
source_versions = ListField(read_only=True)
collection_versions = ListField(read_only=True)
checksums = SerializerMethodField()

class Meta:
model = Concept
Expand All @@ -301,10 +300,6 @@ def __init__(self, *args, **kwargs):

super().__init__(*args, **kwargs)

@staticmethod
def get_checksums(obj):
return obj.get_checksums(queue=True)


class ConceptVersionCascadeSerializer(ConceptVersionListSerializer):
class Meta:
Expand Down Expand Up @@ -411,7 +406,6 @@ class ConceptDetailSerializer(ConceptAbstractSerializer):
url = CharField(required=False, source='versioned_object_url')
updated_by = DateTimeField(source='updated_by.username', read_only=True)
created_by = DateTimeField(source='created_by.username', read_only=True)
checksums = SerializerMethodField()

class Meta:
model = Concept
Expand Down Expand Up @@ -441,10 +435,6 @@ def update(self, instance, validated_data):
self._errors.update(errors)
return instance

@staticmethod
def get_checksums(obj):
return obj.get_checksums(queue=True)


class ConceptVersionExportSerializer(ModelSerializer):
type = CharField(source='resource_type')
Expand Down Expand Up @@ -506,7 +496,6 @@ class ConceptVersionDetailSerializer(ModelSerializer):
source_versions = ListField(read_only=True)
collection_versions = ListField(read_only=True)
references = SerializerMethodField()
checksums = SerializerMethodField()

def __init__(self, *args, **kwargs):
request = get(kwargs, 'context.request')
Expand Down Expand Up @@ -552,10 +541,6 @@ class Meta:
'version_updated_on', 'version_updated_by'
)

@staticmethod
def get_checksums(obj):
return obj.get_checksums(queue=True)

def get_references(self, obj):
collection = get(self, 'context.request.instance')
if collection:
Expand Down
1 change: 1 addition & 0 deletions core/mappings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ def update_versioned_object(self):
mapping.from_source_version = self.from_source_version
mapping.updated_by_id = self.updated_by_id
mapping.save()
mapping.set_checksums()

@classmethod
def persist_clone(cls, obj, user=None, **kwargs): # pylint: disable=too-many-statements
Expand Down
10 changes: 0 additions & 10 deletions core/mappings/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ class MappingVersionListSerializer(MappingListSerializer):
previous_version_url = CharField(read_only=True, source='prev_version_uri')
source_versions = ListField(read_only=True)
collection_versions = ListField(read_only=True)
checksums = SerializerMethodField()

class Meta:
model = Mapping
Expand All @@ -151,10 +150,6 @@ def __init__(self, *args, **kwargs):

super().__init__(*args, **kwargs)

@staticmethod
def get_checksums(obj):
return obj.get_checksums(queue=True)


class MappingMinimalSerializer(AbstractMappingSerializer):
id = CharField(source='mnemonic', read_only=True)
Expand Down Expand Up @@ -222,7 +217,6 @@ class MappingDetailSerializer(MappingListSerializer):
to_source = SourceDetailSerializer()
created_on = DateTimeField(source='created_at', read_only=True)
updated_on = DateTimeField(source='updated_at', read_only=True)
checksums = SerializerMethodField()

class Meta:
model = Mapping
Expand All @@ -243,10 +237,6 @@ def update(self, instance, validated_data):
self._errors.update(errors)
return instance

@staticmethod
def get_checksums(obj):
return obj.get_checksums(queue=True)


class MappingVersionDetailSerializer(MappingDetailSerializer):
previous_version_url = CharField(read_only=True, source='prev_version_uri')
Expand Down
4 changes: 4 additions & 0 deletions core/orgs/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def create(self, validated_data):
organization = self.prepare_object(validated_data)
if not self._errors:
organization.save()
if organization.id:
organization.set_checksums()
return organization


Expand Down Expand Up @@ -129,6 +131,8 @@ def update(self, instance, validated_data):
instance.text = validated_data.get('text', None)
instance.updated_by = request_user
instance.save()
if instance.id:
instance.set_checksums()
return instance


Expand Down
2 changes: 2 additions & 0 deletions core/orgs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ def update(self, request, **kwargs): # pylint: disable=arguments-differ
instance.extras = get(instance, 'extras', {})
instance.extras[key] = value
instance.save()
instance.set_checksums()
return Response({key: value})

def delete(self, request, *args, **kwargs):
Expand All @@ -305,6 +306,7 @@ def delete(self, request, *args, **kwargs):
if key in instance.extras:
del instance.extras[key]
instance.save()
instance.set_checksums()
return Response(status=status.HTTP_204_NO_CONTENT)

return Response({'detail': NOT_FOUND}, status=status.HTTP_404_NOT_FOUND)
15 changes: 0 additions & 15 deletions core/sources/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ class SourceVersionListSerializer(ModelSerializer):
version_url = CharField(source='uri')
url = CharField(source='versioned_object_url')
previous_version_url = CharField(source='prev_version_uri')
checksums = SerializerMethodField()

class Meta:
model = Source
Expand All @@ -100,10 +99,6 @@ class Meta:
'previous_version_url', 'checksums'
)

@staticmethod
def get_checksums(obj):
return obj.get_checksums(queue=True)


class SourceCreateOrUpdateSerializer(ModelSerializer):
canonical_url = CharField(allow_blank=True, allow_null=True, required=False)
Expand Down Expand Up @@ -355,7 +350,6 @@ class SourceDetailSerializer(SourceCreateOrUpdateSerializer, AbstractRepoResourc
client_configs = SerializerMethodField()
hierarchy_root = SerializerMethodField()
hierarchy_root_url = CharField(source='hierarchy_root.url', required=False, allow_blank=True, allow_null=True)
checksums = SerializerMethodField()

class Meta:
model = Source
Expand Down Expand Up @@ -422,10 +416,6 @@ def to_representation(self, instance): # used to be to_native
ret.update({"supported_locales": instance.get_supported_locales()})
return ret

@staticmethod
def get_checksums(obj):
return obj.get_checksums(queue=True)


class SourceVersionDetailSerializer(SourceCreateOrUpdateSerializer, AbstractRepoResourcesSerializer):
type = CharField(source='resource_version_type')
Expand All @@ -447,7 +437,6 @@ class SourceVersionDetailSerializer(SourceCreateOrUpdateSerializer, AbstractRepo
previous_version_url = CharField(source='prev_version_uri')
summary = SerializerMethodField()
hierarchy_root_url = CharField(source='hierarchy_root.url', required=False, allow_blank=True, allow_null=True)
checksums = SerializerMethodField()

class Meta:
model = Source
Expand Down Expand Up @@ -487,10 +476,6 @@ def get_summary(self, obj):

return summary

@staticmethod
def get_checksums(obj):
return obj.get_checksums(queue=True)


class SourceVersionExportSerializer(SourceVersionDetailSerializer):
source = JSONField(source='snapshot')
Expand Down
Loading

0 comments on commit 79c376a

Please sign in to comment.