On rajoute un champ visible pour pouvoir travailler sur l'élection sans que tout le monde puisse le voir

This commit is contained in:
Tom Hubrecht 2021-06-17 11:53:09 +02:00
parent 5c35b671ef
commit 52b5830c87
7 changed files with 77 additions and 9 deletions

View file

@ -0,0 +1,18 @@
# Generated by Django 3.2.4 on 2021-06-17 08:49
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("elections", "0027_auto_20210614_1123"),
]
operations = [
migrations.AddField(
model_name="election",
name="visible",
field=models.BooleanField(default=True, verbose_name="visible au public"),
),
]

View file

@ -0,0 +1,18 @@
# Generated by Django 3.2.4 on 2021-06-17 09:51
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("elections", "0028_election_visible"),
]
operations = [
migrations.AlterField(
model_name="election",
name="visible",
field=models.BooleanField(default=False, verbose_name="visible au public"),
),
]

View file

@ -55,12 +55,13 @@ class OpenElectionOnlyMixin(RestrictAccessMixin):
filters = super().get_filters() filters = super().get_filters()
filters[f_prefix + "start_date__lt"] = timezone.now() filters[f_prefix + "start_date__lt"] = timezone.now()
filters[f_prefix + "end_date__gt"] = timezone.now() filters[f_prefix + "end_date__gt"] = timezone.now()
filters[f_prefix + "visible"] = True
filters[f_prefix + "tallied"] = False filters[f_prefix + "tallied"] = False
filters[f_prefix + "archived"] = False filters[f_prefix + "archived"] = False
return filters return filters
class CreatorOnlyMixin(AdminOnlyMixin, RestrictAccessMixin): class CreatorOnlyMixin(AdminOnlyMixin, RestrictAccessMixin, SingleObjectMixin):
"""Restreint l'accès au créateurice de l'élection""" """Restreint l'accès au créateurice de l'élection"""
def get_next_url(self): def get_next_url(self):
@ -73,7 +74,7 @@ class CreatorOnlyMixin(AdminOnlyMixin, RestrictAccessMixin):
return filters return filters
class CreatorOnlyEditMixin(CreatorOnlyMixin, SingleObjectMixin): class CreatorOnlyEditMixin(CreatorOnlyMixin):
"""Permet au créateurice de modifier l'élection implicitement""" """Permet au créateurice de modifier l'élection implicitement"""
def get_filters(self): def get_filters(self):
@ -83,7 +84,7 @@ class CreatorOnlyEditMixin(CreatorOnlyMixin, SingleObjectMixin):
return filters return filters
class ClosedElectionMixin(CreatorOnlyMixin, SingleObjectMixin): class ClosedElectionMixin(CreatorOnlyMixin):
"""Permet d'agir sur une élection terminée""" """Permet d'agir sur une élection terminée"""
def get_filters(self): def get_filters(self):
@ -96,12 +97,15 @@ class ClosedElectionMixin(CreatorOnlyMixin, SingleObjectMixin):
class NotArchivedMixin: class NotArchivedMixin:
"""Permet de ne garder que les élections non archivées ou dont on est l'admin""" """
Permet de ne garder que les élections non archivées, et visibles
ou dont on est l'admin
"""
def get_queryset(self): def get_queryset(self):
user = self.request.user user = self.request.user
qs = super().get_queryset() qs = super().get_queryset()
if user.is_authenticated: if user.is_authenticated:
return qs.filter(Q(archived=False) | Q(created_by=user)) return qs.filter(Q(archived=False, visible=True) | Q(created_by=user))
return qs.filter(archived=False) return qs.filter(archived=False)

View file

@ -39,6 +39,8 @@ class Election(models.Model):
start_date = models.DateTimeField(_("date et heure de début")) start_date = models.DateTimeField(_("date et heure de début"))
end_date = models.DateTimeField(_("date et heure de fin")) end_date = models.DateTimeField(_("date et heure de fin"))
visible = models.BooleanField(_("visible au public"), default=False)
vote_restrictions = TranslatedFieldWithFallback( vote_restrictions = TranslatedFieldWithFallback(
models.TextField(_("conditions de vote"), blank=True) models.TextField(_("conditions de vote"), blank=True)
) )

View file

@ -66,6 +66,16 @@
<hr class="dropdown-divider"> <hr class="dropdown-divider">
{% if not election.visible %}
{# Rend l'élection visible par tout le monde #}
<a class="dropdown-item" href="{% url 'election.set-visible' election.pk %}">
<span class="icon">
<i class="fas fa-eye"></i>
</span>
<span>{% trans "Rendre l'élection visible" %}
</a>
{% endif %}
{# Téléchargement de la liste des votant·e·s #} {# Téléchargement de la liste des votant·e·s #}
<a class="dropdown-item" href="{% url 'election.export-voters' election.pk %}"> <a class="dropdown-item" href="{% url 'election.export-voters' election.pk %}">
<span class="icon"> <span class="icon">

View file

@ -31,6 +31,11 @@ urlpatterns = [
views.DeleteVoteView.as_view(), views.DeleteVoteView.as_view(),
name="election.delete-vote", name="election.delete-vote",
), ),
path(
"visible/<int:pk>",
views.ElectionSetVisibleView.as_view(),
name="election.set-visible",
),
path("update/<int:pk>", views.ElectionUpdateView.as_view(), name="election.update"), path("update/<int:pk>", views.ElectionUpdateView.as_view(), name="election.update"),
path("tally/<int:pk>", views.ElectionTallyView.as_view(), name="election.tally"), path("tally/<int:pk>", views.ElectionTallyView.as_view(), name="election.tally"),
path( path(

View file

@ -20,7 +20,6 @@ from django.views.generic import (
UpdateView, UpdateView,
View, View,
) )
from django.views.generic.detail import SingleObjectMixin
from shared.views import BackgroundUpdateView from shared.views import BackgroundUpdateView
@ -94,7 +93,19 @@ class ElectionAdminView(CreatorOnlyMixin, DetailView):
return super().get_queryset().prefetch_related("questions__options") return super().get_queryset().prefetch_related("questions__options")
class ExportVotersView(CreatorOnlyMixin, SingleObjectMixin, View): class ElectionSetVisibleView(CreatorOnlyEditMixin, BackgroundUpdateView):
model = Election
pattern_name = "election.admin"
success_message = _("Élection visible !")
def get(self, request, *args, **kwargs):
self.election = self.get_object()
self.election.visible = True
self.election.save()
return super().get(request, *args, **kwargs)
class ExportVotersView(CreatorOnlyMixin, View):
model = Election model = Election
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
@ -105,7 +116,7 @@ class ExportVotersView(CreatorOnlyMixin, SingleObjectMixin, View):
writer.writerow(["Nom", "login"]) writer.writerow(["Nom", "login"])
for v in self.get_object().voters.all(): for v in self.get_object().voters.all():
writer.writerow([v.full_name, v.username]) writer.writerow([v.full_name, v.base_username])
return response return response
@ -298,7 +309,7 @@ class ElectionChangePublicationView(ClosedElectionMixin, BackgroundUpdateView):
return super().get(request, *args, **kwargs) return super().get(request, *args, **kwargs)
class DownloadResultsView(CreatorOnlyMixin, SingleObjectMixin, View): class DownloadResultsView(CreatorOnlyMixin, View):
model = Election model = Election
def get_queryset(self): def get_queryset(self):