From 3af0ab99710545ffe18117aa40cd5c6bc0aabf31 Mon Sep 17 00:00:00 2001 From: Tom Hubrecht Date: Wed, 26 May 2021 20:12:25 +0200 Subject: [PATCH] =?UTF-8?q?On=20ne=20peut=20pas=20ouvrir=20l'=C3=A9lection?= =?UTF-8?q?=20si=20le=20mail=20d'annonce=20a=20=C3=A9t=C3=A9=20envoy=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- elections/forms.py | 9 +++++++++ elections/views.py | 13 +++++++++++++ shared/templates/forms/checkbox.html | 2 +- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/elections/forms.py b/elections/forms.py index e9da67e..b6bc9fd 100644 --- a/elections/forms.py +++ b/elections/forms.py @@ -22,6 +22,15 @@ class ElectionForm(forms.ModelForm): self.add_error( "end_date", _("Impossible de terminer l'élection avant de la commencer") ) + + if self.instance.sent_mail and not cleaned_data["restricted"]: + self.add_error( + "restricted", + _( + "Le mail d'annonce a déjà été envoyé, il n'est pas possible " + "d'ouvrir l'élection à tout le monde" + ), + ) return cleaned_data class Meta: diff --git a/elections/views.py b/elections/views.py index a318b03..69a8d74 100644 --- a/elections/views.py +++ b/elections/views.py @@ -196,9 +196,22 @@ class ElectionUpdateView(CreatorOnlyEditMixin, SuccessMessageMixin, UpdateView): success_message = _("Élection modifiée avec succès !") template_name = "elections/election_update.html" + def get_form(self, form_class=None): + form = super().get_form(form_class) + if self.object.sent_mail: + form.fields["restricted"].disabled = True + return form + def get_success_url(self): return reverse("election.admin", args=[self.object.pk]) + def form_valid(self, form): + # Si on ouvre l'élection à tout le monde, on supprime les votant·e·s + # pré-enregistré·e·s + if not form.cleaned_data["restricted"]: + self.object.registered_voters.all().delete() + return super().form_valid(form) + class DeleteVoteView(ClosedElectionMixin, FormView): model = Election diff --git a/shared/templates/forms/checkbox.html b/shared/templates/forms/checkbox.html index 01c54c1..f50ad04 100644 --- a/shared/templates/forms/checkbox.html +++ b/shared/templates/forms/checkbox.html @@ -1,6 +1,6 @@
{% if field.auto_id %} -