Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update ProductTopicsAndSubtopicsWidget to work with m2m topics #6169

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions kitsune/wiki/jinja2/wiki/includes/product_topics_widget.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<section id="accordion" class="mzp-c-details products">
{% for group in topics|groupby('product_id') %}
<h3 class="sumo-card-heading">{{ group.list[0].product }}</h3>
{% for product_title, topics in topics_by_product.items() %}
<h3 class="sumo-card-heading">{{ product_title }}</h3>
<ul class="checkbox-list">
{% for topic in group.list %}
{% for topic in topics %}
<li>
<div class="field checkbox is-condensed">
<input type="checkbox" name="{{ name }}" id="id_{{ topic.id }}" value="{{ topic.id }}"{% if topic.checked %} checked{% endif %}/>
Expand Down
25 changes: 16 additions & 9 deletions kitsune/wiki/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,36 @@
from django import forms
from django.template.loader import render_to_string

from kitsune.products.models import Topic
from kitsune.products.models import Product, Topic
from kitsune.wiki.models import Document


class ProductTopicsAndSubtopicsWidget(forms.widgets.SelectMultiple):
"""A widget to render topics organized by product and with subtopics."""

def render(self, name, value, attrs=None, renderer=None):
topics_and_subtopics = Topic.active.filter(products__isnull=False)
topics = [t for t in topics_and_subtopics if t.parent_id is None]
topics_by_product = {}
for product in Product.active.filter(m2m_topics__isnull=False):
# Get all of the topics and subtopics that apply to this product.
topics_and_subtopics = Topic.active.filter(products=product)
# Get the topics only.
topics = [t for t in topics_and_subtopics if t.parent_id is None]

for topic in topics:
self.process_topic(value, topic)
for topic in topics:
self.process_topic(value, topic)

topic.my_subtopics = [t for t in topics_and_subtopics if t.parent_id == topic.id]
# Get all of the subtopics for this topic.
topic.my_subtopics = [t for t in topics_and_subtopics if t.parent_id == topic.id]

for subtopic in topic.my_subtopics:
self.process_topic(value, subtopic)
for subtopic in topic.my_subtopics:
self.process_topic(value, subtopic)

topics_by_product[product.title] = topics

return render_to_string(
"wiki/includes/product_topics_widget.html",
{
"topics": topics,
"topics_by_product": topics_by_product,
"name": name,
},
)
Expand Down