Merge pull request #6169 from escattone/fix-1930

update `ProductTopicsAndSubtopicsWidget` to work with m2m topics
This commit is contained in:
Tasos Katsoulas 2024-08-06 13:49:22 +03:00 коммит произвёл GitHub
Родитель 0c394c5752 83f49215d7
Коммит acf7a6afc9
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 19 добавлений и 12 удалений

Просмотреть файл

@ -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 %}/>

Просмотреть файл

@ -3,7 +3,7 @@ from collections.abc import Iterable
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
@ -11,21 +11,28 @@ 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,
},
)