From 65430a2a8f1b007b716bb61b8f9df28b97018bda Mon Sep 17 00:00:00 2001 From: Jean-Marie Favreau Date: Wed, 27 Nov 2024 19:57:39 +0100 Subject: [PATCH] Ajout de suggestions de filtres par ville --- src/agenda_culturel/filters.py | 7 ++++++ ...20_referencelocation_suggested_distance.py | 18 +++++++++++++ src/agenda_culturel/models.py | 7 ++++++ src/agenda_culturel/static/style.scss | 10 ++++++-- .../templates/agenda_culturel/filter-inc.html | 7 ++++-- .../templatetags/locations_extra.py | 25 +++++++++++++++++++ src/agenda_culturel/templatetags/tag_extra.py | 2 +- src/scripts/communes.json | 2 ++ src/scripts/create_reference_locations.py | 5 +++- 9 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 src/agenda_culturel/migrations/0120_referencelocation_suggested_distance.py create mode 100644 src/agenda_culturel/templatetags/locations_extra.py diff --git a/src/agenda_culturel/filters.py b/src/agenda_culturel/filters.py index 42e7fad..07c23e8 100644 --- a/src/agenda_culturel/filters.py +++ b/src/agenda_culturel/filters.py @@ -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( diff --git a/src/agenda_culturel/migrations/0120_referencelocation_suggested_distance.py b/src/agenda_culturel/migrations/0120_referencelocation_suggested_distance.py new file mode 100644 index 0000000..83cfe05 --- /dev/null +++ b/src/agenda_culturel/migrations/0120_referencelocation_suggested_distance.py @@ -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)'), + ), + ] diff --git a/src/agenda_culturel/models.py b/src/agenda_culturel/models.py index 022dd6d..218b874 100644 --- a/src/agenda_culturel/models.py +++ b/src/agenda_culturel/models.py @@ -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") diff --git a/src/agenda_culturel/static/style.scss b/src/agenda_culturel/static/style.scss index 6e0b43c..efba3a3 100644 --- a/src/agenda_culturel/static/style.scss +++ b/src/agenda_culturel/static/style.scss @@ -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; diff --git a/src/agenda_culturel/templates/agenda_culturel/filter-inc.html b/src/agenda_culturel/templates/agenda_culturel/filter-inc.html index 1a6353e..d6ca6a7 100644 --- a/src/agenda_culturel/templates/agenda_culturel/filter-inc.html +++ b/src/agenda_culturel/templates/agenda_culturel/filter-inc.html @@ -2,6 +2,7 @@ {% load tag_extra %} {% load utils_extra %} {% load static %} +{% load locations_extra %} {% if noarticle == 0 %}
@@ -65,8 +66,10 @@ -
- {% show_suggested_tags filter=filter %} +
+ Suggestion : + {% show_suggested_positions filter=filter %} + {% show_suggested_tags filter=filter %}
diff --git a/src/agenda_culturel/templatetags/locations_extra.py b/src/agenda_culturel/templatetags/locations_extra.py new file mode 100644 index 0000000..0f95b52 --- /dev/null +++ b/src/agenda_culturel/templatetags/locations_extra.py @@ -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 += ' ' + picto_from_name("map-pin") + ' ' + l.name + ' ' + str(l.suggested_distance) + 'km' + + return mark_safe(result) diff --git a/src/agenda_culturel/templatetags/tag_extra.py b/src/agenda_culturel/templatetags/tag_extra.py index 88c3fd4..cba036c 100644 --- a/src/agenda_culturel/templatetags/tag_extra.py +++ b/src/agenda_culturel/templatetags/tag_extra.py @@ -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 :" + result = "" for t in tags: if filter.tag_exists(t.name) and not filter.is_selected_tag(t.name): diff --git a/src/scripts/communes.json b/src/scripts/communes.json index 78e1b25..8ec8063 100644 --- a/src/scripts/communes.json +++ b/src/scripts/communes.json @@ -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 diff --git a/src/scripts/create_reference_locations.py b/src/scripts/create_reference_locations.py index 172c020..2e777a5 100644 --- a/src/scripts/create_reference_locations.py +++ b/src/scripts/create_reference_locations.py @@ -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)