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 @@