diff --git a/kitsune/products/tests/test_templates.py b/kitsune/products/tests/test_templates.py index 48c03dc5bb6..e5c64ffcca9 100644 --- a/kitsune/products/tests/test_templates.py +++ b/kitsune/products/tests/test_templates.py @@ -154,16 +154,25 @@ def test_subtopics(self): url = reverse('products.documents', args=[p.slug, t.slug]) r = self.client.get(url, follow=True) eq_(200, r.status_code) - doc = pq(r.content) - eq_(0, len(doc('ul.subtopics'))) + pqdoc = pq(r.content) + eq_(0, len(pqdoc('ul.subtopics'))) - # Create a subtopic and verify it is listed - topic(parent=t, save=True) - url = reverse('products.documents', args=[p.slug, t.slug]) + # Create a subtopic, it still shouldn't show up because no + # articles are assigned. + subtopic = topic(parent=t, save=True) r = self.client.get(url, follow=True) eq_(200, r.status_code) - doc = pq(r.content) - eq_(1, len(doc('ul.subtopics'))) + pqdoc = pq(r.content) + eq_(0, len(pqdoc('ul.subtopics'))) + + # Add a document to the subtopic, now it should appear. + doc.topics.add(subtopic) + self.refresh() + + r = self.client.get(url, follow=True) + eq_(200, r.status_code) + pqdoc = pq(r.content) + eq_(1, len(pqdoc('ul.subtopics'))) class ProductViewsNewTopicsTestCase(ElasticTestCase): @@ -312,13 +321,22 @@ def test_subtopics(self): url = reverse('products.documents', args=[p.slug, t.slug]) r = self.client.get(url, follow=True) eq_(200, r.status_code) - doc = pq(r.content) - eq_(0, len(doc('ul.subtopics'))) + pqdoc = pq(r.content) + eq_(0, len(pqdoc('ul.subtopics'))) - # Create a subtopic and verify it is listed - new_topic(parent=t, product=p, save=True) - url = reverse('products.documents', args=[p.slug, t.slug]) + # Create a subtopic, it still shouldn't show up because no + # articles are assigned. + subtopic = new_topic(parent=t, product=p, save=True) r = self.client.get(url, follow=True) eq_(200, r.status_code) - doc = pq(r.content) - eq_(1, len(doc('ul.subtopics'))) + pqdoc = pq(r.content) + eq_(0, len(pqdoc('ul.subtopics'))) + + # Add a document to the subtopic, now it should appear. + doc.new_topics.add(subtopic) + self.refresh() + + r = self.client.get(url, follow=True) + eq_(200, r.status_code) + pqdoc = pq(r.content) + eq_(1, len(pqdoc('ul.subtopics'))) diff --git a/kitsune/products/views.py b/kitsune/products/views.py index 7e117646dd3..15f10a2affa 100644 --- a/kitsune/products/views.py +++ b/kitsune/products/views.py @@ -44,7 +44,7 @@ def product_landing(request, template, slug): 'topics': topics_for( products=[product], new_topics=new_topics, - include_subtopics=False), + parent=None), 'hot_docs': hot_docs, 'fallback_hot_docs': fallback_hot_docs, 'search_params': {'product': slug}}) @@ -59,10 +59,8 @@ def document_listing(request, template, product_slug, topic_slug): if new_topics: topic = get_object_or_404(NewTopic, slug=topic_slug, product=product) - subtopics = NewTopic.objects.filter(parent=topic) else: topic = get_object_or_404(Topic, slug=topic_slug) - subtopics = Topic.objects.filter(parent=topic) documents, fallback_documents = documents_for( locale=request.LANGUAGE_CODE, products=[product], topics=[topic], @@ -72,7 +70,8 @@ def document_listing(request, template, product_slug, topic_slug): 'product': product, 'topic': topic, 'topics': topics_for(products=[product], new_topics=new_topics), - 'subtopics': subtopics, + 'subtopics': topics_for( + products=[product], parent=topic, new_topics=new_topics), 'documents': documents, 'fallback_documents': fallback_documents, 'search_params': {'product': product_slug}}) diff --git a/kitsune/wiki/facets.py b/kitsune/wiki/facets.py index 6ee97cf19b9..f4348dbc9ad 100644 --- a/kitsune/wiki/facets.py +++ b/kitsune/wiki/facets.py @@ -14,10 +14,11 @@ # TODO: Remove the new_topics argument when we remove old topics. -def topics_for(products, include_subtopics=True, new_topics=False): +def topics_for(products, parent=False, new_topics=False): """Returns a list of topics that apply to passed in products and topics. :arg products: a list of Product instances + :arg parent: (optional) limit to topics with the given parent """ statsd.incr('wiki.facets.topics_for.db') @@ -40,8 +41,8 @@ def topics_for(products, include_subtopics=True, new_topics=False): .annotate(num_docs=Count('document')) .distinct()) - if not include_subtopics: - qs = qs.filter(parent=None) + if parent or parent is None: + qs = qs.filter(parent=parent) return qs diff --git a/kitsune/wiki/models.py b/kitsune/wiki/models.py index 44bf0a7b7c1..eb3eb5903c6 100644 --- a/kitsune/wiki/models.py +++ b/kitsune/wiki/models.py @@ -810,6 +810,10 @@ def index(cls, document, **kwargs): 'wiki', Document.topics.through, m2m=True) +register_for_indexing( + 'wiki', + Document.new_topics.through, + m2m=True) register_for_indexing( 'wiki', Document.products.through,