Chunks: move to reside under the collection.

master
Tom Hacohen 4 years ago
parent 1d5baece1e
commit 393b85d3ca

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

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

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

@ -85,23 +85,22 @@ def chunk_directory_path(instance, filename):
if custom_func is not None: if custom_func is not None:
return custom_func(instance, filename) return custom_func(instance, filename)
item = instance.item col = instance.collection
col = item.collection
user_id = col.owner.id 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): class CollectionItemChunk(models.Model):
uid = models.CharField(db_index=True, blank=False, null=False, uid = models.CharField(db_index=True, blank=False, null=False,
max_length=60, validators=[UidValidator]) 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) chunkFile = models.FileField(upload_to=chunk_directory_path, max_length=150, unique=True)
def __str__(self): def __str__(self):
return self.uid return self.uid
class Meta: class Meta:
unique_together = ('item', 'uid') unique_together = ('collection', 'uid')
def generate_stoken_uid(): def generate_stoken_uid():

@ -37,7 +37,7 @@ def process_revisions_for_item(item, revision_data):
content = chunk[1] content = chunk[1]
# If the chunk already exists we assume it's fine. Otherwise, we upload it. # If the chunk already exists we assume it's fine. Otherwise, we upload it.
if chunk_obj is None: 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.chunkFile.save('IGNORED', ContentFile(content))
chunk_obj.save() chunk_obj.save()
else: else:

@ -416,7 +416,7 @@ class CollectionItemChunkViewSet(viewsets.ViewSet):
def update(self, request, *args, collection_uid=None, collection_item_uid=None, uid=None, **kwargs): 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 = 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 = { data = {
"uid": uid, "uid": uid,
@ -426,7 +426,7 @@ class CollectionItemChunkViewSet(viewsets.ViewSet):
serializer = self.get_serializer_class()(data=data) serializer = self.get_serializer_class()(data=data)
serializer.is_valid(raise_exception=True) serializer.is_valid(raise_exception=True)
try: try:
serializer.save(item=col_it) serializer.save(collection=col)
except IntegrityError: except IntegrityError:
return Response( return Response(
{"code": "chunk_exists", "detail": "Chunk already exists."}, {"code": "chunk_exists", "detail": "Chunk already exists."},
@ -441,8 +441,8 @@ class CollectionItemChunkViewSet(viewsets.ViewSet):
from django.views.static import serve from django.views.static import serve
col = get_object_or_404(self.get_collection_queryset(), main_item__uid=collection_uid) 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)
chunk = get_object_or_404(col_it.chunks, uid=uid) chunk = get_object_or_404(col.chunks, uid=uid)
filename = chunk.chunkFile.path filename = chunk.chunkFile.path
dirname = os.path.dirname(filename) dirname = os.path.dirname(filename)

Loading…
Cancel
Save