From 8e552f2574a55c4863e0c0287b5a2592e2333a3b Mon Sep 17 00:00:00 2001 From: Jean-Marie Favreau Date: Sat, 2 Nov 2024 12:23:33 +0100 Subject: [PATCH] =?UTF-8?q?On=20ajoute=20le=20principe=20qu'un=20tag=20pui?= =?UTF-8?q?sse=20=C3=AAtre=20une=20sous-cat=C3=A9gorie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migrations/0100_tag_category.py | 19 +++++++++++++++++++ src/agenda_culturel/models.py | 12 ++++++++++++ .../templates/agenda_culturel/tag.html | 6 ++++-- .../templates/agenda_culturel/tags.html | 7 ++++++- src/agenda_culturel/templatetags/tag_extra.py | 12 ++++++++++-- src/agenda_culturel/views.py | 15 ++++++++++----- 6 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 src/agenda_culturel/migrations/0100_tag_category.py diff --git a/src/agenda_culturel/migrations/0100_tag_category.py b/src/agenda_culturel/migrations/0100_tag_category.py new file mode 100644 index 0000000..2fe6ca8 --- /dev/null +++ b/src/agenda_culturel/migrations/0100_tag_category.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.9 on 2024-11-02 10:54 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('agenda_culturel', '0099_update_categories'), + ] + + operations = [ + migrations.AddField( + model_name='tag', + name='category', + field=models.ForeignKey(default=None, help_text='This tags corresponds to a sub-category of the given category', null=True, on_delete=django.db.models.deletion.SET_NULL, to='agenda_culturel.category', verbose_name='Category'), + ), + ] diff --git a/src/agenda_culturel/models.py b/src/agenda_culturel/models.py index a0ab122..80c9ce6 100644 --- a/src/agenda_culturel/models.py +++ b/src/agenda_culturel/models.py @@ -155,6 +155,9 @@ class Category(models.Model): def css_class(self): return "cat-" + str(self.id) + def get_absolute_url(self): + return reverse('home') + '?catgory=' + str(self.pk) + def __str__(self): return self.name @@ -183,6 +186,15 @@ class Tag(models.Model): default=True, ) + category = models.ForeignKey( + Category, + verbose_name=_("Category"), + help_text=_("This tags corresponds to a sub-category of the given category"), + null=True, + default=None, + on_delete=models.SET_NULL, + ) + def get_absolute_url(self): return reverse("view_tag", kwargs={"t": self.name}) diff --git a/src/agenda_culturel/templates/agenda_culturel/tag.html b/src/agenda_culturel/templates/agenda_culturel/tag.html index 6e48a98..036587d 100644 --- a/src/agenda_culturel/templates/agenda_culturel/tag.html +++ b/src/agenda_culturel/templates/agenda_culturel/tag.html @@ -35,10 +35,12 @@ {% if object %}

{{ object.description|safe }}

+

{% if object.category %}Cette étiquette est une sous-catégorie de {{ object.category| small_cat:object.category.get_absolute_url }}{% endif %}

{% endif %} -{% include 'agenda_culturel/paginator.html' %} - + {% for event in paginator_filter %} diff --git a/src/agenda_culturel/templates/agenda_culturel/tags.html b/src/agenda_culturel/templates/agenda_culturel/tags.html index 90f1ef3..099452c 100644 --- a/src/agenda_culturel/templates/agenda_culturel/tags.html +++ b/src/agenda_culturel/templates/agenda_culturel/tags.html @@ -9,6 +9,10 @@ {% block fluid %}{% endblock %} +{% block entete_header %} + {% css_categories %} +{% endblock %} + {% block content %} {% with cache_timeout=user.is_authenticated|yesno:"30,600" %} @@ -25,7 +29,8 @@
{{ tag.name }}
{{ tag.description|safe }} -
+

{% if tag.category %}Cette étiquette est une sous-catégorie de {{ tag.category| small_cat:tag.category.get_absolute_url }}{% endif %}

+ {% endfor %} diff --git a/src/agenda_culturel/templatetags/tag_extra.py b/src/agenda_culturel/templatetags/tag_extra.py index b666be4..74c3721 100644 --- a/src/agenda_culturel/templatetags/tag_extra.py +++ b/src/agenda_culturel/templatetags/tag_extra.py @@ -3,12 +3,14 @@ from django.utils.safestring import mark_safe from django.urls import reverse_lazy from agenda_culturel.models import Tag from .utils_extra import * +from .cat_extra import * register = template.Library() -def t_button(tag, url, strike=False): +def t_button(tag, url, strike=False, category=None): strike_class = " strike" if strike else "" + cat = "" if category is None else circle_cat(category) + ' ' if not url is None: return mark_safe( '' + + cat + tag + "" ) @@ -24,6 +27,7 @@ def t_button(tag, url, strike=False): '' + + cat + tag + "" ) @@ -54,6 +58,10 @@ def show_suggested_tags(filter): for t in tags: if filter.tag_exists(t.name) and not filter.is_selected_tag(t.name): - result += ' ' + t_button(t.name, filter.get_url_add_tag(t.name)) + if not filter.has_category() or t.category is None or filter.get_categories()[0] == t.category: + if filter.has_category(): + result += ' ' + t_button(t.name, filter.get_url_add_tag(t.name), category=t.category) + else: + result += ' ' + t_button(t.name, filter.get_url_add_tag(t.name, t.category), category=t.category) return mark_safe(result) diff --git a/src/agenda_culturel/views.py b/src/agenda_culturel/views.py index 391c23f..2a51ae0 100644 --- a/src/agenda_culturel/views.py +++ b/src/agenda_culturel/views.py @@ -309,6 +309,9 @@ class EventFilter(django_filters.FilterSet): def get_categories(self): return self.get_cleaned_data("category") + + def has_category(self): + return "category" in self.form.cleaned_data and len(self.get_cleaned_data("category")) > 0 def get_tags(self): return self.get_cleaned_data("tags") @@ -393,11 +396,13 @@ class EventFilter(django_filters.FilterSet): def is_selected_tag(self, tag): return "tags" in self.form.cleaned_data and tag in self.form.cleaned_data["tags"] - def get_url_add_tag(self, tag, full_path = None): - if full_path is None: - full_path = self.request.get_full_path() + def get_url_add_tag(self, tag, category=None): + full_path = self.request.get_full_path() result = full_path + ('&' if '?' in full_path else '?') + 'tags=' + str(tag) + + if not category is None: + result = self.get_url_add_categories([category.pk], result) return result def tag_exists(self, tag): @@ -2084,14 +2089,14 @@ class PlaceFromEventCreateView(PlaceCreateView): class TagUpdateView(PermissionRequiredMixin, SuccessMessageMixin, UpdateView): model = Tag permission_required = "agenda_culturel.change_tag" - fields = ["name", "description", "principal"] + fields = ["name", "description", "principal", "category"] success_message = _("The tag has been successfully updated.") class TagCreateView(PermissionRequiredMixin, SuccessMessageMixin, CreateView): model = Tag permission_required = "agenda_culturel.add_tag" - fields = ["name", "description", "principal"] + fields = ["name", "description", "principal", "category"] success_message = _("The tag has been successfully created.") def get_initial(self, *args, **kwargs):