Rajoute le téléchargement des résultats en un fichier texte, pour l'instant les votes par classement ne sont pas supportés
This commit is contained in:
parent
6aa269c2a5
commit
bd365fe387
6 changed files with 41 additions and 0 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -128,6 +128,14 @@
|
|||
</a>
|
||||
{% endif %}
|
||||
|
||||
{# Export des résultats #}
|
||||
<a class="dropdown-item" href="{% url 'election.download-results' election.pk %}">
|
||||
<span class="icon">
|
||||
<i class="fas fa-save"></i>
|
||||
</span>
|
||||
<span>{% trans "Télécharger les résultats" %}</span>
|
||||
</a>
|
||||
|
||||
{# Archivage #}
|
||||
{% if not election.archived %}
|
||||
<a class="dropdown-item" href="{% url 'election.archive' election.pk %}">
|
||||
|
|
3
elections/templates/elections/results/rank_export.txt
Normal file
3
elections/templates/elections/results/rank_export.txt
Normal file
|
@ -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 %}
|
3
elections/templates/elections/results/select_export.txt
Normal file
3
elections/templates/elections/results/select_export.txt
Normal file
|
@ -0,0 +1,3 @@
|
|||
{{ question.text }} :
|
||||
{% for o in question.options.all %}- {{ o.nb_votes }} {{ o.text }}{% if not forloop.last %}
|
||||
{% endif %}{% endfor %}
|
|
@ -21,6 +21,11 @@ urlpatterns = [
|
|||
views.ExportVotersView.as_view(),
|
||||
name="election.export-voters",
|
||||
),
|
||||
path(
|
||||
"results/<int:pk>",
|
||||
views.DownloadResultsView.as_view(),
|
||||
name="election.download-results",
|
||||
),
|
||||
path(
|
||||
"delete-vote/<int:pk>/<int:user_pk>/<int:anchor>",
|
||||
views.DeleteVoteView.as_view(),
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in a new issue