On ajoute le principe qu'un tag puisse être une sous-catégorie

This commit is contained in:
Jean-Marie Favreau 2024-11-02 12:23:33 +01:00
parent af297b5d25
commit 8e552f2574
6 changed files with 61 additions and 10 deletions

View File

@ -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'),
),
]

View File

@ -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})

View File

@ -35,10 +35,12 @@
{% if object %}
<p>{{ object.description|safe }}</p>
<p>{% if object.category %}Cette étiquette est une sous-catégorie de {{ object.category| small_cat:object.category.get_absolute_url }}{% endif %}</p>
{% endif %}
{% include 'agenda_culturel/paginator.html' %}
<footer>
{% include 'agenda_culturel/paginator.html' %}
</footer>
</article>
{% for event in paginator_filter %}

View File

@ -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 @@
<header><a href="{% url 'view_tag' tag.name %}" role="button" class="small-cat">{{ tag.name }}</a></header>
<div>
{{ tag.description|safe }}
</div>
<p>{% if tag.category %}Cette étiquette est une sous-catégorie de {{ tag.category| small_cat:tag.category.get_absolute_url }}{% endif %}</p>
</div>
</article>
{% endfor %}

View File

@ -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(
'<a href="'
@ -16,6 +18,7 @@ def t_button(tag, url, strike=False):
+ '" role="button" class="small-cat'
+ strike_class
+ '">'
+ cat
+ tag
+ "</a>"
)
@ -24,6 +27,7 @@ def t_button(tag, url, strike=False):
'<span role="button" class="small-cat'
+ strike_class
+ '">'
+ cat
+ tag
+ "</span>"
)
@ -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)

View File

@ -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):