From 0fbc5c104c0359be3c37747b10fadbdd167f8591 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Wed, 15 Apr 2020 17:54:39 +0300 Subject: [PATCH] Implement collection updating. --- django_etesync/serializers.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/django_etesync/serializers.py b/django_etesync/serializers.py index f7f4b71..d4b809f 100644 --- a/django_etesync/serializers.py +++ b/django_etesync/serializers.py @@ -197,3 +197,19 @@ class CollectionSerializer(serializers.ModelSerializer): ).save() return instance + + def update(self, instance, validated_data): + """Function that's called when this serializer is meant to update an item""" + revision_data = validated_data.pop('content') + + with transaction.atomic(): + main_item = instance.mainItem + # 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. + current_revision = main_item.revisions.filter(current=True).select_for_update().first() + current_revision.current = None + current_revision.save() + + process_revisions_for_item(main_item, revision_data) + + return instance