Election creation

This commit is contained in:
Tom Hubrecht 2020-12-19 20:58:38 +01:00
parent 3387186f76
commit b284d65d3e
2 changed files with 17 additions and 3 deletions

View file

@ -29,6 +29,7 @@
{% endfor %}
<h1 class="title">{% trans "Création d'une élection" %}</h1>
<hr>
<div class="container">
<form action="" method="post">
@ -37,9 +38,9 @@
{% include "elections/forms/form.html" with errors=False %}
<div class="field">
<p class="control">
<input class="button is-fullwidth" type="submit" value={% trans "Enregistrer" %}>
</p>
<div class="control">
<button class="button is-fullwidth is-outlined is-primary is-light">{% trans "Enregistrer" %}</button>
</div>
</div>
</form>
</div>

View file

@ -4,6 +4,7 @@ from django.db.models import Count, Prefetch
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.utils import timezone
from django.utils.text import slugify
from django.utils.translation import gettext_lazy as _
from django.views.generic import CreateView, DetailView, RedirectView, UpdateView
from django.views.generic.detail import SingleObjectMixin
@ -20,6 +21,18 @@ class ElectionCreateView(SuccessMessageMixin, CreateView):
template_name = "elections/election_create.html"
success_message = _("Élection crée avec succès !")
def get_success_url(self):
return reverse("election.admin", args=[self.object.pk])
def form_valid(self, form):
# We need to add the short name and the creator od the election
form.instance.short_name = slugify(
str(form.instance.start_date.year) + "_" + form.instance.name
)[:50]
# TODO: Change this if we modify the user model
form.instance.created_by = self.request.user
return super().form_valid(form)
# TODO : only the creator can edit the election and view the admin panel
class ElectionAdminView(DetailView):