diff --git a/elections/forms.py b/elections/forms.py index f8d5a81..7da7185 100644 --- a/elections/forms.py +++ b/elections/forms.py @@ -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} diff --git a/elections/migrations/0011_question_type.py b/elections/migrations/0011_question_type.py new file mode 100644 index 0000000..64f4765 --- /dev/null +++ b/elections/migrations/0011_question_type.py @@ -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", + ), + ), + ] diff --git a/elections/models.py b/elections/models.py index 26559cf..7bd04e1 100644 --- a/elections/models.py +++ b/elections/models.py @@ -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", diff --git a/elections/staticdefs.py b/elections/staticdefs.py index 6699d54..73a4bcf 100644 --- a/elections/staticdefs.py +++ b/elections/staticdefs.py @@ -17,3 +17,7 @@ CONNECTION_METHODS = { "pwd": _("mot de passe"), "cas": _("CAS"), } + +QUESTION_TYPES = [ + ("assentiment", _("Assentiment")), +] diff --git a/elections/templates/elections/election_admin.html b/elections/templates/elections/election_admin.html index acf2a8c..cd5d03f 100644 --- a/elections/templates/elections/election_admin.html +++ b/elections/templates/elections/election_admin.html @@ -112,6 +112,7 @@ {% trans "Modifier" %} {% endif %} + {{ q.get_type_display }} {# Liste des options possibles #} @@ -177,6 +178,17 @@ + +
+ + + +
+
diff --git a/elections/utils.py b/elections/utils.py index 9beda7d..0c37de7 100644 --- a/elections/utils.py +++ b/elections/utils.py @@ -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 = [] diff --git a/elections/views.py b/elections/views.py index 93a7870..f37fa70 100644 --- a/elections/views.py +++ b/elections/views.py @@ -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):