From d418fd9e3d3f6c5c1230e2a2f9932918bca6ae77 Mon Sep 17 00:00:00 2001 From: Tom Hubrecht Date: Sat, 19 Dec 2020 15:04:04 +0100 Subject: [PATCH] Admin page to view an election --- .../migrations/0002_auto_20201218_1452.py | 23 ++++++++ .../migrations/0003_auto_20201218_1954.py | 23 ++++++++ elections/mixins.py | 0 elections/models.py | 6 +- .../templates/elections/election_admin.html | 58 ++++++++++++++++--- .../templates/elections/election_update.html | 51 ++++++++++++++++ elections/urls.py | 1 + elections/views.py | 15 ++++- 8 files changed, 163 insertions(+), 14 deletions(-) create mode 100644 elections/migrations/0002_auto_20201218_1452.py create mode 100644 elections/migrations/0003_auto_20201218_1954.py create mode 100644 elections/mixins.py create mode 100644 elections/templates/elections/election_update.html diff --git a/elections/migrations/0002_auto_20201218_1452.py b/elections/migrations/0002_auto_20201218_1452.py new file mode 100644 index 0000000..d58de76 --- /dev/null +++ b/elections/migrations/0002_auto_20201218_1452.py @@ -0,0 +1,23 @@ +# Generated by Django 2.2.17 on 2020-12-18 14:52 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("elections", "0001_initial"), + ] + + operations = [ + migrations.RenameField( + model_name="election", + old_name="end_time", + new_name="end", + ), + migrations.RenameField( + model_name="election", + old_name="start_time", + new_name="start", + ), + ] diff --git a/elections/migrations/0003_auto_20201218_1954.py b/elections/migrations/0003_auto_20201218_1954.py new file mode 100644 index 0000000..f9a2b2b --- /dev/null +++ b/elections/migrations/0003_auto_20201218_1954.py @@ -0,0 +1,23 @@ +# Generated by Django 2.2.17 on 2020-12-18 19:54 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("elections", "0002_auto_20201218_1452"), + ] + + operations = [ + migrations.RenameField( + model_name="election", + old_name="end", + new_name="end_date", + ), + migrations.RenameField( + model_name="election", + old_name="start", + new_name="start_date", + ), + ] diff --git a/elections/mixins.py b/elections/mixins.py new file mode 100644 index 0000000..e69de29 diff --git a/elections/models.py b/elections/models.py index 3d2056a..f75d393 100644 --- a/elections/models.py +++ b/elections/models.py @@ -1,6 +1,6 @@ +from django.contrib.auth import get_user_model from django.db import models from django.utils.translation import gettext_lazy as _ -from django.contrib.auth import get_user_model User = get_user_model() @@ -10,8 +10,8 @@ class Election(models.Model): short_name = models.SlugField(_("nom bref"), unique=True) description = models.TextField(_("description"), blank=True) - start_time = models.DateTimeField(_("date et heure de début")) - end_time = models.DateTimeField(_("date et heure de fin")) + start_date = models.DateTimeField(_("date et heure de début")) + end_date = models.DateTimeField(_("date et heure de fin")) created_by = models.ForeignKey( User, on_delete=models.SET_NULL, blank=True, null=True diff --git a/elections/templates/elections/election_admin.html b/elections/templates/elections/election_admin.html index cd39b23..efdfa65 100644 --- a/elections/templates/elections/election_admin.html +++ b/elections/templates/elections/election_admin.html @@ -4,11 +4,35 @@ {% block content %} -

{{ election.name }}

+
+
+
+

{{ election.name }}

+
+
+ {{ election.start_date|date:"d/m/Y H:i" }} + + + + {{ election.end_date|date:"d/m/Y H:i" }} +
+
+ +

-
-

{{ election.description }}

+
+
{{ election.description|linebreaksbr }}
@@ -16,15 +40,33 @@ {% for q in election.questions.all %}
{% for o in q.options.all %} diff --git a/elections/templates/elections/election_update.html b/elections/templates/elections/election_update.html new file mode 100644 index 0000000..8bd1225 --- /dev/null +++ b/elections/templates/elections/election_update.html @@ -0,0 +1,51 @@ +{% extends "elections/base.html" %} +{% load i18n static %} + + +{% block extra_head %} +{# DateTimePicker #} + + + + +{% endblock %} + +{% block content %} + +{% for error in form.non_field_errors %} +
+ {{ error }} +
+{% endfor %} + +

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

+
+ +
+
+ {% csrf_token %} + + {% include "elections/forms/form.html" with errors=False %} + +
+
+ +
+ +
+
+
+ +{% endblock %} diff --git a/elections/urls.py b/elections/urls.py index 7b179a9..b8cde62 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("admin/", views.ElectionAdminView.as_view(), name="election.admin"), path("update/", views.ElectionUpdateView.as_view(), name="election.update"), path("view/", views.ElectionView.as_view(), name="election.view"), path("vote/", views.VoteView.as_view(), name="election.vote"), diff --git a/elections/views.py b/elections/views.py index 2fe7e7a..f449797 100644 --- a/elections/views.py +++ b/elections/views.py @@ -6,7 +6,7 @@ from django.urls import reverse from django.utils.translation import gettext_lazy as _ from django.views.generic import CreateView, DetailView, UpdateView -from .forms import ElectionCreateForm, OptionFormSet +from .forms import OptionFormSet from .models import Election, Option, Question # TODO: access control *everywhere* @@ -14,14 +14,23 @@ from .models import Election, Option, Question class ElectionCreateView(SuccessMessageMixin, CreateView): model = Election - form_class = ElectionCreateForm + fields = ["name", "description", "start_date", "end_date"] template_name = "elections/election_create.html" success_message = _("Élection crée avec succès !") +# TODO : only the creator can edit the election and view the admin panel +class ElectionAdminView(DetailView): + model = Election + template_name = "elections/election_admin.html" + + def get_queryset(self): + return super().get_queryset().prefetch_related("questions__options") + + class ElectionUpdateView(SuccessMessageMixin, UpdateView): model = Election - fields = ["name", "description", "start_time", "end_time"] + fields = ["name", "description", "start_date", "end_date"] success_message = _("Élection modifiée avec succès !") def get_queryset(self):