Fix creation of collection membership when creating collections.

master
Tom Hacohen 5 years ago
parent 7a38e26872
commit 771d2d013d

@ -56,6 +56,22 @@ class CollectionSerializer(serializers.ModelSerializer):
def get_ctag(self, obj):
return 'FIXME'
def create(self, validated_data):
"""Function that's called when this serializer creates an item"""
encryption_key = validated_data.pop('encryptionKey')
instance = self.__class__.Meta.model(**validated_data)
print(validated_data)
with transaction.atomic():
instance.save()
models.CollectionMember(collection=instance,
user=validated_data.get('owner'),
accessLevel=models.CollectionMember.AccessLevels.ADMIN,
encryptionKey=encryption_key,
).save()
return instance
class CollectionItemChunkSerializer(serializers.ModelSerializer):
class Meta:

@ -13,7 +13,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.contrib.auth import get_user_model
from django.db import IntegrityError, transaction
from django.db import IntegrityError
from django.http import Http404
from django.shortcuts import get_object_or_404
@ -24,7 +24,7 @@ from rest_framework.decorators import action as action_decorator
from rest_framework.response import Response
from . import app_settings, paginators
from .models import Collection, CollectionItem, CollectionMember
from .models import Collection, CollectionItem
from .serializers import (
CollectionSerializer,
CollectionItemSerializer,
@ -72,13 +72,7 @@ class CollectionViewSet(BaseViewSet):
serializer = self.serializer_class(data=request.data)
if serializer.is_valid():
try:
with transaction.atomic():
col = serializer.save(owner=self.request.user)
CollectionMember(collection=col,
user=self.request.user,
accessLevel=CollectionMember.AccessLevels.ADMIN,
encryptionKey=serializer.validated_data['encryptionKey']
).save()
serializer.save(owner=self.request.user)
except IntegrityError:
content = {'code': 'integrity_error'}
return Response(content, status=status.HTTP_400_BAD_REQUEST)

Loading…
Cancel
Save