Collection remove the redundant mainItem model attr.

This commit is contained in:
Tom Hacohen 2020-04-16 11:28:49 +03:00
parent 0fbc5c104c
commit a72543f6c9
3 changed files with 23 additions and 5 deletions

View File

@ -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',
),
]

View File

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

View File

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