On rajoute la possibilité d'avoir plusieurs types de question

This commit is contained in:
Tom Hubrecht 2021-03-19 11:48:38 +01:00
parent 39adc92c68
commit 84199c38fe
7 changed files with 62 additions and 5 deletions

View file

@ -50,7 +50,7 @@ class VoterMailForm(forms.Form):
class QuestionForm(forms.ModelForm):
class Meta:
model = Question
fields = ["text"]
fields = ["text", "type"]
widgets = {"text": forms.TextInput}

View file

@ -0,0 +1,23 @@
# Generated by Django 2.2.19 on 2021-03-19 10:33
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("elections", "0010_blank_voters"),
]
operations = [
migrations.AddField(
model_name="question",
name="type",
field=models.CharField(
choices=[("assentiment", "Assentiment")],
default="assentiment",
max_length=11,
verbose_name="type de question",
),
),
]

View file

@ -3,7 +3,8 @@ from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.translation import gettext_lazy as _
from .staticdefs import CONNECTION_METHODS
from .staticdefs import CONNECTION_METHODS, QUESTION_TYPES
from .utils import choices_length
# #############################################################################
# Models regarding an election
@ -54,6 +55,12 @@ class Question(models.Model):
Election, related_name="questions", on_delete=models.CASCADE
)
text = models.TextField(_("question"), blank=False)
type = models.CharField(
_("type de question"),
choices=QUESTION_TYPES,
default="assentiment",
max_length=choices_length(QUESTION_TYPES),
)
# We cache the maximum number of votes for an option
max_votes = models.PositiveSmallIntegerField(
_("nombre maximal de votes reçus"), default=0
@ -74,6 +81,7 @@ class Option(models.Model):
Question, related_name="options", on_delete=models.CASCADE
)
text = models.TextField(_("texte"), blank=False)
voters = models.ManyToManyField(
settings.AUTH_USER_MODEL,
related_name="votes",

View file

@ -17,3 +17,7 @@ CONNECTION_METHODS = {
"pwd": _("mot de passe"),
"cas": _("CAS"),
}
QUESTION_TYPES = [
("assentiment", _("Assentiment")),
]

View file

@ -112,6 +112,7 @@
<span>{% trans "Modifier" %}</span>
</a>
{% endif %}
<span class="tag is-outlined is-primary is-light is-pulled-right">{{ q.get_type_display }}</span>
</div>
{# Liste des options possibles #}
@ -177,6 +178,17 @@
<i class="fas fa-question"></i>
</span>
</div>
<div class="control">
<span class="select is-primary">
<select name="type" id="id_type">
{% for val, choice in question_types %}
<option value="{{ val }}">{{ choice }}</option>
{% endfor %}
</select>
</span>
</div>
<div class="control">
<button class="button is-primary is-outlined">{% trans "Valider" %}</button>
</div>

View file

@ -8,7 +8,13 @@ from django.core.mail import EmailMessage, get_connection
from django.core.validators import validate_email
from django.utils.translation import gettext_lazy as _
from .models import User
def choices_length(choices):
"""Renvoie la longueur maximale des choix de choices"""
m = 0
for c in choices:
m = max(m, len(c[0]))
return m
def create_users(election, csv_file):
@ -91,6 +97,8 @@ def send_mail(election, mail_form):
"""Envoie le mail d'annonce de l'élection avec identifiants et mot de passe
aux votant·e·s, le mdp est généré en même temps que le mail est envoyé.
"""
from .models import User
voters = list(election.registered_voters.all())
url = f"https://kadenios.eleves.ens.fr/elections/view/{election.id}"
messages = []

View file

@ -26,7 +26,7 @@ from .forms import (
)
from .mixins import CreatorOnlyEditMixin, CreatorOnlyMixin, OpenElectionOnlyMixin
from .models import Election, Option, Question
from .staticdefs import MAIL_VOTERS
from .staticdefs import MAIL_VOTERS, QUESTION_TYPES
from .utils import create_users, send_mail
# TODO: access control *everywhere*
@ -81,7 +81,9 @@ class ElectionAdminView(CreatorOnlyMixin, DetailView):
return reverse("election.view", args=[self.object.pk])
def get_context_data(self, **kwargs):
kwargs.update({"current_time": timezone.now()})
kwargs.update(
{"current_time": timezone.now(), "question_types": QUESTION_TYPES}
)
return super().get_context_data(**kwargs)
def get_queryset(self):