From 4dbdb3d7cfeaaa07776a49caa1a80167c767708d Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Sun, 20 Sep 2020 19:33:55 +0300 Subject: [PATCH] Invitations: gracefully error when trying to invite an already invited user. --- django_etebase/serializers.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/django_etebase/serializers.py b/django_etebase/serializers.py index b683580..3c32e10 100644 --- a/django_etebase/serializers.py +++ b/django_etebase/serializers.py @@ -17,7 +17,7 @@ import base64 from django.core.files.base import ContentFile from django.core import exceptions as django_exceptions from django.contrib.auth import get_user_model -from django.db import transaction +from django.db import IntegrityError, transaction from rest_framework import serializers, status from . import models from .utils import get_user_queryset, create_user @@ -368,7 +368,10 @@ class CollectionInvitationSerializer(BetterErrorsMixin, serializers.ModelSeriali member = collection.members.get(user=request.user) with transaction.atomic(): - return type(self).Meta.model.objects.create(**validated_data, fromMember=member) + try: + return type(self).Meta.model.objects.create(**validated_data, fromMember=member) + except IntegrityError: + raise EtebaseValidationError('invitation_exists', 'Invitation already exists') def update(self, instance, validated_data): with transaction.atomic():