Create collection member when creating collection.

This commit is contained in:
Tom Hacohen 2020-02-26 20:42:28 +02:00
parent be11e3e0e6
commit 3eb79e0a04

View File

@ -24,7 +24,7 @@ from rest_framework.decorators import action as action_decorator
from rest_framework.response import Response from rest_framework.response import Response
from . import app_settings, paginators from . import app_settings, paginators
from .models import Collection, CollectionItem from .models import Collection, CollectionItem, CollectionMember
from .serializers import ( from .serializers import (
CollectionSerializer, CollectionSerializer,
CollectionItemSerializer, CollectionItemSerializer,
@ -73,7 +73,12 @@ class CollectionViewSet(BaseViewSet):
if serializer.is_valid(): if serializer.is_valid():
try: try:
with transaction.atomic(): with transaction.atomic():
serializer.save(owner=self.request.user) col = serializer.save(owner=self.request.user)
CollectionMember(collection=col,
user=self.request.user,
accessLevel=CollectionMember.AccessLevels.ADMIN,
encryptionKey=serializer.validated_data['encryptionKey']
).save()
except IntegrityError: except IntegrityError:
content = {'code': 'integrity_error'} content = {'code': 'integrity_error'}
return Response(content, status=status.HTTP_400_BAD_REQUEST) return Response(content, status=status.HTTP_400_BAD_REQUEST)