From 422b62d5b2ef184fe115c6e08f9cbbd733a858e5 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Sun, 1 Nov 2020 11:17:44 +0200 Subject: [PATCH] Disallow creating new collections without a collection type set. --- django_etebase/serializers.py | 12 +++--------- django_etebase/views.py | 2 +- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/django_etebase/serializers.py b/django_etebase/serializers.py index 1634d9f..97dcd64 100644 --- a/django_etebase/serializers.py +++ b/django_etebase/serializers.py @@ -273,8 +273,7 @@ class CollectionListMultiSerializer(BetterErrorsMixin, serializers.Serializer): class CollectionSerializer(BetterErrorsMixin, serializers.ModelSerializer): collectionKey = CollectionEncryptionKeyField() - # FIXME: make required once "collection-type-migration" is done - collectionType = CollectionTypeField(required=False) + collectionType = CollectionTypeField() accessLevel = serializers.SerializerMethodField('get_access_level_from_context') stoken = serializers.CharField(read_only=True) @@ -293,8 +292,7 @@ class CollectionSerializer(BetterErrorsMixin, serializers.ModelSerializer): def create(self, validated_data): """Function that's called when this serializer creates an item""" collection_key = validated_data.pop('collectionKey') - # FIXME: remove the None fallback once "collection-type-migration" is done - collection_type = validated_data.pop('collectionType', None) + collection_type = validated_data.pop('collectionType') user = validated_data.get('owner') main_item_data = validated_data.pop('main_item') @@ -318,11 +316,7 @@ class CollectionSerializer(BetterErrorsMixin, serializers.ModelSerializer): process_revisions_for_item(main_item, revision_data) - # FIXME: remove the if statement (and else branch) once "collection-type-migration" is done - if collection_type is not None: - collection_type_obj, _ = models.CollectionType.objects.get_or_create(uid=collection_type, owner=user) - else: - collection_type_obj = None + collection_type_obj, _ = models.CollectionType.objects.get_or_create(uid=collection_type, owner=user) models.CollectionMember(collection=instance, stoken=models.Stoken.objects.create(), diff --git a/django_etebase/views.py b/django_etebase/views.py index 3c40e17..a60346b 100644 --- a/django_etebase/views.py +++ b/django_etebase/views.py @@ -221,7 +221,7 @@ class CollectionViewSet(BaseViewSet): collection_types = serializer.validated_data['collectionTypes'] queryset = self.get_queryset() - # FIXME: Remove the isnull part once "collection-type-migration" is done + # FIXME: Remove the isnull part once we attach collection types to all objects ("collection-type-migration") queryset = queryset.filter( Q(members__collectionType__uid__in=collection_types) | Q(members__collectionType__isnull=True))