forbid banned user to be candidates in elections

This commit is contained in:
imperosol committed 2026-11-27 11:05:31 +01:00
1 parent 96800a249d
commit 6bb1a049c6
2 files changed
+25 -4

No files matched your search

+19 -3
View File
@@ -5,6 +5,7 @@ from typing import TYPE_CHECKING
from django import forms
from django.conf import settings
from django.core.exceptions import ValidationError
from django.db import transaction
from django.db.models import Count
from django.forms.models import ModelChoiceIterator, ModelChoiceIteratorValue
@@ -54,9 +55,7 @@ class CandidateForm(forms.ModelForm):
class Meta:
model = Candidature
fields = ["user", "role", "program", "election_list"]
labels = {
"user": _("User to candidate"),
}
labels = {"user": _("User to candidate")}
widgets = {
"program": MarkdownInput,
"user": AutoCompleteSelectUser,
@@ -71,6 +70,23 @@ class CandidateForm(forms.ModelForm):
if not can_edit:
self.fields["user"].widget = forms.HiddenInput()
def clean_user(self):
user = self.cleaned_data.get("user")
if (
user
and user.ban_groups.filter(
id__in=[
settings.SITH_GROUP_BANNED_RESPONSIBILITY_ID,
settings.SITH_GROUP_BANNED_SUBSCRIPTION_ID,
]
).exists()
):
raise ValidationError(
_("%(user)s is forbidden to run in a election.")
% {"user": user.get_full_name()}
)
return user
class VoteForm(forms.Form):
def __init__(self, election: Election, user: User, *args, **kwargs):