From a72543f6c9ae10f8f9bff8bd439dbbef7159703d Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Thu, 16 Apr 2020 11:28:49 +0300 Subject: [PATCH] Collection remove the redundant mainItem model attr. --- .../0039_remove_collection_mainitem.py | 17 +++++++++++++++++ django_etesync/models.py | 7 +++++-- django_etesync/serializers.py | 4 +--- 3 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 django_etesync/migrations/0039_remove_collection_mainitem.py diff --git a/django_etesync/migrations/0039_remove_collection_mainitem.py b/django_etesync/migrations/0039_remove_collection_mainitem.py new file mode 100644 index 0000000..1822bc7 --- /dev/null +++ b/django_etesync/migrations/0039_remove_collection_mainitem.py @@ -0,0 +1,17 @@ +# Generated by Django 3.0.3 on 2020-04-16 08:28 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('django_etesync', '0038_remove_collectionitemrevision_chunks'), + ] + + operations = [ + migrations.RemoveField( + model_name='collection', + name='mainItem', + ), + ] diff --git a/django_etesync/models.py b/django_etesync/models.py index 449c743..7315ee8 100644 --- a/django_etesync/models.py +++ b/django_etesync/models.py @@ -29,7 +29,6 @@ class Collection(models.Model): max_length=44, validators=[UidValidator]) version = models.PositiveSmallIntegerField() owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) - mainItem = models.OneToOneField('CollectionItem', related_name='of_collection', null=True, on_delete=models.SET_NULL) class Meta: unique_together = ('uid', 'owner') @@ -37,9 +36,13 @@ class Collection(models.Model): def __str__(self): return self.uid + @cached_property + def main_item(self): + return self.items.get(uid=None) + @cached_property def content(self): - return self.mainItem.content + return self.main_item.content class CollectionItem(models.Model): diff --git a/django_etesync/serializers.py b/django_etesync/serializers.py index d4b809f..b2d6683 100644 --- a/django_etesync/serializers.py +++ b/django_etesync/serializers.py @@ -185,11 +185,9 @@ class CollectionSerializer(serializers.ModelSerializer): instance.save() main_item = models.CollectionItem.objects.create( uid=None, encryptionKey=None, version=instance.version, collection=instance) - instance.mainItem = main_item process_revisions_for_item(main_item, revision_data) - instance.save() models.CollectionMember(collection=instance, user=validated_data.get('owner'), accessLevel=models.CollectionMember.AccessLevels.ADMIN, @@ -203,7 +201,7 @@ class CollectionSerializer(serializers.ModelSerializer): revision_data = validated_data.pop('content') with transaction.atomic(): - main_item = instance.mainItem + 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. current_revision = main_item.revisions.filter(current=True).select_for_update().first()