From d1c9d27a6542ec8701b7192a193cf7dafbd99dcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Sun, 6 Oct 2019 00:28:32 +0200 Subject: [PATCH] Rewrite home as a class-based view --- cof/urls.py | 2 +- .../templates/{ => gestioncof}/home.html | 24 +++++---------- gestioncof/views.py | 29 ++++++++++--------- 3 files changed, 23 insertions(+), 32 deletions(-) rename gestioncof/templates/{ => gestioncof}/home.html (90%) diff --git a/cof/urls.py b/cof/urls.py index 0a239c62..82d047e7 100644 --- a/cof/urls.py +++ b/cof/urls.py @@ -28,7 +28,7 @@ admin.autodiscover() urlpatterns = [ # Page d'accueil - path("", gestioncof_views.home, name="home"), + path("", gestioncof_views.HomeView.as_view(), name="home"), # Le BdA path("bda/", include("bda.urls")), # Les exports diff --git a/gestioncof/templates/home.html b/gestioncof/templates/gestioncof/home.html similarity index 90% rename from gestioncof/templates/home.html rename to gestioncof/templates/gestioncof/home.html index b14c0a82..90fa3b04 100644 --- a/gestioncof/templates/home.html +++ b/gestioncof/templates/gestioncof/home.html @@ -7,18 +7,8 @@ {% block interm_content %}
-
+
- {% if open_surveys %}

Sondages en cours

{% endif %} @@ -65,14 +55,14 @@ {% endif %}
- + {% if user.profile.is_cof %}

Divers

diff --git a/gestioncof/views.py b/gestioncof/views.py index c69f70f6..a4e7a4c7 100644 --- a/gestioncof/views.py +++ b/gestioncof/views.py @@ -6,6 +6,7 @@ import unicodecsv from custommail.shortcuts import send_custom_mail from django.contrib import messages from django.contrib.auth.decorators import login_required +from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.models import User from django.contrib.auth.views import ( LoginView as DjangoLoginView, @@ -18,7 +19,7 @@ from django.shortcuts import get_object_or_404, redirect, render from django.urls import reverse_lazy from django.utils import timezone from django.utils.translation import ugettext_lazy as _ -from django.views.generic import FormView +from django.views.generic import FormView, TemplateView from django_cas_ng.views import LogoutView as CasLogoutView from icalendar import Calendar, Event as Vevent @@ -58,20 +59,20 @@ from gestioncof.models import ( from utils.views.autocomplete import Select2QuerySetView -@login_required -def home(request): - data = { - "surveys": Survey.objects.filter(old=False).all(), - "events": Event.objects.filter(old=False).all(), - "open_surveys": Survey.objects.filter(survey_open=True, old=False).all(), - "open_events": Event.objects.filter(registration_open=True, old=False).all(), - "active_tirages": Tirage.objects.filter(active=True).all(), - "open_tirages": Tirage.objects.filter( +class HomeView(LoginRequiredMixin, TemplateView): + template_name = "gestioncof/home.html" + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context["surveys"] = Survey.objects.filter(old=False) + context["events"] = Event.objects.filter(old=False) + context["open_surveys"] = Survey.objects.filter(survey_open=True, old=False) + context["active_tirages"] = Tirage.objects.filter(active=True) + context["open_tirages"] = Tirage.objects.filter( active=True, ouverture__lte=timezone.now() - ).all(), - "now": timezone.now(), - } - return render(request, "home.html", data) + ) + context["now"] = timezone.now() + return context def login(request):