From ab9d66fcc0da829ce3bed28d1d436ea48206a168 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Wed, 15 Apr 2020 16:53:31 +0300 Subject: [PATCH] Implement collection creation. --- django_etesync/serializers.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/django_etesync/serializers.py b/django_etesync/serializers.py index 1b87a86..4d8a206 100644 --- a/django_etesync/serializers.py +++ b/django_etesync/serializers.py @@ -14,6 +14,7 @@ import base64 +from django.core.files.base import ContentFile from django.contrib.auth import get_user_model from django.db import transaction from django.utils.crypto import get_random_string @@ -164,14 +165,28 @@ class CollectionSerializer(serializers.ModelSerializer): instance = self.__class__.Meta.model(**validated_data) with transaction.atomic(): + instance.save() main_item = models.CollectionItem.objects.create( uid=None, encryptionKey=None, version=instance.version, collection=instance) instance.mainItem = main_item + chunks_ids = [] chunks = revision_data.pop('chunks') - revision = models.CollectionItemRevision.objects.create(**revision_data, uid=generate_rev_uid(), - item=main_item) - revision.chunks.set(chunks) + for chunk in chunks: + uid = chunk[0] + if len(chunk) > 1: + content = chunk[1] + # FIXME: fix order! + chunk = models.CollectionItemChunk(uid=uid, item=main_item, order='abc') + chunk.chunkFile.save('IGNORED', ContentFile(content)) + chunk.save() + chunks_ids.append(chunk.id) + else: + chunk = models.CollectionItemChunk.objects.get(uid=uid) + chunks_ids.append(chunk.id) + + revision = models.CollectionItemRevision.objects.create(**revision_data, item=main_item) + revision.chunks.set(chunks_ids) instance.save() models.CollectionMember(collection=instance,