Prepare for operations on the election

This commit is contained in:
Tom Hubrecht 2020-12-19 16:57:28 +01:00
parent bc7967f01a
commit 78a96f8067
3 changed files with 105 additions and 4 deletions

View file

@ -6,9 +6,12 @@
<div class="level"> <div class="level">
<div class="level-left"> <div class="level-left">
{# Titre de l'élection #}
<div class="level-item"> <div class="level-item">
<h1 class="title">{{ election.name }}</h1> <h1 class="title">{{ election.name }}</h1>
</div> </div>
{# Dates d'ouverture de l'élection #}
<div class="level-item"> <div class="level-item">
<span class="tag is-medium is-primary">{{ election.start_date|date:"d/m/Y H:i" }}</span> <span class="tag is-medium is-primary">{{ election.start_date|date:"d/m/Y H:i" }}</span>
<span class="icon"> <span class="icon">
@ -17,8 +20,12 @@
<span class="tag is-medium is-primary">{{ election.end_date|date:"d/m/Y H:i" }}</span> <span class="tag is-medium is-primary">{{ election.end_date|date:"d/m/Y H:i" }}</span>
</div> </div>
</div> </div>
<div class="level-right"> <div class="level-right">
<div class="level-item"> <div class="level-item">
{% if election.start_date > current_time %}
{# Lien pour la modification #}
<a class="button is-light is-outlined is-primary" href="{% url 'election.update' election.pk %}"> <a class="button is-light is-outlined is-primary" href="{% url 'election.update' election.pk %}">
<span class="icon"> <span class="icon">
<i class="fas fa-edit"></i> <i class="fas fa-edit"></i>
@ -26,21 +33,57 @@
&nbsp; &nbsp;
{% trans "Modifier" %} {% trans "Modifier" %}
</a> </a>
{% elif election.end_date < current_time %}
{% if not election.tallied %}
{# Lien pour le dépouillement #}
<a class="button is-light is-outlined is-primary" href="{% url 'election.tally' election.pk %}">
<span class="icon">
<i class="fas fa-poll-h"></i>
</span>
&nbsp;
{% trans "Dépouiller" %}
</a>
{% else %}
{# Lien pour la publication des résultats #}
<a class="button is-light is-outlined is-primary" href="{% url 'election.publish' election.pk %}">
<span class="icon">
<i class="fas fa-edit"></i>
</span>
&nbsp;
{% trans "Publier" %}
</a>
{# Lien pour l'archivage #}
<a class="button is-light is-outlined is-primary" href="{% url 'election.archive' election.pk %}">
<span class="icon">
<i class="fas fa-edit"></i>
</span>
&nbsp;
{% trans "Archiver" %}
</a>
{% endif %}
{% endif %}
</div> </div>
</div> </div>
</div> </div>
<hr> <hr>
{# Description de l'élection #}
<div class="message is-primary"> <div class="message is-primary">
<div class="message-body">{{ election.description|linebreaksbr }}</div> <div class="message-body">{{ election.description|linebreaksbr }}</div>
</div> </div>
<div class="container"> <div class="container">
{# Liste des questions #}
{% for q in election.questions.all %} {% for q in election.questions.all %}
<div class="panel"> <div class="panel">
<div class="panel-heading is-size-6"> <div class="panel-heading is-size-6">
{{ q.text }}&nbsp; {{ q.text }}
{% if election.start_date > current_time %}
&nbsp;
<a class="tag is-small is-outlined is-light is-danger"> <a class="tag is-small is-outlined is-light is-danger">
<span class="icon"> <span class="icon">
<i class="fas fa-times"></i> <i class="fas fa-times"></i>
@ -53,9 +96,13 @@
</span> </span>
{% trans "Modifier" %} {% trans "Modifier" %}
</a> </a>
{% endif %}
</div> </div>
{# Liste des options possibles #}
{% for o in q.options.all %} {% for o in q.options.all %}
<div class="panel-block"> <div class="panel-block">
{% if election.start_date > current_time %}
<div class="tags has-addons"> <div class="tags has-addons">
<a class="tag is-danger" title="{% trans "Supprimer" %}"> <a class="tag is-danger" title="{% trans "Supprimer" %}">
<span class="icon"> <span class="icon">
@ -67,11 +114,17 @@
<i class="fas fa-edit"></i> <i class="fas fa-edit"></i>
</span> </span>
</a> </a>
</div> </div>&nbsp;&nbsp;
&nbsp;&nbsp;{{ o.text }} {% elif election.tallied %}
<span class="tag is-primary">
</span>
{% endif %}
{{ o.text }}
</div> </div>
{% endfor %} {% endfor %}
{# Rajout d'une option #}
{% if election.start_date > current_time %}
<form action="" method="POST"> <form action="" method="POST">
<div class="panel-block field has-addons"> <div class="panel-block field has-addons">
{% csrf_token %} {% csrf_token %}
@ -87,9 +140,12 @@
</div> </div>
</div> </div>
</form> </form>
{% endif %}
</div> </div>
{% endfor %} {% endfor %}
{# Rajout d'une question #}
{% if election.start_date > current_time %}
<div class="columns is-centered"> <div class="columns is-centered">
<div class="column is-two-thirds"> <div class="column is-two-thirds">
<form action="" method="POST"> <form action="" method="POST">
@ -109,6 +165,7 @@
</form> </form>
</div> </div>
</div> </div>
{% endif %}
</div> </div>
{% endblock %} {% endblock %}

View file

@ -6,6 +6,13 @@ urlpatterns = [
path("create/", views.ElectionCreateView.as_view(), name="election.create"), path("create/", views.ElectionCreateView.as_view(), name="election.create"),
path("admin/<int:pk>", views.ElectionAdminView.as_view(), name="election.admin"), path("admin/<int:pk>", views.ElectionAdminView.as_view(), name="election.admin"),
path("update/<int:pk>", views.ElectionUpdateView.as_view(), name="election.update"), path("update/<int:pk>", views.ElectionUpdateView.as_view(), name="election.update"),
path("tally/<int:pk>", views.ElectionTallyView.as_view(), name="election.tally"),
path(
"publish/<int:pk>", views.ElectionPublishView.as_view(), name="election.publish"
),
path(
"archive/<int:pk>", views.ElectionArchiveView.as_view(), name="election.archive"
),
path("view/<int:pk>", views.ElectionView.as_view(), name="election.view"), path("view/<int:pk>", views.ElectionView.as_view(), name="election.view"),
path("vote/<int:pk>", views.VoteView.as_view(), name="election.vote"), path("vote/<int:pk>", views.VoteView.as_view(), name="election.vote"),
] ]

View file

@ -3,8 +3,10 @@ from django.contrib.messages.views import SuccessMessageMixin
from django.db.models import Count, Prefetch from django.db.models import Count, Prefetch
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.urls import reverse from django.urls import reverse
from django.utils import timezone
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.views.generic import CreateView, DetailView, UpdateView from django.views.generic import CreateView, DetailView, RedirectView, UpdateView
from django.views.generic.detail import SingleObjectMixin
from .forms import OptionFormSet from .forms import OptionFormSet
from .models import Election, Option, Question from .models import Election, Option, Question
@ -24,6 +26,10 @@ class ElectionAdminView(DetailView):
model = Election model = Election
template_name = "elections/election_admin.html" template_name = "elections/election_admin.html"
def get_context_data(self, **kwargs):
kwargs.update({"current_time": timezone.now()})
return super().get_context_data(**kwargs)
def get_queryset(self): def get_queryset(self):
return super().get_queryset().prefetch_related("questions__options") return super().get_queryset().prefetch_related("questions__options")
@ -32,12 +38,43 @@ class ElectionUpdateView(SuccessMessageMixin, UpdateView):
model = Election model = Election
fields = ["name", "description", "start_date", "end_date"] fields = ["name", "description", "start_date", "end_date"]
success_message = _("Élection modifiée avec succès !") success_message = _("Élection modifiée avec succès !")
template_name = "elections/election_update.html"
def get_success_url(self):
return reverse("election.admin", args=[self.object.pk])
def get_queryset(self): def get_queryset(self):
# On ne peut plus modifier une élection déjà comptée # On ne peut plus modifier une élection déjà comptée
return super().get_queryset().filter(tallied=False, archived=False) return super().get_queryset().filter(tallied=False, archived=False)
class ElectionTallyView(SuccessMessageMixin, SingleObjectMixin, RedirectView):
model = Election
def get(self, request, *args, **kwargs):
election = self.get_object()
election.update(tallied=True)
return super().get(request, *args, **kwargs)
class ElectionPublishView(SuccessMessageMixin, SingleObjectMixin, RedirectView):
model = Election
def get(self, request, *args, **kwargs):
election = self.get_object()
election.update(tallied=True)
return super().get(request, *args, **kwargs)
class ElectionArchiveView(SuccessMessageMixin, SingleObjectMixin, RedirectView):
model = Election
def get(self, request, *args, **kwargs):
election = self.get_object()
election.update(tallied=True)
return super().get(request, *args, **kwargs)
class ElectionView(DetailView): class ElectionView(DetailView):
model = Election model = Election
template_name = "elections/election.html" template_name = "elections/election.html"