Implement collection updating.

This commit is contained in:
Tom Hacohen 2020-04-15 17:54:39 +03:00
parent 62a7496b66
commit 0fbc5c104c

View File

@ -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