début d'implémentation des filtres. L'apparence est moche, et les filtres ne marchent pas sur les arrayfields de tags...

This commit is contained in:
Jean-Marie Favreau 2023-11-06 23:19:50 +01:00
parent dc70973ca1
commit 201e2cef3e
10 changed files with 114 additions and 50 deletions

View File

@ -99,6 +99,14 @@ class Event(models.Model):
verbose_name = _('Event') verbose_name = _('Event')
verbose_name_plural = _('Events') verbose_name_plural = _('Events')
def get_all_tags():
tags = list(Event.objects.values_list('tags', flat = True))
uniq_tags = set()
for t in tags:
uniq_tags = uniq_tags | set(t)
return list(uniq_tags)
class EventSubmissionForm(models.Model): class EventSubmissionForm(models.Model):
url = models.URLField(max_length=512, verbose_name=_('URL'), help_text=_("URL where this event can be found.")) url = models.URLField(max_length=512, verbose_name=_('URL'), help_text=_("URL where this event can be found."))

View File

@ -8,7 +8,7 @@
<article class="day{{ day|calendar_classes:fixed_style }}" id="{{ daytag }}"> <article class="day{{ day|calendar_classes:fixed_style }}" id="{{ daytag }}">
<header> <header>
<h2><a href="{{ day.date | url_day }}">{{ day.date | date:"l j" }}</a></h2> <h2><a href="{{ day.date | url_day }}{{ filter.get_url }}">{{ day.date | date:"l j" }}</a></h2>
</header> </header>
{% if day.events %} {% if day.events %}
{% if resume %} {% if resume %}
@ -38,7 +38,7 @@
</ul> </ul>
<footer> <footer>
<div class="buttons"> <div class="buttons">
<a href="{{ day.date | url_day }}" role="button">Voir la journée <svg width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <a href="{{ day.date | url_day }}{{ filter.get_url }}" role="button">Voir la journée <svg width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<use href="{% static 'images/feather-sprite.svg' %}#chevron-right" /> <use href="{% static 'images/feather-sprite.svg' %}#chevron-right" />
</svg></a> </svg></a>
</div> </div>
@ -56,7 +56,7 @@
{% endif %} {% endif %}
<a href="{{ daytag }}" data-target="event-{{ event.id }}" onClick="toggleModal(event)">{{ event.title }}</a> <a href="{{ daytag }}" data-target="event-{{ event.id }}" onClick="toggleModal(event)">{{ event.title }}</a>
<dialog id="event-{{ event.id }}"> <dialog id="event-{{ event.id }}">
{% include "agenda_culturel/event-inc.html" with event=event display="modal" close_button=1 %} {% include "agenda_culturel/event-inc.html" with event=event display="modal" close_button=1 filter=filter %}
</dialog> </dialog>
</li> </li>
{% endfor %} {% endfor %}

View File

@ -3,7 +3,7 @@
<article class='ephemeris'> <article class='ephemeris'>
{% if not event.end_day or event.start_day == event.end_day %} {% if not event.end_day or event.start_day == event.end_day %}
{{ event.start_day |date:"l" }}<br /> {{ event.start_day |date:"l" }}<br />
<a href="{{ event.start_day | url_day }}"><span class="large">{{ event.start_day |date:"j" }}</span><br /> <a href="{{ event.start_day | url_day }}{{ filter.get_url }}"><span class="large">{{ event.start_day |date:"j" }}</span><br />
{{ event.start_day |date:"F" }}</a> {{ event.start_day |date:"F" }}</a>
{% if event.start_time %} {% if event.start_time %}
<footer> <footer>
@ -13,13 +13,13 @@
{% else %} {% else %}
du du
{{ event.start_day |date:"D" }}<br /> {{ event.start_day |date:"D" }}<br />
<a href="{{ event.start_day | url_day }}"><span class="large">{{ event.start_day |date:"j" }} </span> <a href="{{ event.start_day | url_day }}{{ filter.get_url }}"><span class="large">{{ event.start_day |date:"j" }} </span>
{% if event.start_day|date:"F" != event.end_day|date:"F" %} {% if event.start_day|date:"F" != event.end_day|date:"F" %}
{{ event.start_day |date:"F" }} {{ event.start_day |date:"F" }}
{% endif %}</a><br /> {% endif %}</a><br />
au au
{{ event.start_day |date:"D" }} <br /> {{ event.start_day |date:"D" }} <br />
<a href="{{ event.end_day | url_day }}"><span class="large">{{ event.end_day |date:"j" }}</span><br /> <a href="{{ event.end_day | url_day }}{{ filter.get_url }}"><span class="large">{{ event.end_day |date:"j" }}</span><br />
{{ event.end_day |date:"F" }}</a> {{ event.end_day |date:"F" }}</a>
{% endif %} {% endif %}
</article> </article>

View File

@ -15,7 +15,7 @@
</article> </article>
{% endif %} {% endif %}
{% if display == "in list" %} {% if display == "in list" %}
{% include "agenda_culturel/ephemeris-inc.html" with event=event %} {% include "agenda_culturel/ephemeris-inc.html" with event=event filter=filter %}
{% endif %} {% endif %}
{{ event.category | small_cat }} {{ event.category | small_cat }}
{% if event.location %}<hgroup>{% endif %} {% if event.location %}<hgroup>{% endif %}
@ -67,7 +67,7 @@
{% else %} {% else %}
<header> <header>
{% include "agenda_culturel/ephemeris-inc.html" with event=event %} {% include "agenda_culturel/ephemeris-inc.html" with event=event filter=filter %}
{{ event.category | small_cat }} {{ event.category | small_cat }}
<h1>{{ event.title }}</h1> <h1>{{ event.title }}</h1>
<p><svg width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <p><svg width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">

View File

@ -0,0 +1,12 @@
<article>
<details>
<summary role="button">Filtres</summary>
<section>
<form method="get" class="form django-form">
{% csrf_token %}<br />
{{ filter.form }}<br />
<button type="submit">Filtrer</button><br />
</form>
</section>
</details>
</article>

View File

@ -24,7 +24,8 @@
<h1>Les événements du {{ day | date:"l j F" }}</h1> <h1>Les événements du {{ day | date:"l j F" }}</h1>
{% include "agenda_culturel/filter-inc.html" with filter=filter %}
@ -32,14 +33,14 @@
<div class="navigation"> <div class="navigation">
<div> <div>
{% with day|shift_day:-1 as pred_day %} {% with day|shift_day:-1 as pred_day %}
<a role="button" href="{% url 'day_view' pred_day.year pred_day.month pred_day.day %}"><svg width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <a role="button" href="{% url 'day_view' pred_day.year pred_day.month pred_day.day %}{{ filter.get_url }}"><svg width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<use href="{% static 'images/feather-sprite.svg' %}#chevron-left" /> <use href="{% static 'images/feather-sprite.svg' %}#chevron-left" />
</svg> précédent</a> </svg> précédent</a>
{% endwith %} {% endwith %}
</div> </div>
<div class="right"> <div class="right">
{% with day|shift_day:1 as next_day %} {% with day|shift_day:1 as next_day %}
<a role="button" href="{% url 'day_view' next_day.year next_day.month next_day.day %}">suivant <svg width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <a role="button" href="{% url 'day_view' next_day.year next_day.month next_day.day %}{{ filter.get_url }}">suivant <svg width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<use href="{% static 'images/feather-sprite.svg' %}#chevron-right" /> <use href="{% static 'images/feather-sprite.svg' %}#chevron-right" />
</svg></a> </svg></a>
{% endwith %} {% endwith %}
@ -49,7 +50,7 @@
{% if events %} {% if events %}
{% for event in events %} {% for event in events %}
{% include "agenda_culturel/event-inc.html" with event=event display="in list by day" %} {% include "agenda_culturel/event-inc.html" with event=event display="in list by day" filter=filter %}
{% endfor %} {% endfor %}
{% else %} {% else %}
<article> <article>
@ -58,9 +59,6 @@
{% endif %} {% endif %}
<article> <article>
{% if calendar.today_in_calendar %} <a role="button" href="{% url 'week_view' day.year day|week %}{{ filter.get_url }}">Toute la semaine</a>
<a role="button" href="{% url 'aujourdhui' %}">Aujourd'hui</a>
{% endif %}
<a role="button" href="{% url 'week_view' day.year day|week %}">Toute la semaine</a>
</article> </article>
{% endblock %} {% endblock %}

View File

@ -10,6 +10,6 @@
{% block content %} {% block content %}
{% include "agenda_culturel/event-inc.html" with event=event display="single" %} {% include "agenda_culturel/event-inc.html" with event=event display="single" filter=filter %}
{% endblock %} {% endblock %}

View File

@ -25,16 +25,19 @@
{% block content %} {% block content %}
<h1>Événements de {{ calendar.firstdate | date:"F o" }}</h1> <h1>Événements de {{ calendar.firstdate | date:"F o" }}</h1>
{% include "agenda_culturel/filter-inc.html" with filter=filter %}
<article> <article>
<header> <header>
<div class="navigation"> <div class="navigation">
<div> <div>
<a role="button" href="{% url 'month_view' calendar.previous_month.year calendar.previous_month.month %}"><svg width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <a role="button" href="{% url 'month_view' calendar.previous_month.year calendar.previous_month.month %}{{ filter.get_url }}"><svg width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<use href="{% static 'images/feather-sprite.svg' %}#chevron-left" /> <use href="{% static 'images/feather-sprite.svg' %}#chevron-left" />
</svg> précédent</a> </svg> précédent</a>
</div> </div>
<div class="right"> <div class="right">
<a role="button" href="{% url 'month_view' calendar.next_month.year calendar.next_month.month %}">suivant <svg width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <a role="button" href="{% url 'month_view' calendar.next_month.year calendar.next_month.month %}{{ filter.get_url }}">suivant <svg width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<use href="{% static 'images/feather-sprite.svg' %}#chevron-right" /> <use href="{% static 'images/feather-sprite.svg' %}#chevron-right" />
</svg></a> </svg></a>
</div> </div>
@ -44,15 +47,15 @@
<div class="grid"> <div class="grid">
{% for d in calendar.calendar_days_list %} {% for d in calendar.calendar_days_list %}
{% if forloop.counter0|divisibleby:7 and not forloop.first %}</div><div class="grid">{% endif %} {% if forloop.counter0|divisibleby:7 and not forloop.first %}</div><div class="grid">{% endif %}
{% include "agenda_culturel/day-inc.html" with day=d resume=1 fixed_style=calendar.all_in_past %} {% include "agenda_culturel/day-inc.html" with day=d resume=1 fixed_style=calendar.all_in_past filter=filter %}
{% endfor %} {% endfor %}
</div> </div>
</div> </div>
<footer> <footer>
{% if calendar.today_in_calendar %} {% if calendar.today_in_calendar %}
<a role="button" href="{% url 'aujourdhui' %}">Aujourd'hui</a> <a role="button" href="{% url 'aujourdhui' %}{{ filter.get_url }}">Aujourd'hui</a>
<a role="button" href="{% url 'cette_semaine' %}">Cette semaine</a> <a role="button" href="{% url 'cette_semaine' %}{{ filter.get_url }}">Cette semaine</a>
{% endif %} {% endif %}
</footer> </footer>
</article> </article>

View File

@ -28,18 +28,20 @@
<h1>Les événements de la semaine {{ week }}</h1> <h1>Les événements de la semaine {{ week }}</h1>
<h2>Du {{ calendar.calendar_days_list.0.date }} au {{ calendar.calendar_days_list.6.date }} </h2> <h2>Du {{ calendar.calendar_days_list.0.date }} au {{ calendar.calendar_days_list.6.date }} </h2>
</hgroup> </hgroup>
{% include "agenda_culturel/filter-inc.html" with filter=filter %}
<article> <article>
<header> <header>
<div class="navigation"> <div class="navigation">
<div> <div>
<a role="button" href="{% url 'week_view' calendar.previous_week.year calendar.previous_week|week %}"><svg width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <a role="button" href="{% url 'week_view' calendar.previous_week.year calendar.previous_week|week %}{{ filter.get_url }}"><svg width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<use href="{% static 'images/feather-sprite.svg' %}#chevron-left" /> <use href="{% static 'images/feather-sprite.svg' %}#chevron-left" />
</svg> précédente</a> </svg> précédente</a>
</div> </div>
<div class="right"> <div class="right">
<a role="button" href="{% url 'week_view' calendar.next_week.year calendar.next_week|week %}">suivante <svg width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <a role="button" href="{% url 'week_view' calendar.next_week.year calendar.next_week|week %}{{ filter.get_url }}">suivante <svg width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<use href="{% static 'images/feather-sprite.svg' %}#chevron-right" /> <use href="{% static 'images/feather-sprite.svg' %}#chevron-right" />
</svg></a> </svg></a>
</div> </div>
@ -47,17 +49,17 @@
</header> </header>
<div class="grid" id="calendar"> <div class="grid" id="calendar">
{% for d in calendar.calendar_days_list %} {% for d in calendar.calendar_days_list %}
{% include "agenda_culturel/day-inc.html" with day=d fixed_style=calendar.all_in_past %} {% include "agenda_culturel/day-inc.html" with day=d fixed_style=calendar.all_in_past filter=filter %}
{% endfor %} {% endfor %}
</div> </div>
<footer> <footer>
{% if calendar.today_in_calendar %} {% if calendar.today_in_calendar %}
<a role="button" href="{% url 'aujourdhui' %}">Aujourd'hui</a> <a role="button" href="{% url 'aujourdhui' %}{{ filter.get_url }}">Aujourd'hui</a>
{% endif %} {% endif %}
<a role="button" href="{% url 'month_view' calendar.firstdate.year calendar.firstdate.month %}">{{ calendar.firstdate | date:"F o" }}</a> <a role="button" href="{% url 'month_view' calendar.firstdate.year calendar.firstdate.month %}{{ filter.get_url }}">{{ calendar.firstdate | date:"F o" }}</a>
{% if calendar.firstdate.month != calendar.lastdate.month %} {% if calendar.firstdate.month != calendar.lastdate.month %}
<a role="button" href="{% url 'month_view' calendar.lastdate.year calendar.lastdate.month %}">{{ calendar.lastdate | date:"F o" }}</a> <a role="button" href="{% url 'month_view' calendar.lastdate.year calendar.lastdate.month %}{{ filter.get_url }}">{{ calendar.lastdate | date:"F o" }}</a>
{% endif %} {% endif %}
</footer> </footer>
</article> </article>

View File

@ -59,10 +59,11 @@ class CalendarDay:
class CalendarList: class CalendarList:
def __init__(self, firstdate, lastdate): def __init__(self, firstdate, lastdate, filter):
self.firstdate = firstdate self.firstdate = firstdate
self.lastdate = lastdate self.lastdate = lastdate
self.now = date.today() self.now = date.today()
self.filter = filter
# start the first day of the first week # start the first day of the first week
self.c_firstdate = firstdate + timedelta(days=-firstdate.weekday()) self.c_firstdate = firstdate + timedelta(days=-firstdate.weekday())
@ -84,7 +85,11 @@ class CalendarList:
return self.lastdate < self.now return self.lastdate < self.now
def fill_calendar_days(self): def fill_calendar_days(self):
self.events = Event.objects.filter(start_day__lte=self.c_lastdate, start_day__gte=self.c_firstdate).order_by("start_day", "start_time") if self.filter is None:
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")
for e in self.events: for e in self.events:
for d in daterange(e.start_day, e.end_day): for d in daterange(e.start_day, e.end_day):
@ -112,7 +117,7 @@ class CalendarList:
class CalendarMonth(CalendarList): class CalendarMonth(CalendarList):
def __init__(self, year, month): def __init__(self, year, month, filter):
self.year = year self.year = year
self.month = month self.month = month
r = calendar.monthrange(year, month) r = calendar.monthrange(year, month)
@ -120,7 +125,7 @@ class CalendarMonth(CalendarList):
first = date(year, month, r[0]) first = date(year, month, r[0])
last = date(year, month, r[1]) last = date(year, month, r[1])
super().__init__(first, last) super().__init__(first, last, filter)
def get_month_name(self): def get_month_name(self):
return self.firstdate.strftime("%B") return self.firstdate.strftime("%B")
@ -134,14 +139,14 @@ class CalendarMonth(CalendarList):
class CalendarWeek(CalendarList): class CalendarWeek(CalendarList):
def __init__(self, year, week): def __init__(self, year, week, filter):
self.year = year self.year = year
self.week = week self.week = week
first = date.fromisocalendar(self.year, self.week, 1) first = date.fromisocalendar(self.year, self.week, 1)
last = date.fromisocalendar(self.year, self.week, 7) last = date.fromisocalendar(self.year, self.week, 7)
super().__init__(first, last) super().__init__(first, last, filter)
def next_week(self): def next_week(self):
return self.firstdate + timedelta(days=7) return self.firstdate + timedelta(days=7)
@ -150,6 +155,36 @@ class CalendarWeek(CalendarList):
return self.firstdate + timedelta(days=-7) return self.firstdate + timedelta(days=-7)
class EventFilter(django_filters.FilterSet):
tags = django_filters.MultipleChoiceFilter(choices=[(t, t) for t in Event.get_all_tags()], lookup_expr='in')
category = django_filters.ModelMultipleChoiceFilter(field_name="category__id", to_field_name='id', queryset=Category.objects.all())
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if len(args) > 0:
self.url = EventFilter.build_get_url(args[0])
else:
self.url = ""
class Meta:
model = Event
fields = ["category", "tags"]
field_labels = { 'category': "Catégories", "tags": "Étiquettes" }
def get_url(self):
return self.url
def build_get_url(get, first = "?"):
result = ""
for p in get:
if p in ["category", "tags"]:
for v in get.getlist(p):
result += first if len(result) == 0 else "&"
result += str(p) + "=" + str(v)
return result
def home(request): def home(request):
return week_view(request) return week_view(request)
@ -160,9 +195,13 @@ def month_view(request, year = None, month = None):
year = now.year year = now.year
if month is None: if month is None:
month = now.month month = now.month
cmonth = CalendarMonth(year, month)
context = {"year": year, "month": cmonth.get_month_name(), "calendar": cmonth} filtering_url = EventFilter.build_get_url(request.GET)
filter = EventFilter(request.GET, queryset=Event.objects.all())
cmonth = CalendarMonth(year, month, filter)
context = {"year": year, "month": cmonth.get_month_name(), "calendar": cmonth, "filter": filter }
return render(request, 'agenda_culturel/page-month.html', context) return render(request, 'agenda_culturel/page-month.html', context)
@ -173,9 +212,11 @@ def week_view(request, year = None, week = None):
if week is None: if week is None:
week = now.isocalendar()[1] week = now.isocalendar()[1]
cweek = CalendarWeek(year, week) filtering_url = EventFilter.build_get_url(request.GET)
filter = EventFilter(request.GET, queryset=Event.objects.all())
cweek = CalendarWeek(year, week, filter)
context = {"year": year, "week": week, "calendar": cweek} context = {"year": year, "week": week, "calendar": cweek, "filter": filter }
return render(request, 'agenda_culturel/page-week.html', context) return render(request, 'agenda_culturel/page-week.html', context)
@ -190,9 +231,12 @@ def day_view(request, year = None, month = None, day = None):
day = date(year, month, day) day = date(year, month, day)
events = Event.objects.filter(start_day__lte=day, start_day__gte=day).order_by("start_day", "start_time") filtering_url = EventFilter.build_get_url(request.GET)
# TODO
context = {"day": day, "events": events} filter = EventFilter(request.GET, Event.objects.all())
events = filter.qs.filter(start_day__lte=day, start_day__gte=day).order_by("start_day", "start_time")
context = {"day": day, "events": events, "filter": filter}
return render(request, 'agenda_culturel/page-day.html', context) return render(request, 'agenda_culturel/page-day.html', context)
@ -206,12 +250,8 @@ def tag_list(request):
nfkd_form = unicodedata.normalize('NFKD', input_str) nfkd_form = unicodedata.normalize('NFKD', input_str)
return u"".join([c for c in nfkd_form if not unicodedata.combining(c)]) return u"".join([c for c in nfkd_form if not unicodedata.combining(c)])
tags = Event.get_all_tags()
tags = list(Event.objects.values_list('tags', flat = True)) context = {"tags": sorted(tags, key=lambda x: remove_accents(x).lower())}
uniq_tags = set()
for t in tags:
uniq_tags = uniq_tags | set(t)
context = {"tags": sorted(list(uniq_tags), key=lambda x: remove_accents(x).lower())}
return render(request, 'agenda_culturel/tags.html', context) return render(request, 'agenda_culturel/tags.html', context)
@ -258,7 +298,8 @@ class EventSubmissionFormView(FormView):
class EventFilter(django_filters.FilterSet):
class EventFilterAdmin(django_filters.FilterSet):
tags = django_filters.CharFilter(lookup_expr='icontains') tags = django_filters.CharFilter(lookup_expr='icontains')
@ -280,7 +321,7 @@ class EventFilter(django_filters.FilterSet):
@login_required(login_url="/accounts/login/") @login_required(login_url="/accounts/login/")
def event_list(request): def event_list(request):
filter = EventFilter(request.GET, queryset=Event.objects.all()) filter = EventFilterAdmin(request.GET, queryset=Event.objects.all())
paginator = Paginator(filter.qs, 10) paginator = Paginator(filter.qs, 10)
page = request.GET.get('page') page = request.GET.get('page')