Ajout de suggestions de filtres par ville

This commit is contained in:
Jean-Marie Favreau 2024-11-27 19:57:39 +01:00
parent 8ef620c8e1
commit 65430a2a8f
9 changed files with 77 additions and 6 deletions

View File

@ -309,6 +309,13 @@ class EventFilter(django_filters.FilterSet):
else:
return str(self.get_cleaned_data("position")) + ' (' + str(self.get_cleaned_data("radius")) + ' km)'
def is_filtered_by_position_radius(self):
return not self.get_cleaned_data("position") is None and not self.get_cleaned_data("radius") is None
def get_url_add_suggested_position(self, location):
result = self.request.get_full_path()
return result + ('&' if '?' in result else '?') + 'position=' + str(location.pk) + "&radius=" + str(location.suggested_distance)
class EventFilterAdmin(django_filters.FilterSet):
status = django_filters.MultipleChoiceFilter(

View File

@ -0,0 +1,18 @@
# Generated by Django 4.2.9 on 2024-11-27 18:25
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('agenda_culturel', '0119_alter_tag_options_alter_event_category_and_more'),
]
operations = [
migrations.AddField(
model_name='referencelocation',
name='suggested_distance',
field=models.IntegerField(default=None, help_text='If this distance is given, this location is part of the suggested filters.', null=True, verbose_name='Suggested distance (km)'),
),
]

View File

@ -398,6 +398,7 @@ class DuplicatedEvents(models.Model):
class ReferenceLocation(models.Model):
name = models.CharField(verbose_name=_("Name"), help_text=_("Name of the location"), unique=True, null=False)
location = LocationField(based_fields=["name"], zoom=12, default=Point(3.08333, 45.783329), srid=4326)
main = models.IntegerField(
@ -405,6 +406,12 @@ class ReferenceLocation(models.Model):
help_text=_("This location is one of the main locations (shown first higher values)."),
default=0,
)
suggested_distance = models.IntegerField(
verbose_name=_("Suggested distance (km)"),
help_text=_("If this distance is given, this location is part of the suggested filters."),
null=True,
default=None
)
class Meta:
verbose_name = _("Reference location")

View File

@ -147,7 +147,7 @@ details[role="list"] summary + ul li.selected>a:hover {
}
}
.suggested-tags {
.suggestions {
font-size: 80%;
}
}
@ -201,10 +201,16 @@ details[role="list"] summary + ul li.selected>a:hover {
text-align: left;
}
.suggested-tags .small-cat {
.suggestions .small-cat {
overflow: visible;
}
.small-location {
@extend .small-cat;
border-color: var(--secondary-inverse);
color: var(--secondary-inverse);
}
.circ-cat.circ-large {
height: 2.6em;
width: 2.6em;

View File

@ -2,6 +2,7 @@
{% load tag_extra %}
{% load utils_extra %}
{% load static %}
{% load locations_extra %}
{% if noarticle == 0 %}
<article id="filters">
@ -65,8 +66,10 @@
<button type="submit">Appliquer le filtre</button>
</form>
</details>
<div class="suggested-tags">
{% show_suggested_tags filter=filter %}
<div class="suggestions">
Suggestion&nbsp;:
{% show_suggested_positions filter=filter %}
{% show_suggested_tags filter=filter %}
</div>
</div>
<div class="clear"></div>

View File

@ -0,0 +1,25 @@
from django import template
from django.utils.safestring import mark_safe
from django.core.cache import cache
from .utils_extra import picto_from_name
from agenda_culturel.models import ReferenceLocation
register = template.Library()
@register.simple_tag
def show_suggested_positions(filter):
filter.form.full_clean()
if filter.is_filtered_by_position_radius():
return ""
locations = ReferenceLocation.objects.all().filter(suggested_distance__isnull=False).order_by("main", "name")
result = ''
for l in locations:
result += ' <a class="small-location" role="button" href="' + filter.get_url_add_suggested_position(l) + '">' + picto_from_name("map-pin") + ' ' + l.name + ' ' + str(l.suggested_distance) + 'km</a>'
return mark_safe(result)

View File

@ -54,7 +54,7 @@ def tag_not_in_db(tag, tags):
def show_suggested_tags(filter):
filter.form.full_clean()
tags = Tag.objects.all().filter(principal=True).order_by("name")
result = "Suggestion&nbsp;:"
result = ""
for t in tags:
if filter.tag_exists(t.name) and not filter.is_selected_tag(t.name):

View File

@ -289,6 +289,7 @@
{
"com_name": "Clermont-Ferrand",
"main": 10,
"suggested": 10,
"geo_point_2d": {
"lon": 3.1153994509459313,
"lat": 45.78590931605406
@ -339,6 +340,7 @@
{
"com_name": "Riom",
"main": 3,
"suggested": 10,
"geo_point_2d": {
"lon": 3.13259085594027,
"lat": 45.89435053196184

View File

@ -13,7 +13,10 @@ def run():
# remove all locations
ReferenceLocation.objects.all().delete()
objs = [ReferenceLocation(location=Point(c["geo_point_2d"]["lon"], c["geo_point_2d"]["lat"]), name=c["com_name"], main=c["main"] if "main" in c else 0) for c in data]
objs = [ReferenceLocation(location=Point(c["geo_point_2d"]["lon"], c["geo_point_2d"]["lat"]),
name=c["com_name"],
main=c["main"] if "main" in c else 0,
suggested_distance=c["suggested"] if "suggested" in c else None) for c in data]
objs = ReferenceLocation.objects.bulk_create(objs, ignore_conflicts=True)