From cc00391504889799fae3ca9670e53978e445443b Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Thu, 20 Feb 2020 22:41:39 +0200 Subject: [PATCH] Rename Snapshot to Revision --- .../migrations/0011_auto_20200220_2037.py | 17 ++++++++++++++++ .../migrations/0012_auto_20200220_2038.py | 19 ++++++++++++++++++ django_etesync/models.py | 6 +++--- django_etesync/serializers.py | 20 +++++++++---------- django_etesync/views.py | 10 +++++----- 5 files changed, 54 insertions(+), 18 deletions(-) create mode 100644 django_etesync/migrations/0011_auto_20200220_2037.py create mode 100644 django_etesync/migrations/0012_auto_20200220_2038.py diff --git a/django_etesync/migrations/0011_auto_20200220_2037.py b/django_etesync/migrations/0011_auto_20200220_2037.py new file mode 100644 index 0000000..2d790748 --- /dev/null +++ b/django_etesync/migrations/0011_auto_20200220_2037.py @@ -0,0 +1,17 @@ +# Generated by Django 3.0.3 on 2020-02-20 20:37 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('django_etesync', '0010_auto_20200220_1248'), + ] + + operations = [ + migrations.RenameModel( + old_name='CollectionItemSnapshot', + new_name='CollectionItemRevision', + ), + ] diff --git a/django_etesync/migrations/0012_auto_20200220_2038.py b/django_etesync/migrations/0012_auto_20200220_2038.py new file mode 100644 index 0000000..2657973 --- /dev/null +++ b/django_etesync/migrations/0012_auto_20200220_2038.py @@ -0,0 +1,19 @@ +# Generated by Django 3.0.3 on 2020-02-20 20:38 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('django_etesync', '0011_auto_20200220_2037'), + ] + + operations = [ + migrations.AlterField( + model_name='collectionitemrevision', + name='item', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='revisions', to='django_etesync.CollectionItem'), + ), + ] diff --git a/django_etesync/models.py b/django_etesync/models.py index 37c0dc1..0de3f90 100644 --- a/django_etesync/models.py +++ b/django_etesync/models.py @@ -50,7 +50,7 @@ class CollectionItem(models.Model): @cached_property def content(self): - return self.snapshots.get(current=True) + return self.revisions.get(current=True) def chunk_directory_path(instance, filename): @@ -75,10 +75,10 @@ class CollectionItemChunk(models.Model): return self.uid -class CollectionItemSnapshot(models.Model): +class CollectionItemRevision(models.Model): version = models.PositiveSmallIntegerField() encryptionKey = models.BinaryField(editable=True, blank=False, null=False) - item = models.ForeignKey(CollectionItem, related_name='snapshots', on_delete=models.CASCADE) + item = models.ForeignKey(CollectionItem, related_name='revisions', on_delete=models.CASCADE) chunks = models.ManyToManyField(CollectionItemChunk, related_name='items') hmac = models.CharField(max_length=50, blank=False, null=False) current = models.BooleanField(db_index=True, default=True) diff --git a/django_etesync/serializers.py b/django_etesync/serializers.py index a4d6fbe..de49c98 100644 --- a/django_etesync/serializers.py +++ b/django_etesync/serializers.py @@ -64,7 +64,7 @@ class CollectionItemChunkSerializer(serializers.ModelSerializer): fields = ('uid', 'chunkFile') -class CollectionItemSnapshotBaseSerializer(serializers.ModelSerializer): +class CollectionItemRevisionBaseSerializer(serializers.ModelSerializer): encryptionKey = BinaryBase64Field() chunks = serializers.SlugRelatedField( slug_field='uid', @@ -73,15 +73,15 @@ class CollectionItemSnapshotBaseSerializer(serializers.ModelSerializer): ) class Meta: - model = models.CollectionItemSnapshot + model = models.CollectionItemRevision fields = ('version', 'encryptionKey', 'chunks', 'hmac') -class CollectionItemSnapshotSerializer(CollectionItemSnapshotBaseSerializer): +class CollectionItemRevisionSerializer(CollectionItemRevisionBaseSerializer): chunksUrls = serializers.SerializerMethodField('get_chunks_urls') - class Meta(CollectionItemSnapshotBaseSerializer.Meta): - fields = CollectionItemSnapshotBaseSerializer.Meta.fields + ('chunksUrls', ) + class Meta(CollectionItemRevisionBaseSerializer.Meta): + fields = CollectionItemRevisionBaseSerializer.Meta.fields + ('chunksUrls', ) # FIXME: currently the user is exposed in the url. We don't want that, and we can probably avoid that but still save it under the user. # We would probably be better off just let the user calculate the urls from the uid and a base url for the snapshot. @@ -94,11 +94,11 @@ class CollectionItemSnapshotSerializer(CollectionItemSnapshotBaseSerializer): return ret -class CollectionItemSnapshotInlineSerializer(CollectionItemSnapshotBaseSerializer): +class CollectionItemRevisionInlineSerializer(CollectionItemRevisionBaseSerializer): chunksData = serializers.SerializerMethodField('get_chunks_data') - class Meta(CollectionItemSnapshotBaseSerializer.Meta): - fields = CollectionItemSnapshotBaseSerializer.Meta.fields + ('chunksData', ) + class Meta(CollectionItemRevisionBaseSerializer.Meta): + fields = CollectionItemRevisionBaseSerializer.Meta.fields + ('chunksData', ) def get_chunks_data(self, obj): ret = [] @@ -109,7 +109,7 @@ class CollectionItemSnapshotInlineSerializer(CollectionItemSnapshotBaseSerialize return ret class CollectionItemSerializer(serializers.ModelSerializer): - content = CollectionItemSnapshotSerializer(read_only=True, many=False) + content = CollectionItemRevisionSerializer(read_only=True, many=False) class Meta: model = models.CollectionItem @@ -117,4 +117,4 @@ class CollectionItemSerializer(serializers.ModelSerializer): class CollectionItemInlineSerializer(CollectionItemSerializer): - content = CollectionItemSnapshotInlineSerializer(read_only=True, many=False) + content = CollectionItemRevisionInlineSerializer(read_only=True, many=False) diff --git a/django_etesync/views.py b/django_etesync/views.py index 8e21e3c..d8604cc 100644 --- a/django_etesync/views.py +++ b/django_etesync/views.py @@ -34,8 +34,8 @@ from .serializers import ( CollectionSerializer, CollectionItemSerializer, CollectionItemInlineSerializer, - CollectionItemSnapshotSerializer, - CollectionItemSnapshotInlineSerializer, + CollectionItemRevisionSerializer, + CollectionItemRevisionInlineSerializer, CollectionItemChunkSerializer ) @@ -115,7 +115,7 @@ class CollectionItemViewSet(BaseViewSet): collection = self.get_collection_queryset(Collection.objects).get(uid=collection_uid) except Collection.DoesNotExist: raise Http404("Collection does not exist") - # XXX Potentially add this for performance: .prefetch_related('snapshots__chunks') + # XXX Potentially add this for performance: .prefetch_related('revisions__chunks') queryset = type(self).queryset.filter(collection__pk=collection.pk) return queryset @@ -149,11 +149,11 @@ class CollectionItemViewSet(BaseViewSet): return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED) @action_decorator(detail=True, methods=['GET']) - def snapshots(self, request, collection_uid=None, uid=None): + def revision(self, request, collection_uid=None, uid=None): col = get_object_or_404(Collection.objects, uid=collection_uid) col_it = get_object_or_404(col.items, uid=uid) - serializer = CollectionItemSnapshotSerializer(col_it.snapshots.order_by('-id'), many=True) + serializer = CollectionItemRevisionSerializer(col_it.revisions.order_by('-id'), many=True) return Response(serializer.data)