From 393b85d3ca23513a5ea7a61fdeab94f33c7eb490 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Tue, 4 Aug 2020 15:19:45 +0300 Subject: [PATCH] Chunks: move to reside under the collection. --- .../0023_collectionitemchunk_collection.py | 19 +++++++++++++ .../migrations/0024_auto_20200804_1209.py | 22 +++++++++++++++ .../migrations/0025_auto_20200804_1216.py | 27 +++++++++++++++++++ django_etebase/models.py | 9 +++---- django_etebase/serializers.py | 2 +- django_etebase/views.py | 8 +++--- 6 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 django_etebase/migrations/0023_collectionitemchunk_collection.py create mode 100644 django_etebase/migrations/0024_auto_20200804_1209.py create mode 100644 django_etebase/migrations/0025_auto_20200804_1216.py diff --git a/django_etebase/migrations/0023_collectionitemchunk_collection.py b/django_etebase/migrations/0023_collectionitemchunk_collection.py new file mode 100644 index 0000000..b5d6841 --- /dev/null +++ b/django_etebase/migrations/0023_collectionitemchunk_collection.py @@ -0,0 +1,19 @@ +# Generated by Django 3.0.3 on 2020-08-04 12:08 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('django_etebase', '0022_auto_20200804_1059'), + ] + + operations = [ + migrations.AddField( + model_name='collectionitemchunk', + name='collection', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='chunks', to='django_etebase.Collection'), + ), + ] diff --git a/django_etebase/migrations/0024_auto_20200804_1209.py b/django_etebase/migrations/0024_auto_20200804_1209.py new file mode 100644 index 0000000..54c80a3 --- /dev/null +++ b/django_etebase/migrations/0024_auto_20200804_1209.py @@ -0,0 +1,22 @@ +# Generated by Django 3.0.3 on 2020-08-04 12:09 + +from django.db import migrations + + +def change_chunk_to_collections(apps, schema_editor): + CollectionItemChunk = apps.get_model('django_etebase', 'CollectionItemChunk') + + for chunk in CollectionItemChunk.objects.all(): + chunk.collection = chunk.item.collection + chunk.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('django_etebase', '0023_collectionitemchunk_collection'), + ] + + operations = [ + migrations.RunPython(change_chunk_to_collections), + ] diff --git a/django_etebase/migrations/0025_auto_20200804_1216.py b/django_etebase/migrations/0025_auto_20200804_1216.py new file mode 100644 index 0000000..8849f53 --- /dev/null +++ b/django_etebase/migrations/0025_auto_20200804_1216.py @@ -0,0 +1,27 @@ +# Generated by Django 3.0.3 on 2020-08-04 12:16 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('django_etebase', '0024_auto_20200804_1209'), + ] + + operations = [ + migrations.AlterField( + model_name='collectionitemchunk', + name='collection', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='chunks', to='django_etebase.Collection'), + ), + migrations.AlterUniqueTogether( + name='collectionitemchunk', + unique_together={('collection', 'uid')}, + ), + migrations.RemoveField( + model_name='collectionitemchunk', + name='item', + ), + ] diff --git a/django_etebase/models.py b/django_etebase/models.py index 0c33301..403d2b7 100644 --- a/django_etebase/models.py +++ b/django_etebase/models.py @@ -85,23 +85,22 @@ def chunk_directory_path(instance, filename): if custom_func is not None: return custom_func(instance, filename) - item = instance.item - col = item.collection + col = instance.collection user_id = col.owner.id - return Path('user_{}'.format(user_id), col.uid, item.uid, instance.uid) + return Path('user_{}'.format(user_id), col.uid, instance.uid) class CollectionItemChunk(models.Model): uid = models.CharField(db_index=True, blank=False, null=False, max_length=60, validators=[UidValidator]) - item = models.ForeignKey(CollectionItem, related_name='chunks', on_delete=models.CASCADE) + collection = models.ForeignKey(Collection, related_name='chunks', on_delete=models.CASCADE) chunkFile = models.FileField(upload_to=chunk_directory_path, max_length=150, unique=True) def __str__(self): return self.uid class Meta: - unique_together = ('item', 'uid') + unique_together = ('collection', 'uid') def generate_stoken_uid(): diff --git a/django_etebase/serializers.py b/django_etebase/serializers.py index 0e44228..8cfddad 100644 --- a/django_etebase/serializers.py +++ b/django_etebase/serializers.py @@ -37,7 +37,7 @@ def process_revisions_for_item(item, revision_data): content = chunk[1] # If the chunk already exists we assume it's fine. Otherwise, we upload it. if chunk_obj is None: - chunk_obj = models.CollectionItemChunk(uid=uid, item=item) + chunk_obj = models.CollectionItemChunk(uid=uid, collection=item.collection) chunk_obj.chunkFile.save('IGNORED', ContentFile(content)) chunk_obj.save() else: diff --git a/django_etebase/views.py b/django_etebase/views.py index c299dc2..2678451 100644 --- a/django_etebase/views.py +++ b/django_etebase/views.py @@ -416,7 +416,7 @@ class CollectionItemChunkViewSet(viewsets.ViewSet): def update(self, request, *args, collection_uid=None, collection_item_uid=None, uid=None, **kwargs): col = get_object_or_404(self.get_collection_queryset(), main_item__uid=collection_uid) - col_it = get_object_or_404(col.items, uid=collection_item_uid) + # IGNORED FOR NOW: col_it = get_object_or_404(col.items, uid=collection_item_uid) data = { "uid": uid, @@ -426,7 +426,7 @@ class CollectionItemChunkViewSet(viewsets.ViewSet): serializer = self.get_serializer_class()(data=data) serializer.is_valid(raise_exception=True) try: - serializer.save(item=col_it) + serializer.save(collection=col) except IntegrityError: return Response( {"code": "chunk_exists", "detail": "Chunk already exists."}, @@ -441,8 +441,8 @@ class CollectionItemChunkViewSet(viewsets.ViewSet): from django.views.static import serve col = get_object_or_404(self.get_collection_queryset(), main_item__uid=collection_uid) - col_it = get_object_or_404(col.items, uid=collection_item_uid) - chunk = get_object_or_404(col_it.chunks, uid=uid) + # IGNORED FOR NOW: col_it = get_object_or_404(col.items, uid=collection_item_uid) + chunk = get_object_or_404(col.chunks, uid=uid) filename = chunk.chunkFile.path dirname = os.path.dirname(filename)