From 9347682997de3ff9d024b948d966620532568f20 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Sun, 31 May 2020 13:29:03 +0300 Subject: [PATCH] Collection update: support limiting vs not limiting based on stoken. --- django_etesync/serializers.py | 6 +----- django_etesync/views.py | 7 +++++++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/django_etesync/serializers.py b/django_etesync/serializers.py index c5734d2..3b50c36 100644 --- a/django_etesync/serializers.py +++ b/django_etesync/serializers.py @@ -203,7 +203,7 @@ class CollectionSerializer(serializers.ModelSerializer): with transaction.atomic(): if etag is not None: - raise serializers.ValidationError('Stoken is not None') + raise serializers.ValidationError('etag is not None') instance.save() main_item = models.CollectionItem.objects.create( @@ -222,13 +222,9 @@ class CollectionSerializer(serializers.ModelSerializer): def update(self, instance, validated_data): """Function that's called when this serializer is meant to update an item""" - etag = validated_data.pop('etag') revision_data = validated_data.pop('content') with transaction.atomic(): - if etag != instance.etag: - raise serializers.ValidationError('Wrong etag. Expected {} got {}'.format(instance.etag, etag)) - main_item = instance.main_item # We don't have to use select_for_update here because the unique constraint on current guards against # the race condition. But it's a good idea because it'll lock and wait rather than fail. diff --git a/django_etesync/views.py b/django_etesync/views.py index 235d378..dff1096 100644 --- a/django_etesync/views.py +++ b/django_etesync/views.py @@ -163,6 +163,13 @@ class CollectionViewSet(BaseViewSet): def update(self, request, *args, **kwargs): instance = self.get_object() + + stoken = request.GET.get('stoken', None) + + if stoken is not None and stoken != instance.stoken: + content = {'code': 'stale_stoken', 'detail': 'Stoken is too old'} + return Response(content, status=status.HTTP_400_BAD_REQUEST) + serializer = self.get_serializer(instance, data=request.data) serializer.is_valid(raise_exception=True) self.perform_update(serializer)