diff --git a/openslides/motion/forms.py b/openslides/motion/forms.py index 989af7a6e..908347d17 100644 --- a/openslides/motion/forms.py +++ b/openslides/motion/forms.py @@ -106,6 +106,18 @@ class MotionCategoryMixin(forms.ModelForm): class MotionIdentifierMixin(forms.ModelForm): - """Mixin to let the user choose the identifier for the motion.""" + """ + Mixin to let the user choose the identifier for the motion. + """ - identifier = forms.CharField(required=False, label=ugettext_lazy("Identifier")) + identifier = forms.CharField(required=False, label=ugettext_lazy('Identifier')) + + def clean_identifier(self): + """ + Test, that the identifier is unique + """ + identifier = self.cleaned_data['identifier'] + if Motion.objects.filter(identifier=identifier).exists(): + raise forms.ValidationError(_('The Identifier is not unique.')) + else: + return identifier diff --git a/tests/motion/test_views.py b/tests/motion/test_views.py index 60405838a..94138065d 100644 --- a/tests/motion/test_views.py +++ b/tests/motion/test_views.py @@ -119,6 +119,15 @@ class TestMotionCreateView(MotionViewTestCase): response = self.staff_client.get('/motion/') self.assertContains(response, 'href="/motion/new/"', status_code=200) + def test_identifier_not_unique(self): + Motion.objects.create(identifier='foo') + response = self.admin_client.post(self.url, {'title': 'foo', + 'text': 'bar', + 'submitter': self.admin, + 'identifier': 'foo'}) + self.assertFormError(response, 'form', 'identifier', 'The Identifier is not unique.') + + class TestMotionUpdateView(MotionViewTestCase): url = '/motion/1/edit/'