From bd365fe387c52fd5a5aa61efa87273d72edf2987 Mon Sep 17 00:00:00 2001 From: Tom Hubrecht Date: Thu, 10 Jun 2021 21:13:48 +0200 Subject: [PATCH] =?UTF-8?q?Rajoute=20le=20t=C3=A9l=C3=A9chargement=20des?= =?UTF-8?q?=20r=C3=A9sultats=20en=20un=20fichier=20texte,=20pour=20l'insta?= =?UTF-8?q?nt=20les=20votes=20par=20classement=20ne=20sont=20pas=20support?= =?UTF-8?q?=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- elections/models.py | 7 +++++++ elections/templates/elections/election_admin.html | 8 ++++++++ .../templates/elections/results/rank_export.txt | 3 +++ .../templates/elections/results/select_export.txt | 3 +++ elections/urls.py | 5 +++++ elections/views.py | 15 +++++++++++++++ 6 files changed, 41 insertions(+) create mode 100644 elections/templates/elections/results/rank_export.txt create mode 100644 elections/templates/elections/results/select_export.txt diff --git a/elections/models.py b/elections/models.py index 07d1f17..3d4fda0 100644 --- a/elections/models.py +++ b/elections/models.py @@ -3,6 +3,7 @@ from translated_fields import TranslatedFieldWithFallback from django.conf import settings from django.contrib.auth.models import AbstractUser from django.db import models, transaction +from django.template.loader import render_to_string from django.utils.translation import gettext_lazy as _ from shared.utils import choices_length @@ -112,6 +113,12 @@ class Question(models.Model): tally_function = getattr(TallyFunctions, TALLY_FUNCTIONS[self.type]) tally_function(self) + @property + def results(self): + return render_to_string( + f"elections/results/{self.vote_type}_export.txt", {"question": self} + ) + def get_formset(self): from .forms import BallotFormset # Avoid circular imports diff --git a/elections/templates/elections/election_admin.html b/elections/templates/elections/election_admin.html index fcdf5d1..0d492c0 100644 --- a/elections/templates/elections/election_admin.html +++ b/elections/templates/elections/election_admin.html @@ -128,6 +128,14 @@ {% endif %} + {# Export des résultats #} + + + + + {% trans "Télécharger les résultats" %} + + {# Archivage #} {% if not election.archived %} diff --git a/elections/templates/elections/results/rank_export.txt b/elections/templates/elections/results/rank_export.txt new file mode 100644 index 0000000..24c82a9 --- /dev/null +++ b/elections/templates/elections/results/rank_export.txt @@ -0,0 +1,3 @@ +{{ question.text }} : +{% for o in question.options.all %}- ({% if o.abbreviation %}{{ o.abbreviation }}{% else %}{{ forloop.counter }}{% endif %}) {{ o.text }}{% if not forloop.last %} +{% endif %}{% endfor %} diff --git a/elections/templates/elections/results/select_export.txt b/elections/templates/elections/results/select_export.txt new file mode 100644 index 0000000..625dc85 --- /dev/null +++ b/elections/templates/elections/results/select_export.txt @@ -0,0 +1,3 @@ +{{ question.text }} : +{% for o in question.options.all %}- {{ o.nb_votes }} {{ o.text }}{% if not forloop.last %} +{% endif %}{% endfor %} diff --git a/elections/urls.py b/elections/urls.py index 974f6fa..e065b2d 100644 --- a/elections/urls.py +++ b/elections/urls.py @@ -21,6 +21,11 @@ urlpatterns = [ views.ExportVotersView.as_view(), name="election.export-voters", ), + path( + "results/", + views.DownloadResultsView.as_view(), + name="election.download-results", + ), path( "delete-vote///", views.DeleteVoteView.as_view(), diff --git a/elections/views.py b/elections/views.py index 1214807..d63b5b7 100644 --- a/elections/views.py +++ b/elections/views.py @@ -298,6 +298,21 @@ class ElectionChangePublicationView(ClosedElectionMixin, BackgroundUpdateView): return super().get(request, *args, **kwargs) +class DownloadResultsView(CreatorOnlyMixin, SingleObjectMixin, View): + model = Election + + def get_queryset(self): + return super().get_queryset().filter(tallied=True) + + def get(self, request, *args, **kwargs): + content = "\n".join([q.results for q in self.get_object().questions.all()]) + + response = HttpResponse(content, content_type="text/plain") + response["Content-Disposition"] = "attachment; filename=results.txt" + + return response + + class ElectionArchiveView(ClosedElectionMixin, BackgroundUpdateView): model = Election pattern_name = "election.admin"