Ajout d'une fonction de recherche (perfectible)

This commit is contained in:
Jean-Marie Favreau 2023-11-10 22:45:17 +01:00
parent 6e80291df8
commit 5dd1125a68
7 changed files with 121 additions and 10 deletions

View File

@ -208,4 +208,7 @@ msgid "next month"
msgstr "le mois prochain"
msgid "Add another"
msgstr "Ajouter une autre"
msgstr "Ajouter une autre"
msgid "Search"
msgstr "Rechercher"

View File

@ -23,6 +23,7 @@
{% endblock %}
</head>
{% load event_extra %}
{% load utils_extra %}
<body>
<nav class="container-fluid">
<ul>
@ -37,6 +38,9 @@
</li>
</ul>
<ul>
<li>
<a href="{% url 'event_search' %}">Rechercher {% picto_from_name "search" %}</a>
</li>
<li>
<details role="list" dir="rtl">
<summary aria-haspopup="listbox" role="link">Maintenant</summary>

View File

@ -0,0 +1,57 @@
{% extends "agenda_culturel/page.html" %}
{% block title %}Rechercher un évnement{% endblock %}
{% block content %}
<article>
<header>
<h1>Rechercher un événement</h1>
</header>
<form method="get" class="form django-form">
{% for field in filter.form %}
<div>
{{ field.errors }}
{{ field.label_tag }} {{ field }}
</div>
{% if forloop.first %}
<details>
<summary>Rechercher avancée</summary>
{% endif %}
{% if forloop.last %}
</details>
{% endif %}
{% endfor %}
<button type="submit">Rechercher</button>
</form>
<ul>
{% for obj in paginator_filter %}
<li><a href="{{ obj.get_absolute_url }}">{{ obj }}</a></li>
{% endfor %}
</ul>
<footer>
<span>
{% if paginator_filter.has_previous %}
<a href="?page=1" role="button">&laquo; premier</a>
<a href="?page={{ paginator_filter.previous_page_number }}" role="button">précédent</a>
{% endif %}
<span>
Page {{ paginator_filter.number }} sur {{ paginator_filter.paginator.num_pages }}
</span>
{% if paginator_filter.has_next %}
<a href="?page={{ paginator_filter.next_page_number }}" role="button">suivant</a>
<a href="?page={{ paginator_filter.paginator.num_pages }}" role="button">dernier &raquo;</a>
{% endif %}
</span>
</footer>
</article>
{% endblock %}

View File

@ -1,10 +1,11 @@
from django import template
from django.utils.safestring import mark_safe
from django.templatetags.static import static
from agenda_culturel.models import Event
from django.db.models import Q
from .utils_extra import picto_from_name
register = template.Library()
@register.filter
@ -25,12 +26,6 @@ def need_complete_display(event, display):
return event.end_day and event.end_day != event.start_day and (event.start_time or event.end_time or display == "in list by day")
def picto_from_name(name, datatooltip=""):
return mark_safe('<span data-tooltip="' + datatooltip + '"><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") + '#' + name + '" />' + \
'</svg></span>')
@register.filter
def picto_status(event):
if event.is_draft():

View File

@ -4,6 +4,7 @@ from django.utils.safestring import mark_safe
from urllib.parse import urlparse
from datetime import timedelta, date
from django.urls import reverse_lazy
from django.templatetags.static import static
register = template.Library()
@ -54,4 +55,10 @@ def calendar_classes(d, fixed_style):
@register.filter
def url_day(d):
return reverse_lazy("day_view", kwargs={"year": d.year, "month": d.month, "day": d.day})
return reverse_lazy("day_view", kwargs={"year": d.year, "month": d.month, "day": d.day})
@register.simple_tag
def picto_from_name(name, datatooltip=""):
return mark_safe('<span data-tooltip="' + datatooltip + '"><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") + '#' + name + '" />' + \
'</svg></span>')

View File

@ -27,7 +27,8 @@ urlpatterns = [
path('accounts/', include('django.contrib.auth.urls')),
path("test_app/", include("test_app.urls")),
path("static-content/create", StaticContentCreateView.as_view(), name="create_static_content"),
path("static-content/<int:pk>/edit", StaticContentUpdateView.as_view(), name="edit_static_content")
path("static-content/<int:pk>/edit", StaticContentUpdateView.as_view(), name="edit_static_content"),
path('rechercher/', event_search, name='event_search')
]
if settings.DEBUG:

View File

@ -401,3 +401,47 @@ def event_list(request):
response = paginator.page(paginator.num_pages)
return render(request, 'agenda_culturel/list.html', {'filter': filter, 'paginator_filter': response})
class SearchEventFilter(django_filters.FilterSet):
tags = django_filters.CharFilter(lookup_expr='icontains')
title = django_filters.CharFilter(lookup_expr='contains')
location = django_filters.CharFilter(lookup_expr='contains')
description = django_filters.CharFilter(lookup_expr='contains')
start_day = django_filters.DateFromToRangeFilter(widget=django_filters.widgets.RangeWidget(attrs={'type': 'date'}))
q = django_filters.CharFilter(method='custom_filter', label=_("Search"))
o = django_filters.OrderingFilter(
# tuple-mapping retains order
fields=(
('title', 'title'),
('description', 'description'),
('start_day', 'start_day'),
),
)
def custom_filter(self, queryset, name, value):
return queryset.filter(
Q(title__contains=value) | Q(location__contains=value) | Q(description__contains=value))
class Meta:
model = Event
fields = ['q', 'title', 'location', 'description', 'category', 'tags', 'start_day']
def event_search(request):
filter = SearchEventFilter(request.GET, queryset=get_event_qs(request).order_by("-start_day"))
paginator = Paginator(filter.qs, 10)
page = request.GET.get('page')
try:
response = paginator.page(page)
except PageNotAnInteger:
response = paginator.page(1)
except EmptyPage:
response = paginator.page(paginator.num_pages)
return render(request, 'agenda_culturel/search.html', {'filter': filter, 'paginator_filter': response})