diff --git a/src/agenda_culturel/calendar.py b/src/agenda_culturel/calendar.py index c641801..b469a0f 100644 --- a/src/agenda_culturel/calendar.py +++ b/src/agenda_culturel/calendar.py @@ -1,5 +1,9 @@ from datetime import datetime, timedelta, date, time import calendar +from django.db.models import Q + +import logging +logger = logging.getLogger(__name__) def daterange(start, end, step=timedelta(1)): @@ -32,15 +36,8 @@ class CalendarDay: def is_today(self): return self.today - def is_event_finishing_early_in_this_day(self, event): - if event.end_day is None or event.end_time is None: - return False - if event.start_day == event.end_day: - return False - return event.end_day == self.date and event.end_time < time(8) - def add_event(self, event): - if not self.is_event_finishing_early_in_this_day(event): + if event.contains_date(self.date): self.events.append(event) if event.category is None: if not "" in self.events_by_category: @@ -91,10 +88,14 @@ class CalendarList: qs = Event.objects() else: qs = self.filter.qs - self.events = qs.filter(start_day__lte=self.c_lastdate, start_day__gte=self.c_firstdate).order_by("start_day", "start_time") - + self.events = qs.filter( + (Q(end_day__isnull=True) & Q(start_day__gte=self.c_firstdate) & Q(start_day__lte=self.c_lastdate)) | + (Q(end_day__isnull=False) & ~(Q(start_day__gt=self.c_lastdate) | Q(end_day__lt=self.c_firstdate))) + ).order_by("start_day", "start_time") + logger.error(str(len(self.events)) + " events") for e in self.events: for d in daterange(e.start_day, e.end_day): + logger.error(d) if d.__str__() in self.calendar_days: self.calendar_days[d.__str__()].add_event(e) diff --git a/src/agenda_culturel/models.py b/src/agenda_culturel/models.py index 2e74cb6..b479ae6 100644 --- a/src/agenda_culturel/models.py +++ b/src/agenda_culturel/models.py @@ -8,7 +8,10 @@ from ckeditor.fields import RichTextField from django.template.defaultfilters import date as _date -from datetime import datetime +from datetime import datetime, time, timedelta + +import logging +logger = logging.getLogger(__name__) class StaticContent(models.Model): @@ -120,6 +123,23 @@ class Event(models.Model): tags = ArrayField(models.CharField(max_length=64), verbose_name=_('Tags'), help_text=_("A list of tags that describe the event."), blank=True, null=True) + def get_consolidated_end_day(self, intuitive=True): + if intuitive: + end_day = self.get_consolidated_end_day(False) + if end_day != self.start_day and self.end_time and self.end_time < time(8): + return end_day + timedelta(days=-1) + else: + return end_day + else: + return self.end_day if self.end_day else self.start_day + + def is_single_day(self, intuitive=True): + return self.start_day == self.get_consolidated_end_day(intuitive) + + def contains_date(self, d, intuitive=True): + logger.error("et toc") + #logger.error(str(d) + " " + str(self.start_day) + " " + str(self.get_consolidated_end_day(intuitive))) + return d >= self.start_day and d <= self.get_consolidated_end_day(intuitive) def get_absolute_url(self): return reverse("view_event", kwargs={"pk": self.pk, "extra": slugify(self.title)}) diff --git a/src/agenda_culturel/templates/agenda_culturel/ephemeris-inc.html b/src/agenda_culturel/templates/agenda_culturel/ephemeris-inc.html index f5d7aa3..86458d6 100644 --- a/src/agenda_culturel/templates/agenda_culturel/ephemeris-inc.html +++ b/src/agenda_culturel/templates/agenda_culturel/ephemeris-inc.html @@ -1,7 +1,7 @@ {% load utils_extra %}
- {% if not event.end_day or event.start_day == event.end_day %} + {% if event.is_single_day %} {{ event.start_day |date:"l" }}
{{ event.start_day |date:"j" }}
{{ event.start_day |date:"F" }}
@@ -18,7 +18,7 @@ {{ event.start_day |date:"F" }} {% endif %}
au - {{ event.start_day |date:"D" }}
+ {{ event.end_day |date:"D" }}
{{ event.end_day |date:"j" }}
{{ event.end_day |date:"F" }}
{% endif %}