Sith/election/views.py

623 lines
20 KiB
Python
Raw Permalink Normal View History

2024-06-24 11:07:36 +00:00
from ajax_select import make_ajax_field
from ajax_select.fields import AutoCompleteSelectField
from django import forms
2017-06-07 14:22:04 +00:00
from django.core.exceptions import PermissionDenied
from django.db import transaction
2016-12-22 16:39:32 +00:00
from django.db.models.query import QuerySet
2024-06-24 11:07:36 +00:00
from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse, reverse_lazy
from django.utils.translation import gettext_lazy as _
from django.views.generic import DetailView, ListView
from django.views.generic.edit import CreateView, DeleteView, FormView, UpdateView
2024-06-24 11:07:36 +00:00
from core.models import User
from core.views import CanCreateMixin, CanEditMixin, CanViewMixin
2024-07-21 14:16:40 +00:00
from core.views.forms import MarkdownInput, SelectDateTime
2024-06-24 11:07:36 +00:00
from election.models import Candidature, Election, ElectionList, Role, Vote
2016-12-21 23:15:16 +00:00
# Custom form field
2018-10-04 19:29:19 +00:00
2016-12-24 01:16:21 +00:00
class LimitedCheckboxField(forms.ModelMultipleChoiceField):
2024-07-12 07:34:16 +00:00
"""A `ModelMultipleChoiceField`, with a max limit of selectable inputs."""
2017-06-07 14:22:04 +00:00
2019-10-06 14:50:55 +00:00
def __init__(self, queryset, max_choice, **kwargs):
2016-12-21 23:15:16 +00:00
self.max_choice = max_choice
2016-12-22 16:39:32 +00:00
widget = forms.CheckboxSelectMultiple()
2024-06-27 12:46:43 +00:00
super().__init__(queryset, **kwargs)
2016-12-21 23:15:16 +00:00
def clean(self, value):
2024-06-27 12:46:43 +00:00
qs = super().clean(value)
2016-12-21 23:15:16 +00:00
self.validate(qs)
2016-12-22 16:39:32 +00:00
return qs
2016-12-21 23:15:16 +00:00
def validate(self, qs):
if qs.count() > self.max_choice:
2018-10-04 19:29:19 +00:00
raise forms.ValidationError(
_("You have selected too much candidates."), code="invalid"
)
2016-12-21 23:15:16 +00:00
# Forms
2016-12-24 01:16:21 +00:00
class CandidateForm(forms.ModelForm):
2024-07-12 07:34:16 +00:00
"""Form to candidate."""
2018-10-04 19:29:19 +00:00
2016-12-24 01:16:21 +00:00
class Meta:
model = Candidature
2018-10-04 19:29:19 +00:00
fields = ["user", "role", "program", "election_list"]
widgets = {"program": MarkdownInput}
2016-12-24 01:16:21 +00:00
2018-10-04 19:29:19 +00:00
user = AutoCompleteSelectField(
"users", label=_("User to candidate"), help_text=None, required=True
)
2016-12-24 01:16:21 +00:00
def __init__(self, *args, **kwargs):
2018-10-04 19:29:19 +00:00
election_id = kwargs.pop("election_id", None)
can_edit = kwargs.pop("can_edit", False)
2024-06-27 12:46:43 +00:00
super().__init__(*args, **kwargs)
2016-12-24 01:16:21 +00:00
if election_id:
2018-10-04 19:29:19 +00:00
self.fields["role"].queryset = Role.objects.filter(
election__id=election_id
).all()
self.fields["election_list"].queryset = ElectionList.objects.filter(
election__id=election_id
).all()
2016-12-24 01:16:21 +00:00
if not can_edit:
2018-10-04 19:29:19 +00:00
self.fields["user"].widget = forms.HiddenInput()
class VoteForm(forms.Form):
2016-12-21 23:15:16 +00:00
def __init__(self, election, user, *args, **kwargs):
2024-06-27 12:46:43 +00:00
super().__init__(*args, **kwargs)
2016-12-23 22:49:00 +00:00
if not election.has_voted(user):
for role in election.roles.all():
2016-12-23 21:58:54 +00:00
cand = role.candidatures
2016-12-21 23:15:16 +00:00
if role.max_choice > 1:
2018-10-04 19:29:19 +00:00
self.fields[role.title] = LimitedCheckboxField(
cand, role.max_choice, required=False
)
2016-12-21 23:15:16 +00:00
else:
2018-10-04 19:29:19 +00:00
self.fields[role.title] = forms.ModelChoiceField(
cand,
required=False,
widget=forms.RadioSelect(),
empty_label=_("Blank vote"),
)
2016-12-23 16:13:46 +00:00
class RoleForm(forms.ModelForm):
2024-07-12 07:34:16 +00:00
"""Form for creating a role."""
2018-10-04 19:29:19 +00:00
2016-12-23 16:13:46 +00:00
class Meta:
model = Role
2018-10-04 19:29:19 +00:00
fields = ["title", "election", "description", "max_choice"]
2016-12-23 16:13:46 +00:00
2016-12-24 02:06:39 +00:00
def __init__(self, *args, **kwargs):
2018-10-04 19:29:19 +00:00
election_id = kwargs.pop("election_id", None)
2024-06-27 12:46:43 +00:00
super().__init__(*args, **kwargs)
2016-12-24 02:06:39 +00:00
if election_id:
2018-10-04 19:29:19 +00:00
self.fields["election"].queryset = Election.objects.filter(
id=election_id
).all()
2016-12-24 02:06:39 +00:00
2016-12-23 16:13:46 +00:00
def clean(self):
2024-06-27 12:46:43 +00:00
cleaned_data = super().clean()
2018-10-04 19:29:19 +00:00
title = cleaned_data.get("title")
election = cleaned_data.get("election")
2016-12-23 16:13:46 +00:00
if Role.objects.filter(title=title, election=election).exists():
2018-10-04 19:29:19 +00:00
raise forms.ValidationError(
_("This role already exists for this election"), code="invalid"
)
2016-12-21 23:15:16 +00:00
class ElectionListForm(forms.ModelForm):
class Meta:
model = ElectionList
2018-10-04 19:29:19 +00:00
fields = ("title", "election")
def __init__(self, *args, **kwargs):
2018-10-04 19:29:19 +00:00
election_id = kwargs.pop("election_id", None)
2024-06-27 12:46:43 +00:00
super().__init__(*args, **kwargs)
if election_id:
2018-10-04 19:29:19 +00:00
self.fields["election"].queryset = Election.objects.filter(
id=election_id
).all()
class ElectionForm(forms.ModelForm):
class Meta:
model = Election
2018-10-04 19:29:19 +00:00
fields = [
"title",
"description",
"archived",
"start_candidature",
"end_candidature",
"start_date",
"end_date",
"edit_groups",
"view_groups",
"vote_groups",
"candidature_groups",
]
edit_groups = make_ajax_field(
Election, "edit_groups", "groups", help_text="", label=_("edit groups")
)
view_groups = make_ajax_field(
Election, "view_groups", "groups", help_text="", label=_("view groups")
)
vote_groups = make_ajax_field(
Election, "vote_groups", "groups", help_text="", label=_("vote groups")
)
candidature_groups = make_ajax_field(
Election,
"candidature_groups",
"groups",
help_text="",
label=_("candidature groups"),
)
2024-07-21 14:16:40 +00:00
start_date = forms.DateTimeField(
label=_("Start date"), widget=SelectDateTime, required=True
)
end_date = forms.DateTimeField(
label=_("End date"), widget=SelectDateTime, required=True
)
start_candidature = forms.DateTimeField(
label=_("Start candidature"), widget=SelectDateTime, required=True
)
end_candidature = forms.DateTimeField(
label=_("End candidature"), widget=SelectDateTime, required=True
2018-10-04 19:29:19 +00:00
)
2016-12-25 22:36:34 +00:00
2016-12-05 19:18:03 +00:00
# Display elections
class ElectionsListView(CanViewMixin, ListView):
2024-07-12 07:34:16 +00:00
"""A list of all non archived elections visible."""
2018-10-04 19:29:19 +00:00
2016-12-05 19:18:03 +00:00
model = Election
2017-06-07 14:43:50 +00:00
ordering = ["-id"]
paginate_by = 10
2018-10-04 19:29:19 +00:00
template_name = "election/election_list.jinja"
2016-12-05 22:49:13 +00:00
def get_queryset(self):
2024-06-27 12:46:43 +00:00
return super().get_queryset().filter(archived=False).all()
class ElectionListArchivedView(CanViewMixin, ListView):
2024-07-12 07:34:16 +00:00
"""A list of all archived elections visible."""
2018-10-04 19:29:19 +00:00
model = Election
ordering = ["-id"]
paginate_by = 10
2018-10-04 19:29:19 +00:00
template_name = "election/election_list.jinja"
def get_queryset(self):
2024-06-27 12:46:43 +00:00
return super().get_queryset().filter(archived=True).all()
2016-12-05 22:49:13 +00:00
class ElectionDetailView(CanViewMixin, DetailView):
2024-07-12 07:34:16 +00:00
"""Details an election responsability by responsability."""
2018-10-04 19:29:19 +00:00
2016-12-05 22:49:13 +00:00
model = Election
2018-10-04 19:29:19 +00:00
template_name = "election/election_detail.jinja"
2016-12-05 22:49:13 +00:00
pk_url_kwarg = "election_id"
def get(self, request, *arg, **kwargs):
2024-06-27 12:46:43 +00:00
response = super().get(request, *arg, **kwargs)
election: Election = self.get_object()
if request.user.can_edit(election) and election.is_vote_editable:
2018-10-04 19:29:19 +00:00
action = request.GET.get("action", None)
role = request.GET.get("role", None)
if action and role and Role.objects.filter(id=role).exists():
if action == "up":
Role.objects.get(id=role).up()
elif action == "down":
Role.objects.get(id=role).down()
elif action == "bottom":
Role.objects.get(id=role).bottom()
elif action == "top":
Role.objects.get(id=role).top()
2018-10-04 19:29:19 +00:00
return redirect(
reverse("election:detail", kwargs={"election_id": election.id})
2018-10-04 19:29:19 +00:00
)
return response
def get_context_data(self, **kwargs):
2024-07-12 07:34:16 +00:00
"""Add additionnal data to the template."""
2024-06-27 12:46:43 +00:00
kwargs = super().get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
kwargs["election_form"] = VoteForm(self.object, self.request.user)
kwargs["election_results"] = self.object.results
return kwargs
2016-12-20 22:08:47 +00:00
# Form view
2018-10-04 19:29:19 +00:00
class VoteFormView(CanCreateMixin, FormView):
2024-07-12 07:34:16 +00:00
"""Alows users to vote."""
2018-10-04 19:29:19 +00:00
form_class = VoteForm
2018-10-04 19:29:19 +00:00
template_name = "election/election_detail.jinja"
def dispatch(self, request, *arg, **kwargs):
2018-10-04 19:29:19 +00:00
self.election = get_object_or_404(Election, pk=kwargs["election_id"])
2024-06-27 12:46:43 +00:00
return super().dispatch(request, *arg, **kwargs)
def vote(self, election_data):
with transaction.atomic():
for role_title in election_data.keys():
# If we have a multiple choice field
if isinstance(election_data[role_title], QuerySet):
if election_data[role_title].count() > 0:
2017-06-07 17:16:55 +00:00
vote = Vote(role=election_data[role_title].first().role)
vote.save()
for el in election_data[role_title]:
vote.candidature.add(el)
# If we have a single choice
elif election_data[role_title] is not None:
vote = Vote(role=election_data[role_title].role)
2016-12-22 16:39:32 +00:00
vote.save()
vote.candidature.add(election_data[role_title])
self.election.voters.add(self.request.user)
def get_form_kwargs(self):
2024-06-27 12:46:43 +00:00
kwargs = super().get_form_kwargs()
2018-10-04 19:29:19 +00:00
kwargs["election"] = self.election
kwargs["user"] = self.request.user
return kwargs
def form_valid(self, form):
2024-07-12 07:34:16 +00:00
"""Verify that the user is part in a vote group."""
data = form.clean()
res = super(FormView, self).form_valid(form)
for grp_id in self.election.vote_groups.values_list("pk", flat=True):
if self.request.user.is_in_group(pk=grp_id):
self.vote(data)
return res
return res
def get_success_url(self, **kwargs):
2018-10-04 19:29:19 +00:00
return reverse_lazy("election:detail", kwargs={"election_id": self.election.id})
2016-12-22 00:28:51 +00:00
def get_context_data(self, **kwargs):
2024-07-12 07:34:16 +00:00
"""Add additionnal data to the template."""
2024-06-27 12:46:43 +00:00
kwargs = super().get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
kwargs["object"] = self.election
kwargs["election"] = self.election
kwargs["election_form"] = self.get_form()
2016-12-22 00:28:51 +00:00
return kwargs
# Create views
2018-10-04 19:29:19 +00:00
2016-12-24 01:16:21 +00:00
class CandidatureCreateView(CanCreateMixin, CreateView):
2024-07-12 07:34:16 +00:00
"""View dedicated to a cundidature creation."""
2018-10-04 19:29:19 +00:00
2016-12-24 01:16:21 +00:00
form_class = CandidateForm
model = Candidature
2018-10-04 19:29:19 +00:00
template_name = "election/candidate_form.jinja"
2016-12-24 01:16:21 +00:00
def dispatch(self, request, *arg, **kwargs):
2018-10-04 19:29:19 +00:00
self.election = get_object_or_404(Election, pk=kwargs["election_id"])
2024-06-27 12:46:43 +00:00
return super().dispatch(request, *arg, **kwargs)
2016-12-24 01:16:21 +00:00
def get_initial(self):
init = {}
self.can_edit = self.request.user.can_edit(self.election)
2018-10-04 19:29:19 +00:00
init["user"] = self.request.user.id
2016-12-24 01:16:21 +00:00
return init
def get_form_kwargs(self):
2024-06-27 12:46:43 +00:00
kwargs = super().get_form_kwargs()
2018-10-04 19:29:19 +00:00
kwargs["election_id"] = self.election.id
kwargs["can_edit"] = self.can_edit
2016-12-24 01:16:21 +00:00
return kwargs
def form_valid(self, form):
2024-07-12 07:34:16 +00:00
"""Verify that the selected user is in candidate group."""
2016-12-24 01:16:21 +00:00
obj = form.instance
obj.election = Election.objects.get(id=self.election.id)
2018-10-04 19:29:19 +00:00
if (obj.election.can_candidate(obj.user)) and (
obj.user == self.request.user or self.can_edit
):
2016-12-24 01:16:21 +00:00
return super(CreateView, self).form_valid(form)
raise PermissionDenied
def get_context_data(self, **kwargs):
2024-06-27 12:46:43 +00:00
kwargs = super().get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
kwargs["election"] = self.election
2016-12-24 01:16:21 +00:00
return kwargs
def get_success_url(self, **kwargs):
2018-10-04 19:29:19 +00:00
return reverse_lazy("election:detail", kwargs={"election_id": self.election.id})
2016-12-24 01:16:21 +00:00
2016-12-20 22:08:47 +00:00
class ElectionCreateView(CanCreateMixin, CreateView):
model = Election
form_class = ElectionForm
2018-10-04 19:29:19 +00:00
template_name = "core/create.jinja"
def dispatch(self, request, *args, **kwargs):
if not request.user.is_subscribed:
raise PermissionDenied
2024-06-27 12:46:43 +00:00
return super().dispatch(request, *args, **kwargs)
2016-12-24 01:16:21 +00:00
def form_valid(self, form):
2024-07-12 07:34:16 +00:00
"""Allow every user that had passed the dispatch to create an election."""
return super(CreateView, self).form_valid(form)
2016-12-24 01:16:21 +00:00
def get_success_url(self, **kwargs):
2018-10-04 19:29:19 +00:00
return reverse_lazy("election:detail", kwargs={"election_id": self.object.id})
2016-12-20 22:08:47 +00:00
class RoleCreateView(CanCreateMixin, CreateView):
model = Role
2016-12-23 16:13:46 +00:00
form_class = RoleForm
2018-10-04 19:29:19 +00:00
template_name = "core/create.jinja"
2016-12-20 22:08:47 +00:00
2016-12-24 02:06:39 +00:00
def dispatch(self, request, *arg, **kwargs):
2018-10-04 19:29:19 +00:00
self.election = get_object_or_404(Election, pk=kwargs["election_id"])
2016-12-25 21:04:31 +00:00
if not self.election.is_vote_editable:
raise PermissionDenied
2024-06-27 12:46:43 +00:00
return super().dispatch(request, *arg, **kwargs)
2016-12-24 02:06:39 +00:00
def get_initial(self):
init = {}
2018-10-04 19:29:19 +00:00
init["election"] = self.election
2016-12-24 02:06:39 +00:00
return init
2016-12-20 22:08:47 +00:00
def form_valid(self, form):
2024-07-12 07:34:16 +00:00
"""Verify that the user can edit properly."""
obj: Role = form.instance
user: User = self.request.user
2016-12-20 22:08:47 +00:00
if obj.election:
for grp_id in obj.election.edit_groups.values_list("pk", flat=True):
if user.is_in_group(pk=grp_id):
2016-12-24 01:16:21 +00:00
return super(CreateView, self).form_valid(form)
2016-12-20 22:08:47 +00:00
raise PermissionDenied
2016-12-24 02:06:39 +00:00
def get_form_kwargs(self):
2024-06-27 12:46:43 +00:00
kwargs = super().get_form_kwargs()
2018-10-04 19:29:19 +00:00
kwargs["election_id"] = self.election.id
2016-12-24 02:06:39 +00:00
return kwargs
2016-12-20 22:08:47 +00:00
def get_success_url(self, **kwargs):
2018-10-04 19:29:19 +00:00
return reverse_lazy(
"election:detail", kwargs={"election_id": self.object.election.id}
)
2016-12-20 22:08:47 +00:00
class ElectionListCreateView(CanCreateMixin, CreateView):
model = ElectionList
form_class = ElectionListForm
2018-10-04 19:29:19 +00:00
template_name = "core/create.jinja"
def dispatch(self, request, *arg, **kwargs):
2018-10-04 19:29:19 +00:00
self.election = get_object_or_404(Election, pk=kwargs["election_id"])
2016-12-24 17:29:26 +00:00
if not self.election.is_vote_editable:
raise PermissionDenied
2024-06-27 12:46:43 +00:00
return super().dispatch(request, *arg, **kwargs)
def get_initial(self):
init = {}
2018-10-04 19:29:19 +00:00
init["election"] = self.election
return init
def get_form_kwargs(self):
2024-06-27 12:46:43 +00:00
kwargs = super().get_form_kwargs()
2018-10-04 19:29:19 +00:00
kwargs["election_id"] = self.election.id
return kwargs
def form_valid(self, form):
2024-07-12 07:34:16 +00:00
"""Verify that the user can vote on this election."""
obj: ElectionList = form.instance
user: User = self.request.user
if obj.election:
for grp_id in obj.election.candidature_groups.values_list("pk", flat=True):
if user.is_in_group(pk=grp_id):
2016-12-24 01:16:21 +00:00
return super(CreateView, self).form_valid(form)
for grp_id in obj.election.edit_groups.values_list("pk", flat=True):
if user.is_in_group(pk=grp_id):
2016-12-24 01:16:21 +00:00
return super(CreateView, self).form_valid(form)
raise PermissionDenied
def get_success_url(self, **kwargs):
2018-10-04 19:29:19 +00:00
return reverse_lazy(
"election:detail", kwargs={"election_id": self.object.election.id}
)
# Update view
class ElectionUpdateView(CanEditMixin, UpdateView):
model = Election
form_class = ElectionForm
2018-10-04 19:29:19 +00:00
template_name = "core/edit.jinja"
pk_url_kwarg = "election_id"
def get_initial(self):
init = {}
try:
2018-10-04 19:29:19 +00:00
init["start_date"] = self.object.start_date.strftime("%Y-%m-%d %H:%M:%S")
2017-06-07 14:22:04 +00:00
except Exception:
pass
try:
2018-10-04 19:29:19 +00:00
init["end_date"] = self.object.end_date.strftime("%Y-%m-%d %H:%M:%S")
2017-06-07 14:22:04 +00:00
except Exception:
pass
try:
2018-10-04 19:29:19 +00:00
init["start_candidature"] = self.object.start_candidature.strftime(
"%Y-%m-%d %H:%M:%S"
)
2017-06-07 14:22:04 +00:00
except Exception:
pass
try:
2018-10-04 19:29:19 +00:00
init["end_candidature"] = self.object.end_candidature.strftime(
"%Y-%m-%d %H:%M:%S"
)
2017-06-07 14:22:04 +00:00
except Exception:
pass
return init
2016-12-24 17:29:26 +00:00
def get_success_url(self, **kwargs):
2018-10-04 19:29:19 +00:00
return reverse_lazy("election:detail", kwargs={"election_id": self.object.id})
2016-12-24 17:29:26 +00:00
class CandidatureUpdateView(CanEditMixin, UpdateView):
model = Candidature
form_class = CandidateForm
2018-10-04 19:29:19 +00:00
template_name = "core/edit.jinja"
pk_url_kwarg = "candidature_id"
2016-12-24 17:29:26 +00:00
def dispatch(self, request, *arg, **kwargs):
self.object = self.get_object()
if not self.object.role.election.is_vote_editable:
raise PermissionDenied
2024-06-27 12:46:43 +00:00
return super().dispatch(request, *arg, **kwargs)
2016-12-24 17:29:26 +00:00
def remove_fields(self):
2018-10-04 19:29:19 +00:00
self.form.fields.pop("role", None)
2016-12-24 17:29:26 +00:00
def get(self, request, *args, **kwargs):
self.form = self.get_form()
self.remove_fields()
return self.render_to_response(self.get_context_data(form=self.form))
def post(self, request, *args, **kwargs):
self.form = self.get_form()
self.remove_fields()
2018-10-04 19:29:19 +00:00
if (
request.user.is_authenticated
2018-10-04 19:29:19 +00:00
and request.user.can_edit(self.object)
and self.form.is_valid()
):
2024-06-27 12:46:43 +00:00
return super().form_valid(self.form)
2016-12-24 17:29:26 +00:00
return self.form_invalid(self.form)
def get_form_kwargs(self):
2024-06-27 12:46:43 +00:00
kwargs = super().get_form_kwargs()
2018-10-04 19:29:19 +00:00
kwargs["election_id"] = self.object.role.election.id
2016-12-24 17:29:26 +00:00
return kwargs
def get_success_url(self, **kwargs):
2018-10-04 19:29:19 +00:00
return reverse_lazy(
"election:detail", kwargs={"election_id": self.object.role.election.id}
)
2016-12-24 17:29:26 +00:00
class RoleUpdateView(CanEditMixin, UpdateView):
model = Role
form_class = RoleForm
2018-10-04 19:29:19 +00:00
template_name = "core/edit.jinja"
pk_url_kwarg = "role_id"
2016-12-24 17:29:26 +00:00
def dispatch(self, request, *arg, **kwargs):
self.object = self.get_object()
if not self.object.election.is_vote_editable:
raise PermissionDenied
2024-06-27 12:46:43 +00:00
return super().dispatch(request, *arg, **kwargs)
2016-12-24 17:29:26 +00:00
def remove_fields(self):
2018-10-04 19:29:19 +00:00
self.form.fields.pop("election", None)
2016-12-24 17:29:26 +00:00
def get(self, request, *args, **kwargs):
self.object = self.get_object()
self.form = self.get_form()
self.remove_fields()
return self.render_to_response(self.get_context_data(form=self.form))
def post(self, request, *args, **kwargs):
self.object = self.get_object()
self.form = self.get_form()
self.remove_fields()
2018-10-04 19:29:19 +00:00
if (
request.user.is_authenticated
2018-10-04 19:29:19 +00:00
and request.user.can_edit(self.object)
and self.form.is_valid()
):
2024-06-27 12:46:43 +00:00
return super().form_valid(self.form)
2016-12-24 17:29:26 +00:00
return self.form_invalid(self.form)
def get_form_kwargs(self):
2024-06-27 12:46:43 +00:00
kwargs = super().get_form_kwargs()
2018-10-04 19:29:19 +00:00
kwargs["election_id"] = self.object.election.id
2016-12-24 17:29:26 +00:00
return kwargs
def get_success_url(self, **kwargs):
2018-10-04 19:29:19 +00:00
return reverse_lazy(
"election:detail", kwargs={"election_id": self.object.election.id}
)
2016-12-24 17:29:26 +00:00
# Delete Views
class ElectionDeleteView(DeleteView):
model = Election
2018-10-04 19:29:19 +00:00
template_name = "core/delete_confirm.jinja"
pk_url_kwarg = "election_id"
def dispatch(self, request, *args, **kwargs):
if request.user.is_root:
2024-06-27 12:46:43 +00:00
return super().dispatch(request, *args, **kwargs)
raise PermissionDenied
def get_success_url(self, **kwargs):
2018-10-04 19:29:19 +00:00
return reverse_lazy("election:list")
2016-12-24 17:29:26 +00:00
class CandidatureDeleteView(CanEditMixin, DeleteView):
model = Candidature
2018-10-04 19:29:19 +00:00
template_name = "core/delete_confirm.jinja"
pk_url_kwarg = "candidature_id"
2016-12-24 17:29:26 +00:00
def dispatch(self, request, *arg, **kwargs):
self.object = self.get_object()
self.election = self.object.role.election
2017-06-07 17:16:55 +00:00
if not self.election.can_candidate or not self.election.is_vote_editable:
2016-12-24 17:29:26 +00:00
raise PermissionDenied
2024-06-27 12:46:43 +00:00
return super().dispatch(request, *arg, **kwargs)
2016-12-24 17:29:26 +00:00
def get_success_url(self, **kwargs):
2018-10-04 19:29:19 +00:00
return reverse_lazy("election:detail", kwargs={"election_id": self.election.id})
2016-12-24 17:29:26 +00:00
class RoleDeleteView(CanEditMixin, DeleteView):
model = Role
2018-10-04 19:29:19 +00:00
template_name = "core/delete_confirm.jinja"
pk_url_kwarg = "role_id"
2016-12-24 17:29:26 +00:00
def dispatch(self, request, *arg, **kwargs):
self.object = self.get_object()
self.election = self.object.election
if not self.election.is_vote_editable:
raise PermissionDenied
2024-06-27 12:46:43 +00:00
return super().dispatch(request, *arg, **kwargs)
2016-12-24 17:29:26 +00:00
def get_success_url(self, **kwargs):
2018-10-04 19:29:19 +00:00
return reverse_lazy("election:detail", kwargs={"election_id": self.election.id})
class ElectionListDeleteView(CanEditMixin, DeleteView):
model = ElectionList
2018-10-04 19:29:19 +00:00
template_name = "core/delete_confirm.jinja"
pk_url_kwarg = "list_id"
def dispatch(self, request, *args, **kwargs):
self.object = self.get_object()
self.election = self.object.election
if not self.election.is_vote_editable:
raise PermissionDenied
2024-06-27 12:46:43 +00:00
return super().dispatch(request, *args, **kwargs)
def get_success_url(self, **kwargs):
2018-10-04 19:29:19 +00:00
return reverse_lazy("election:detail", kwargs={"election_id": self.election.id})