Collection update: support limiting vs not limiting based on stoken.

master
Tom Hacohen 5 years ago
parent 64b947d455
commit 9347682997

@ -203,7 +203,7 @@ class CollectionSerializer(serializers.ModelSerializer):
with transaction.atomic(): with transaction.atomic():
if etag is not None: if etag is not None:
raise serializers.ValidationError('Stoken is not None') raise serializers.ValidationError('etag is not None')
instance.save() instance.save()
main_item = models.CollectionItem.objects.create( main_item = models.CollectionItem.objects.create(
@ -222,13 +222,9 @@ class CollectionSerializer(serializers.ModelSerializer):
def update(self, instance, validated_data): def update(self, instance, validated_data):
"""Function that's called when this serializer is meant to update an item""" """Function that's called when this serializer is meant to update an item"""
etag = validated_data.pop('etag')
revision_data = validated_data.pop('content') revision_data = validated_data.pop('content')
with transaction.atomic(): with transaction.atomic():
if etag != instance.etag:
raise serializers.ValidationError('Wrong etag. Expected {} got {}'.format(instance.etag, etag))
main_item = instance.main_item main_item = instance.main_item
# We don't have to use select_for_update here because the unique constraint on current guards against # 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. # the race condition. But it's a good idea because it'll lock and wait rather than fail.

@ -163,6 +163,13 @@ class CollectionViewSet(BaseViewSet):
def update(self, request, *args, **kwargs): def update(self, request, *args, **kwargs):
instance = self.get_object() 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 = self.get_serializer(instance, data=request.data)
serializer.is_valid(raise_exception=True) serializer.is_valid(raise_exception=True)
self.perform_update(serializer) self.perform_update(serializer)

Loading…
Cancel
Save