From 9281c76ddc1563bd58774b136d7707b8244ec1e5 Mon Sep 17 00:00:00 2001 From: Tom Hubrecht Date: Sat, 19 Dec 2020 23:48:18 +0100 Subject: [PATCH] =?UTF-8?q?Liste=20des=20=C3=A9lections=20cr=C3=A9=C3=A9es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- elections/mixins.py | 16 ++ elections/models.py | 9 + elections/templates/elections/base.html | 14 +- .../templates/elections/election_admin.html | 196 +++++++++--------- .../templates/elections/election_create.html | 18 +- .../templates/elections/election_list.html | 65 ++++++ .../templates/elections/election_update.html | 34 +-- elections/urls.py | 1 + elections/views.py | 20 +- 9 files changed, 243 insertions(+), 130 deletions(-) create mode 100644 elections/templates/elections/election_list.html diff --git a/elections/mixins.py b/elections/mixins.py index e69de29..e2065b2 100644 --- a/elections/mixins.py +++ b/elections/mixins.py @@ -0,0 +1,16 @@ +# TODO: +class CreatorOnlyMixin: + """Restreint l'accès au créateurice de l'élection""" + + def get_queryset(self): + # TODO: change the way we collect the user according to the model used + user = self.request.user + return super().get_queryset().filter(created_by=user) + + +class AdministratorOnlyMixin: + """Restreint l'accès aux admins""" + + +class VoterOnlyMixin: + """Restreint l'accès aux voteureuses de l'élection""" diff --git a/elections/models.py b/elections/models.py index 9723f6b..abf23c7 100644 --- a/elections/models.py +++ b/elections/models.py @@ -23,6 +23,9 @@ class Election(models.Model): archived = models.BooleanField(_("archivée"), default=False) + class Meta: + ordering = ["-start_date", "-end_date"] + class Question(models.Model): election = models.ForeignKey( @@ -34,6 +37,9 @@ class Question(models.Model): _("nombre maximal de votes reçus"), default=0 ) + class Meta: + ordering = ["id"] + class Option(models.Model): question = models.ForeignKey( @@ -46,3 +52,6 @@ class Option(models.Model): ) # For now, we store the amount of votes received after the election is tallied nb_votes = models.PositiveSmallIntegerField(_("nombre de votes reçus"), default=0) + + class Meta: + ordering = ["id"] diff --git a/elections/templates/elections/base.html b/elections/templates/elections/base.html index df0e4b9..dbf8433 100644 --- a/elections/templates/elections/base.html +++ b/elections/templates/elections/base.html @@ -38,14 +38,16 @@ {% block layout %} diff --git a/elections/templates/elections/election_admin.html b/elections/templates/elections/election_admin.html index 059c0b5..04b1e19 100644 --- a/elections/templates/elections/election_admin.html +++ b/elections/templates/elections/election_admin.html @@ -22,54 +22,56 @@
-
- {% if election.start_date > current_time %} + {% if election.start_date > current_time %} - {# Lien pour la modification #} + {# Lien pour la modification #} +
-   - {% trans "Modifier" %} + {% trans "Modifier" %} - {% elif election.end_date < current_time %} - {% if not election.tallied %} +
+ {% elif election.end_date < current_time %} + {% if not election.tallied %} - {# Lien pour le dépouillement #} + {# Lien pour le dépouillement #} + + {% else %} - {# Lien pour la publication des résultats #} + {# Lien pour la publication des résultats #} + - {# Lien pour l'archivage #} + {# Lien pour l'archivage #} + + {% endif %} + {% endif %}

@@ -79,101 +81,97 @@
{{ election.description|linebreaksbr }}
-
+{# Liste des questions #} +{% for q in election.questions.all %} +
+
+ {{ q.text }} + {% if election.start_date > current_time %} + + + + + {% trans "Supprimer" %} + + + + + + {% trans "Modifier" %} + + {% endif %} +
- {# Liste des questions #} - {% for q in election.questions.all %} -
-
- {{ q.text }} - {% if election.start_date > current_time %} -   - + {# Liste des options possibles #} + {% for o in q.options.all %} +
+ {% if election.start_date > current_time %} + - - {# Liste des options possibles #} - {% for o in q.options.all %} -
- {% if election.start_date > current_time %} -    - {% elif election.tallied %} - - - -   - {{ o.nb_votes }} -    - {% endif %} - {{ o.text }} -
- {% endfor %} - - {# Rajout d'une option #} - {% if election.start_date > current_time %} -
-
- {% csrf_token %} - -
- - - - -
-
- -
-
-
+
   + {% elif election.tallied %} + + + + + {{ o.nb_votes }} +    {% endif %} + {{ o.text }}
{% endfor %} - {# Rajout d'une question #} + {# Rajout d'une option #} {% if election.start_date > current_time %} -
-
-
- {% csrf_token %} + +
+ {% csrf_token %} -
-
- - - - -
-
- -
-
- +
+ + + + +
+
+ +
-
+ {% endif %}
+{% endfor %} + +{# Rajout d'une question #} +{% if election.start_date > current_time %} +
+
+
+ {% csrf_token %} + +
+
+ + + + +
+
+ +
+
+
+
+
+{% endif %} {% endblock %} diff --git a/elections/templates/elections/election_create.html b/elections/templates/elections/election_create.html index d07c883..84e165a 100644 --- a/elections/templates/elections/election_create.html +++ b/elections/templates/elections/election_create.html @@ -31,18 +31,16 @@

{% trans "Création d'une élection" %}


-
-
- {% csrf_token %} + + {% csrf_token %} - {% include "elections/forms/form.html" with errors=False %} + {% include "elections/forms/form.html" with errors=False %} -
-
- -
+
+
+
- -
+
+ {% endblock %} diff --git a/elections/templates/elections/election_list.html b/elections/templates/elections/election_list.html new file mode 100644 index 0000000..f1596f8 --- /dev/null +++ b/elections/templates/elections/election_list.html @@ -0,0 +1,65 @@ +{% extends "elections/base.html" %} +{% load i18n %} + + +{% block content %} + +
+
+
+

{% trans "Liste des élections créées" %}

+
+
+ +
+
+ +{% for e in election_list %} +
+
+
+ {{ e.name }} + {{ e.start_date|date:"d/m/Y H:i" }} + + + + {{ e.end_date|date:"d/m/Y H:i" }} + + {% if e.tallied %} +   + {% trans "Élection dépouillée" %} + {% endif %} + + {% if e.results_public %} +   + {% trans "Élection publiée" %} + {% endif %} + + {% if e.archived %} +   + {% trans "Élection archivée" %} + {% endif %} +
+ + + + + {% trans "" %} + +
+

+ {{ e.description|linebreaksbr }} +

+
+{% endfor %} + +{% endblock %} diff --git a/elections/templates/elections/election_update.html b/elections/templates/elections/election_update.html index 8bd1225..85d5045 100644 --- a/elections/templates/elections/election_update.html +++ b/elections/templates/elections/election_update.html @@ -31,21 +31,29 @@

{% trans "Modification d'une élection" %}


-
-
- {% csrf_token %} + + {% csrf_token %} - {% include "elections/forms/form.html" with errors=False %} + {% include "elections/forms/form.html" with errors=False %} -
-
- -
- +
+
+
- -
+ +
+ {% endblock %} diff --git a/elections/urls.py b/elections/urls.py index 096c5d0..1de4200 100644 --- a/elections/urls.py +++ b/elections/urls.py @@ -4,6 +4,7 @@ from . import views urlpatterns = [ path("create/", views.ElectionCreateView.as_view(), name="election.create"), + path("created/", views.ElectionListView.as_view(), name="election.created"), path("admin/", views.ElectionAdminView.as_view(), name="election.admin"), path("update/", views.ElectionUpdateView.as_view(), name="election.update"), path("tally/", views.ElectionTallyView.as_view(), name="election.tally"), diff --git a/elections/views.py b/elections/views.py index 6678494..07dfe4a 100644 --- a/elections/views.py +++ b/elections/views.py @@ -6,14 +6,25 @@ 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 import ( + CreateView, + DetailView, + ListView, + RedirectView, + UpdateView, +) from django.views.generic.detail import SingleObjectMixin from .forms import ElectionCreateForm, OptionFormSet +from .mixins import CreatorOnlyMixin from .models import Election, Option, Question # TODO: access control *everywhere* +# ############################################################################# +# Administration Views +# ############################################################################# + class ElectionCreateView(SuccessMessageMixin, CreateView): model = Election @@ -35,7 +46,7 @@ class ElectionCreateView(SuccessMessageMixin, CreateView): # TODO : only the creator can edit the election and view the admin panel -class ElectionAdminView(DetailView): +class ElectionAdminView(CreatorOnlyMixin, DetailView): model = Election template_name = "elections/election_admin.html" @@ -47,6 +58,11 @@ class ElectionAdminView(DetailView): return super().get_queryset().prefetch_related("questions__options") +class ElectionListView(CreatorOnlyMixin, ListView): + model = Election + template_name = "elections/election_list.html" + + class ElectionUpdateView(SuccessMessageMixin, UpdateView): model = Election fields = ["name", "description", "start_date", "end_date"]