From 564483cab8df9679bef873bc29fb3b047c01c2f1 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 10 Dec 2018 15:27:55 +0100 Subject: [PATCH] core: prevent duplicate fixed_username/email --- passbook/core/forms/invitations.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/passbook/core/forms/invitations.py b/passbook/core/forms/invitations.py index f9517d6e6..8111cef6e 100644 --- a/passbook/core/forms/invitations.py +++ b/passbook/core/forms/invitations.py @@ -1,13 +1,29 @@ """passbook core invitation form""" from django import forms +from django.core.exceptions import ValidationError +from django.utils.translation import gettext as _ -from passbook.core.models import Invitation +from passbook.core.models import Invitation, User class InvitationForm(forms.ModelForm): """InvitationForm""" + def clean_fixed_username(self): + """Check if username is already used""" + username = self.cleaned_data.get('fixed_username') + if User.objects.filter(username=username).exists(): + raise ValidationError(_('Username is already in use.')) + return username + + def clean_fixed_email(self): + """Check if email is already used""" + email = self.cleaned_data.get('fixed_email') + if User.objects.filter(email=email).exists(): + raise ValidationError(_('E-Mail is already in use.')) + return email + class Meta: model = Invitation