diff --git a/election/forms.py b/election/forms.py
index 944222ed..00e76ead 100644
--- a/election/forms.py
+++ b/election/forms.py
@@ -1,6 +1,9 @@
from django import forms
from django.utils.translation import gettext_lazy as _
+from club.forms import ClubRoleChoiceField
+from club.models import ClubRole
+from club.widgets.ajax_select import AutoCompleteSelectMultipleClub
from core.models import User
from core.views.forms import SelectDateTime
from core.views.widgets.ajax_select import (
@@ -79,18 +82,20 @@ class VoteForm(forms.Form):
class RoleForm(forms.ModelForm):
"""Form for creating a role."""
+ required_css_class = "required"
+ error_css_class = "error"
+
class Meta:
model = Role
- fields = ["title", "election", "description", "max_choice"]
- widgets = {"election": AutoCompleteSelect}
+ fields = ["club_role", "title", "description", "max_choice"]
+ field_classes = {"club_role": ClubRoleChoiceField}
- def __init__(self, *args, **kwargs):
- election_id = kwargs.pop("election_id", None)
+ def __init__(self, *args, election: Election, **kwargs):
super().__init__(*args, **kwargs)
- if election_id:
- self.fields["election"].queryset = Election.objects.filter(
- id=election_id
- ).all()
+ self.instance.election = election
+ self.fields["club_role"].queryset = ClubRole.objects.filter(
+ is_board=True, club__in=election.clubs.all()
+ )
def clean(self):
cleaned_data = super().clean()
@@ -108,21 +113,21 @@ class ElectionListForm(forms.ModelForm):
fields = ("title", "election")
widgets = {"election": AutoCompleteSelect}
- def __init__(self, *args, **kwargs):
- election_id = kwargs.pop("election_id", None)
+ def __init__(self, *args, election: Election, **kwargs):
super().__init__(*args, **kwargs)
- if election_id:
- self.fields["election"].queryset = Election.objects.filter(
- id=election_id
- ).all()
+ self.instance.election = election
class ElectionForm(forms.ModelForm):
+ required_css_class = "required"
+ error_css_class = "error"
+
class Meta:
model = Election
fields = [
"title",
"description",
+ "clubs",
"archived",
"start_candidature",
"end_candidature",
@@ -134,21 +139,13 @@ class ElectionForm(forms.ModelForm):
"candidature_groups",
]
widgets = {
+ "clubs": AutoCompleteSelectMultipleClub,
"edit_groups": AutoCompleteSelectMultipleGroup,
"view_groups": AutoCompleteSelectMultipleGroup,
"vote_groups": AutoCompleteSelectMultipleGroup,
"candidature_groups": AutoCompleteSelectMultipleGroup,
+ "start_date": SelectDateTime,
+ "end_date": SelectDateTime,
+ "start_candidature": SelectDateTime,
+ "end_candidature": SelectDateTime,
}
-
- 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
- )
diff --git a/election/templates/election/role_form.jinja b/election/templates/election/role_form.jinja
new file mode 100644
index 00000000..44ad3e4d
--- /dev/null
+++ b/election/templates/election/role_form.jinja
@@ -0,0 +1,53 @@
+{% extends "core/base.jinja" %}
+
+{% block title %}
+ {% trans name=object_name %}Election role{% endtrans %}
+{% endblock %}
+
+{% block content %}
+ {% if object %}
+
{% trans election=election %}Create role for election "{{ election }}"{% endtrans %}
+ {% else %}
+ {% trans election=election %}Edit role for election "{{ election }}"{% endtrans %}
+ {% endif %}
+
+{% endblock %}
+
+{% block script %}
+
+{% endblock %}
\ No newline at end of file
diff --git a/election/views.py b/election/views.py
index 63cd70d9..67b89e78 100644
--- a/election/views.py
+++ b/election/views.py
@@ -219,7 +219,7 @@ class ElectionCreateView(PermissionRequiredMixin, CreateView):
class RoleCreateView(LoginRequiredMixin, UserPassesTestMixin, CreateView):
model = Role
form_class = RoleForm
- template_name = "core/create.jinja"
+ template_name = "election/role_form.jinja"
@cached_property
def election(self):
@@ -234,16 +234,14 @@ class RoleCreateView(LoginRequiredMixin, UserPassesTestMixin, CreateView):
id__in=self.request.user.all_groups
).exists()
- def get_initial(self):
- return {"election": self.election}
-
def get_form_kwargs(self):
- return super().get_form_kwargs() | {"election_id": self.election.id}
+ return super().get_form_kwargs() | {"election": self.election}
def get_success_url(self, **kwargs):
- return reverse(
- "election:detail", kwargs={"election_id": self.object.election_id}
- )
+ return reverse("election:detail", kwargs={"election_id": self.election.id})
+
+ def get_context_data(self, **kwargs):
+ return super().get_context_data(**kwargs) | {"election": self.election}
class ElectionListCreateView(LoginRequiredMixin, UserPassesTestMixin, CreateView):
@@ -267,16 +265,11 @@ class ElectionListCreateView(LoginRequiredMixin, UserPassesTestMixin, CreateView
)
return not groups.isdisjoint(self.request.user.all_groups.keys())
- def get_initial(self):
- return {"election": self.election}
-
def get_form_kwargs(self):
- return super().get_form_kwargs() | {"election_id": self.election.id}
+ return super().get_form_kwargs() | {"election": self.election}
def get_success_url(self, **kwargs):
- return reverse(
- "election:detail", kwargs={"election_id": self.object.election_id}
- )
+ return reverse("election:detail", kwargs={"election_id": self.election.id})
# Update view
@@ -288,18 +281,6 @@ class ElectionUpdateView(CanEditMixin, UpdateView):
template_name = "core/edit.jinja"
pk_url_kwarg = "election_id"
- def get_initial(self):
- return {
- "start_date": self.object.start_date.strftime("%Y-%m-%d %H:%M:%S"),
- "end_date": self.object.end_date.strftime("%Y-%m-%d %H:%M:%S"),
- "start_candidature": self.object.start_candidature.strftime(
- "%Y-%m-%d %H:%M:%S"
- ),
- "end_candidature": self.object.end_candidature.strftime(
- "%Y-%m-%d %H:%M:%S"
- ),
- }
-
def get_success_url(self, **kwargs):
return reverse_lazy("election:detail", kwargs={"election_id": self.object.id})
@@ -327,7 +308,7 @@ class CandidatureUpdateView(LoginRequiredMixin, CanEditMixin, UpdateView):
class RoleUpdateView(CanEditMixin, UpdateView):
model = Role
form_class = RoleForm
- template_name = "core/edit.jinja"
+ template_name = "election/role_form.jinja"
pk_url_kwarg = "role_id"
def dispatch(self, request, *arg, **kwargs):
@@ -336,19 +317,14 @@ class RoleUpdateView(CanEditMixin, UpdateView):
raise PermissionDenied
return super().dispatch(request, *arg, **kwargs)
- def remove_fields(self):
- self.form.fields.pop("election", None)
-
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()
if (
request.user.is_authenticated
and request.user.can_edit(self.object)
@@ -359,7 +335,7 @@ class RoleUpdateView(CanEditMixin, UpdateView):
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
- kwargs["election_id"] = self.object.election.id
+ kwargs["election"] = self.object.election
return kwargs
def get_success_url(self, **kwargs):
@@ -367,6 +343,9 @@ class RoleUpdateView(CanEditMixin, UpdateView):
"election:detail", kwargs={"election_id": self.object.election.id}
)
+ def get_context_data(self, **kwargs):
+ return super().get_context_data(**kwargs) | {"election": self.object.election}
+
# Delete Views