From b2bc3ad43e363c6ee7735dd834ec8bf4a2eb5154 Mon Sep 17 00:00:00 2001 From: henu Date: Sat, 14 Oct 2017 21:33:51 +0300 Subject: [PATCH] Prevent creating custom kaehmy role with existing name --- webapp/forms.py | 10 +++++++++- webapp/views.py | 3 ++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/webapp/forms.py b/webapp/forms.py index 4dc78a9..6ca9737 100644 --- a/webapp/forms.py +++ b/webapp/forms.py @@ -38,7 +38,15 @@ class KaehmyForm_Form(forms.ModelForm): if number.isdigit(): return number else: - raise ValidationError(_('Invalid value')) + raise ValidationError(_('Invalid phone number')) + + def clean_custom_role_name(self): + """Check that no other custom role with same name exists.""" + custom_name = self.cleaned_data.get('custom_role_name') + if CustomKaehmyRole.objects.get(name=custom_name) is None: + return custom_name + else: + raise ValidationError(_('Custom role with the same name already exists.')) class KaehmyCommentForm(forms.ModelForm): diff --git a/webapp/views.py b/webapp/views.py index c0bc21f..c51907d 100644 --- a/webapp/views.py +++ b/webapp/views.py @@ -235,7 +235,8 @@ def kaehmy_comment(request, *args, **kwargs): 'Käy lukemassa viesti osoitteessa http://sika.sahkoinsinoorikilta.fi/kaehmy').format(name.capitalize()) send_email(email, subject, body) - logging.debug('Sent kaehmy comment email to recipient <{}>'.format(email)) + logging.debug( + 'Sent kaehmy comment email to recipient <{}>'.format(email)) return redirect('/kaehmy') else: