From e05f4d4869815f15e248a13c2856909dfca5cf0e Mon Sep 17 00:00:00 2001 From: Jean-Marie Favreau Date: Fri, 29 Dec 2023 17:21:44 +0100 Subject: [PATCH] =?UTF-8?q?On=20affiche=20le=20nombre=20d'=C3=A9v=C3=A9nem?= =?UTF-8?q?ents=20aux=20m=C3=AAmes=20dates?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/agenda_culturel/calendar.py | 17 +++++++++++------ src/agenda_culturel/models.py | 9 +++++++++ .../templates/agenda_culturel/page-event.html | 8 ++++++-- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/agenda_culturel/calendar.py b/src/agenda_culturel/calendar.py index 257818d..10031e0 100644 --- a/src/agenda_culturel/calendar.py +++ b/src/agenda_culturel/calendar.py @@ -54,16 +54,20 @@ class CalendarDay: class CalendarList: - def __init__(self, firstdate, lastdate, filter): + def __init__(self, firstdate, lastdate, filter=None, exact=False): self.firstdate = firstdate self.lastdate = lastdate self.now = date.today() self.filter = filter - # start the first day of the first week - self.c_firstdate = firstdate + timedelta(days=-firstdate.weekday()) - # end the last day of the last week - self.c_lastdate = lastdate + timedelta(days=6-lastdate.weekday()) + if exact: + self.c_firstdate = self.firstdate + self.c_lastdate = self.lastdate + else: + # start the first day of the first week + self.c_firstdate = firstdate + timedelta(days=-firstdate.weekday()) + # end the last day of the last week + self.c_lastdate = lastdate + timedelta(days=6-lastdate.weekday()) # create a list of CalendarDays @@ -85,7 +89,8 @@ class CalendarList: def fill_calendar_days(self): if self.filter is None: - qs = Event.objects() + from .models import Event + qs = Event.objects.all() else: qs = self.filter.qs self.events = qs.filter( diff --git a/src/agenda_culturel/models.py b/src/agenda_culturel/models.py index 97d61e9..ee9ba72 100644 --- a/src/agenda_culturel/models.py +++ b/src/agenda_culturel/models.py @@ -16,6 +16,7 @@ from django.db.models import Q from django.template.defaultfilters import date as _date from datetime import datetime, time, timedelta +from .calendar import CalendarList import logging logger = logging.getLogger(__name__) @@ -178,6 +179,14 @@ class Event(models.Model): last = self.get_consolidated_end_day() return [first + timedelta(n) for n in range(int((last - first).days) + 1)] + + def get_nb_events_same_dates(self): + first = self.start_day + last = self.get_consolidated_end_day() + calendar = CalendarList(first, last, exact=True) + return [(len(d.events), d.date) for dstr, d in calendar.calendar_days.items()] + + def is_single_day(self, intuitive=True): return self.start_day == self.get_consolidated_end_day(intuitive) diff --git a/src/agenda_culturel/templates/agenda_culturel/page-event.html b/src/agenda_culturel/templates/agenda_culturel/page-event.html index 4188fa7..225a5b5 100644 --- a/src/agenda_culturel/templates/agenda_culturel/page-event.html +++ b/src/agenda_culturel/templates/agenda_culturel/page-event.html @@ -19,13 +19,17 @@
+ {% if event.is_single_day %}

À la même date

+ {% else %} +

Aux mêmes dates

+ {% endif %}