Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Aug 14, 2024
1 parent 021b4f0 commit 5b86cbc
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 88 deletions.
12 changes: 11 additions & 1 deletion lacommunaute/forum/management/commands/populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,19 @@ def handle(self, *args, **options):

for i in range(1, 3):
parent = CategoryForumFactory(with_public_perms=True, name=f"Thème {i}")
TopicFactory.create_batch(
2, forum=ForumFactory(parent=parent, with_public_perms=True, name=f"Fiche {i}-0"), with_post=True
)
for j in range(1, 3):
TopicFactory.create_batch(
2, forum=ForumFactory(parent=parent, with_public_perms=True, name=f"Fiche {i}-{j}"), with_post=True
2,
forum=ForumFactory(
parent=parent,
with_public_perms=True,
name=f"Fiche {i}-{j}",
with_tags=[f"tag_{i}", f"tag_{i}{j}"],
),
with_post=True,
)
sys.stdout.write("documentation created\n")

Expand Down
2 changes: 2 additions & 0 deletions lacommunaute/forum/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
ForumUpdateView,
ForumView,
SubCategoryForumCreateView,
SubCategoryForumListView,
)


Expand All @@ -18,6 +19,7 @@
path("forum/<str:slug>-<int:pk>/", ForumView.as_view(), name="forum"),
path("forum/<str:slug>-<int:pk>/update/", ForumUpdateView.as_view(), name="edit_forum"),
path("forum/<str:slug>-<int:pk>/rate/", ForumRatingView.as_view(), name="rate"),
path("forum/<str:slug>-<int:pk>/subs/", SubCategoryForumListView.as_view(), name="subcategory_forums"),
path("forums/", IndexView.as_view(), name="index"),
path("documentation/", CategoryForumListView.as_view(), name="documentation"),
path("documentation/category/create/", CategoryForumCreateView.as_view(), name="create_category"),
Expand Down
73 changes: 47 additions & 26 deletions lacommunaute/forum/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.contrib.auth.mixins import UserPassesTestMixin
from django.contrib.contenttypes.models import ContentType
from django.db.models.query import QuerySet
from django.http import Http404
from django.shortcuts import get_object_or_404, render
from django.urls import reverse, reverse_lazy
from django.views import View
Expand All @@ -27,7 +28,37 @@
ForumVisibilityContentTree = get_class("forum.visibility", "ForumVisibilityContentTree")


class ForumView(BaseForumView, FilteredTopicsListViewMixin):
class SubCategoryForumListMixin:
def get_descendants(self):
qs = self.get_forum().get_descendants()

forum_tags = self.request.GET.get("forum_tags")
if forum_tags:
qs = qs.filter(tags__slug__in=forum_tags.split(","))

return qs.prefetch_related("tags")

def get_descendants_tags(self):
return Tag.objects.filter(
taggit_taggeditem_items__content_type=ContentType.objects.get_for_model(Forum),
taggit_taggeditem_items__object_id__in=self.get_forum().get_descendants().values_list("id", flat=True),
).distinct()

def forum_tags_context(self):
return {
# TODO : remove permission management, though all forums are public in our case
"sub_forums": ForumVisibilityContentTree.from_forums(
self.request.forum_permission_handler.forum_list_filter(
self.get_descendants(),
self.request.user,
),
),
"sub_forums_tags": self.get_descendants_tags(),
"active_forum_tag_slug": self.request.GET.get("forum_tags") or None,
}


class ForumView(BaseForumView, FilteredTopicsListViewMixin, SubCategoryForumListMixin):
paginate_by = settings.FORUM_TOPICS_NUMBER_PER_PAGE

def get_template_names(self):
Expand All @@ -48,21 +79,6 @@ def will_render_documentation_category_variant(self):
def get_queryset(self):
return self.filter_queryset(self.get_forum().topics.optimized_for_topics_list(self.request.user.id))

def get_descendants(self):
qs = self.get_forum().get_descendants()

forum_tags = self.request.GET.get("forum_tags")
if forum_tags:
qs = qs.filter(tags__slug__in=forum_tags.split(","))

return qs.prefetch_related("tags")

def get_descendants_tags(self):
return Tag.objects.filter(
taggit_taggeditem_items__content_type=ContentType.objects.get_for_model(Forum),
taggit_taggeditem_items__object_id__in=self.get_forum().get_descendants().values_list("id", flat=True),
).distinct()

def get_context_data(self, **kwargs):
forum = self.get_forum()

Expand Down Expand Up @@ -93,16 +109,7 @@ def get_context_data(self, **kwargs):
context = context | self.get_topic_filter_context()

if self.will_render_documentation_category_variant():
# vincentporte, overide the method to add the sub_forums, not testing permissions ^v^
# only in the documentation category which is public
context["sub_forums"] = ForumVisibilityContentTree.from_forums(
self.request.forum_permission_handler.forum_list_filter(
self.get_descendants(),
self.request.user,
),
)
context["sub_forums_tags"] = self.get_descendants_tags()
context["active_forum_tag_slug"] = self.request.GET.get("forum_tags") or None
context = context | self.forum_tags_context()

if self.will_render_documentation_variant():
context["sibling_forums"] = forum.get_siblings(include_self=True)
Expand All @@ -112,6 +119,20 @@ def get_context_data(self, **kwargs):
return context


class SubCategoryForumListView(BaseForumView, SubCategoryForumListMixin):
template_name = "forum/partials/subcategory_forum_list.html"

def get(self, request, **kwargs):
if self.get_forum().is_in_documentation_area and self.forum.level == 0:
return super().get(request, **kwargs)
raise Http404

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context = context | self.forum_tags_context()
return context


class ForumUpdateView(UserPassesTestMixin, UpdateView):
template_name = "forum/forum_create_or_update.html"
form_class = ForumForm
Expand Down
41 changes: 1 addition & 40 deletions lacommunaute/templates/forum/forum_documentation_category.html
Original file line number Diff line number Diff line change
@@ -1,45 +1,6 @@
{% extends "forum/forum_detail.html" %}
{% block forum_head_content %}
{{ block.super }}
{% if sub_forums_tags %}
{% include "forum/partials/forum_tags.html" with tags=sub_forums_tags only %}
{% endif %}
{% endblock forum_head_content %}
{% block subforum_list %}
<div class="row mt-4" id="documentation-category-subforums">
{% for node in sub_forums.top_nodes %}
<div class="col-12 col-md-4 mb-3 mb-md-5">
<div class="card c-card has-one-link-inside h-100">
{% if node.obj.image %}
<div class="card-header card-header-thumbnail rounded">
<img src="{{ node.obj.image.url }}" alt="{{ node.obj.name }}" class="img-fitcover img-fluid" loading="lazy" />
</div>
{% endif %}
<div class="card-body pb-0">
<p class="h3 lh-base">{{ node.obj.name }}</p>
{% spaceless %}
{% for tag in node.obj.tags.all %}
{% if forloop.first %}<div>{% endif %}
{% include "partials/tag.html" with tag=tag only %}
{% if forloop.last %}</div>{% endif %}
{% empty %} {% comment %} no div {% endcomment %}
{% endfor %}
{% endspaceless %}
{% if node.obj.short_description %}<div class="mt-3">{{ node.obj.short_description }}</div>{% endif %}
</div>
<div class="card-footer text-end">
<a href="{% url 'forum_extension:forum' node.obj.slug node.obj.id %}"
class="btn btn-sm btn-ico btn-link stretched-link matomo-event"
data-matomo-category="engagement"
data-matomo-action="view"
data-matomo-option="fiches_techniques">
<i class="ri-arrow-right-line ri-lg"></i>
</a>
</div>
</div>
</div>
{% endfor %}
</div>
{% include "forum/partials/subcategory_forum_list.html" with request=request sub_forums=sub_forums sub_forums_tags=sub_forums_tags active_forum_tag_slug=active_forum_tag_slug only %}
{% endblock subforum_list %}
{% block forum_foot_content %}
{% if user.is_superuser %}
Expand Down
34 changes: 19 additions & 15 deletions lacommunaute/templates/forum/partials/forum_tags.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
{% load url_add_query %}
<div class="container-fluid d-flex justify-content-center c-box">
Filtrez par sous-catégorie :&nbsp;
{% for tag in tags %}
<a href="{% url_add_query request.path forum_tags=tag.slug %}">
{% if tag.slug == active_forum_tag_slug %}
{% include "partials/tag.html" with tag=tag highlight=1 only %}
{% else %}
{% include "partials/tag.html" with tag=tag only %}
{% endif %}
</a>&nbsp;
{% if forloop.last and active_forum_tag_slug %}
<a href="{{ request.path }}"><span class="tag bg-light text-muted"><i class="ri-close-circle-fill"></i> supprimer le filtre</span></a>
{% endif %}
{% endfor %}
</div>
{% for tag in tags %}
{% if path and tag.slug == active_forum_tag_slug %}
<a href="{{ path }}" class="tag bg-info text-white" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Supprimer ce filtre">
<i class="ri-close-circle-fill"></i>
{{ tag.name }}
</a>
{% elif path %}
{% url_add_query path forum_tags=tag.slug as url_with_query_params %}
<a href="{{ url_with_query_params }}" class="tag bg-info-lighter text-info">{{ tag.name }}</a>
{% else %}
<span class="tag bg-info-lighter text-info">{{ tag.name }}</span>
{% endif %}
{% endfor %}
{% comment %}
<button id="filtertopics-button" hx-target="#topicsarea" hx-swap="outerHTML" hx-push-url="true" hx-get="{{ filter_dropdown_endpoint }}?filter={{ filter.0 }}{% if active_tags %}&tags={{ active_tags }}{% endif %}" class="dropdown-item matomo-event" data-matomo-category="engagement" data-matomo-action="filter" data-matomo-option="topics">{{ filter.1 }}
</button>

hx-target"#topicsarea" hx-swap="outerHTML" hx-push-url="true" hx-get="{% url forum_extension:subcategory_forums forum.pk forum.slug %}?forum_tags={{ tag.slug }}" class="tag bg-info text-white" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Supprimer ce filtre"
{% endcomment %}
37 changes: 37 additions & 0 deletions lacommunaute/templates/forum/partials/subcategory_forum_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<div id="subforums_area" class="container-fluid">
{% if sub_forums_tags %}
<div class="c-box">
<div class="container-fluid d-flex justify-content-center mb-3">Afficher les fiches contenant l'étiquette</div>
<div class="container-fluid d-flex justify-content-center">
{% include "forum/partials/forum_tags.html" with tags=sub_forums_tags active_forum_tag_slug=active_forum_tag_slug path=request.path only %}
</div>
</div>
{% endif %}
<div class="row mt-4" id="documentation-category-subforums">
{% for node in sub_forums.top_nodes %}
<div class="col-12 col-md-4 mb-3 mb-md-5">
<div class="card c-card has-one-link-inside h-100">
{% if node.obj.image %}
<div class="card-header card-header-thumbnail rounded">
<img src="{{ node.obj.image.url }}" alt="{{ node.obj.name }}" class="img-fitcover img-fluid" loading="lazy" />
</div>
{% endif %}
<div class="card-body pb-0">
<p class="h3 lh-base">{{ node.obj.name }}</p>
<div>{% include "forum/partials/forum_tags.html" with tags=node.obj.tags.all only %}</div>
{% if node.obj.short_description %}<div class="mt-3">{{ node.obj.short_description }}</div>{% endif %}
</div>
<div class="card-footer text-end">
<a href="{% url 'forum_extension:forum' node.obj.slug node.obj.id %}"
class="btn btn-sm btn-ico btn-link stretched-link matomo-event"
data-matomo-category="engagement"
data-matomo-action="view"
data-matomo-option="fiches_techniques">
<i class="ri-arrow-right-line ri-lg"></i>
</a>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% load url_add_query %}
{% for tag in tags %}
{% url_add_query request.path tags=tag.slug page=1 as url_with_query_params %}
{% include "partials/tag.html" with tag=tag url=url_with_query_params only %}
<a href="{{ url_with_query_params }}" class="tag bg-info-lighter text-info">{{ tag.name }}</a>
{% endfor %}
5 changes: 0 additions & 5 deletions lacommunaute/templates/partials/tag.html

This file was deleted.

0 comments on commit 5b86cbc

Please sign in to comment.