On crée des templates différents pour les modes de vote

This commit is contained in:
Tom Hubrecht 2021-04-16 11:01:54 +02:00
parent 49fa1c219d
commit dd32be28c0
4 changed files with 107 additions and 88 deletions

View file

@ -2,92 +2,6 @@
{% load i18n %} {% load i18n %}
{% block extra_head %}
{% if question.vote_type == 'select' %}
<script>
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('confirm-button').addEventListener('click', () => {
var $modal_body = document.getElementById('modal-body');
var selected_rows = '';
(document.querySelectorAll('.checkbox input') || []).forEach($checkbox => {
if ($checkbox.checked) {
let option_text = $checkbox.nextSibling.textContent.trim();
selected_rows += '<tr><td>' + option_text + '</td></tr>\n';
}
});
$modal_body.innerHTML = `
<table class="table is-fullwidth">
<thead>
<tr>
<th>{% trans "Option(s) selectionnée(s)" %}</th>
</tr>
</thead>
<tbody>
${selected_rows}
</tbody>
</table>`;
});
});
</script>
{% elif question.vote_type == 'rank' %}
<script>
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('confirm-button').addEventListener('click', () => {
var $modal_body = document.getElementById('modal-body');
var ranks = new Array({{ nb_options }} + 1);
(document.querySelectorAll('.control .input') || []).forEach($input => {
var rank = parseInt($input.value);
if ($input.value == '') {
rank = {{ nb_options }};
}
var option = $input.closest('.field').querySelector('.label label').innerHTML;
option = option.substring(0, option.length - 1).trim();
if (rank > 0 && rank <= {{ nb_options }}) {
ranks[rank] = (ranks[rank] || []).concat([option]); // TODO: chercher une meilleure méthode
}
});
var table_rows = '';
for (let i = 1; i <= {{ nb_options }}; i++) {
var option_list = '';
if (!(typeof ranks[i] === 'undefined')) {
for (option of ranks[i]) {
option_list += `${option}<br>`;
}
}
table_rows += `<tr><th>${i}</th><td><div>${option_list}</div></td></tr>\n`
}
$modal_body.innerHTML = `
<table class="table is-fullwidth is-striped">
<thead>
<tr>
<th>{% trans "Classement" %}</th>
<th>{% trans "Option(s) selectionnée(s)" %}</th>
</tr>
</thead>
<tbody>
${table_rows}
</tbody>
</table>`;
});
});
</script>
{% endif %}
{% endblock %}
{% block content %} {% block content %}
<div class="level"> <div class="level">
@ -111,10 +25,11 @@
<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">
{% csrf_token %} {% csrf_token %}
{% include "forms/formset.html" %} {% block vote_form %}{% endblock %}
<div class="field is-grouped is-centered"> <div class="field is-grouped is-centered">
<div class="control is-expanded"> <div class="control is-expanded">

View file

@ -0,0 +1,62 @@
{% extends "elections/vote.html" %}
{% load i18n %}
{% block extra_head %}
<script>
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('confirm-button').addEventListener('click', () => {
var $modal_body = document.getElementById('modal-body');
var ranks = new Array({{ nb_options }} + 1);
(document.querySelectorAll('.control .input') || []).forEach($input => {
var rank = parseInt($input.value);
if ($input.value == '') {
rank = {{ nb_options }};
}
var option = $input.closest('.field').querySelector('.label label').innerHTML;
option = option.substring(0, option.length - 1).trim();
if (rank > 0 && rank <= {{ nb_options }}) {
ranks[rank] = (ranks[rank] || []).concat([option]); // TODO: chercher une meilleure méthode
}
});
var table_rows = '';
for (let i = 1; i <= {{ nb_options }}; i++) {
var option_list = '';
if (!(typeof ranks[i] === 'undefined')) {
for (option of ranks[i]) {
option_list += `${option}<br>`;
}
}
table_rows += `<tr><th>${i}</th><td><div>${option_list}</div></td></tr>\n`
}
$modal_body.innerHTML = `
<table class="table is-fullwidth is-striped">
<thead>
<tr>
<th>{% trans "Classement" %}</th>
<th>{% trans "Option(s) selectionnée(s)" %}</th>
</tr>
</thead>
<tbody>
${table_rows}
</tbody>
</table>`;
});
});
</script>
{% endblock %}
{% block vote_form %}
{% include "forms/formset.html" %}
{% endblock %}

View file

@ -0,0 +1,40 @@
{% extends "elections/vote.html" %}
{% load i18n %}
{% block extra_head %}
<script>
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('confirm-button').addEventListener('click', () => {
var $modal_body = document.getElementById('modal-body');
var selected_rows = '';
(document.querySelectorAll('.checkbox input') || []).forEach($checkbox => {
if ($checkbox.checked) {
let option_text = $checkbox.nextSibling.textContent.trim();
selected_rows += '<tr><td>' + option_text + '</td></tr>\n';
}
});
$modal_body.innerHTML = `
<table class="table is-fullwidth">
<thead>
<tr>
<th>{% trans "Option(s) selectionnée(s)" %}</th>
</tr>
</thead>
<tbody>
${selected_rows}
</tbody>
</table>`;
});
});
</script>
{% endblock %}
{% block vote_form %}
{% include "forms/formset.html" %}
{% endblock %}

View file

@ -481,7 +481,6 @@ class ElectionVotersView(NotArchivedMixin, DetailView):
class VoteView(OpenElectionOnlyMixin, DetailView): class VoteView(OpenElectionOnlyMixin, DetailView):
model = Question model = Question
template_name = "elections/vote.html"
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
# Si l'utilisateur n'est pas connecté on renvoie sur la vue de l'élection # Si l'utilisateur n'est pas connecté on renvoie sur la vue de l'élection
@ -491,6 +490,9 @@ class VoteView(OpenElectionOnlyMixin, DetailView):
) )
return super().dispatch(request, *args, **kwargs) return super().dispatch(request, *args, **kwargs)
def get_template_names(self):
return [f"elections/vote/{self.object.vote_type}.html"]
def get_next_url(self): def get_next_url(self):
return reverse("election.view", args=[self.object.election.pk]) return reverse("election.view", args=[self.object.election.pk])