From c5af5fd4e6b2ee26b9658c9c95a9b9b5294d2d48 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Thu, 16 Apr 2020 16:33:16 +0300 Subject: [PATCH] Collection: move stoken to the model. --- django_etesync/models.py | 9 +++++++++ django_etesync/serializers.py | 10 +--------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/django_etesync/models.py b/django_etesync/models.py index 7315ee8..c6ec571 100644 --- a/django_etesync/models.py +++ b/django_etesync/models.py @@ -44,6 +44,15 @@ class Collection(models.Model): def content(self): return self.main_item.content + @cached_property + def stoken(self): + last_revision = CollectionItemRevision.objects.filter(item__collection=self).last() + if last_revision is None: + # FIXME: what is the etag for None? Though if we use the revision for collection it should be shared anyway. + return None + + return last_revision.uid + class CollectionItem(models.Model): uid = models.CharField(db_index=True, blank=False, null=True, diff --git a/django_etesync/serializers.py b/django_etesync/serializers.py index 03dd0bb..eb50d76 100644 --- a/django_etesync/serializers.py +++ b/django_etesync/serializers.py @@ -149,7 +149,7 @@ class CollectionItemSerializer(serializers.ModelSerializer): class CollectionSerializer(serializers.ModelSerializer): encryptionKey = CollectionEncryptionKeyField() accessLevel = serializers.SerializerMethodField('get_access_level_from_context') - stoken = serializers.SerializerMethodField('get_stoken') + stoken = serializers.CharField(read_only=True) content = CollectionItemRevisionSerializer(many=False) class Meta: @@ -162,14 +162,6 @@ class CollectionSerializer(serializers.ModelSerializer): return obj.members.get(user=request.user).accessLevel return None - def get_stoken(self, obj): - last_revision = models.CollectionItemRevision.objects.filter(item__collection=obj).last() - if last_revision is None: - # FIXME: what is the etag for None? Though if we use the revision for collection it should be shared anyway. - return None - - return last_revision.uid - def create(self, validated_data): """Function that's called when this serializer creates an item""" revision_data = validated_data.pop('content')