Skip to content

Commit

Permalink
leverage get_featured_articles
Browse files Browse the repository at this point in the history
  • Loading branch information
smithellis committed Jan 9, 2025
1 parent 6e209d3 commit 02ed05b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 7 additions & 1 deletion kitsune/products/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,17 @@ def product_landing(request, slug):
documents = {}
topic_counts = {}
for topic in topics:
# We need to have a total count of available documents for each topic
docs, _ = documents_for(
request.user, request.LANGUAGE_CODE, topics=[topic], products=[product]
)
documents[topic] = docs[:3]
topic_counts[topic] = len(docs)
# Passing the topic to get_featured_articles will return 4 random highly visited
# articles for that topic
# - if it can't, we'll just use the first 3 documents
documents[topic] = (
get_featured_articles(product, locale=request.LANGUAGE_CODE, topic=topic) or docs[:3]
)

return render(
request,
Expand Down
7 changes: 6 additions & 1 deletion kitsune/wiki/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ def _active_contributors_id(from_date, to_date, locale, product):
return set(list(editors) + list(reviewers))


def get_featured_articles(product=None, locale=settings.WIKI_DEFAULT_LANGUAGE):
def get_featured_articles(product=None, locale=settings.WIKI_DEFAULT_LANGUAGE, topic=None):
"""Returns 4 random articles from the most visited.
If a product is passed, it returns 4 random highly visited articles.
If passed a topic, it returns 4 random highly visited articles for that topic.
"""
visits = (
WikiDocumentVisits.objects.filter(period=LAST_7_DAYS)
Expand All @@ -124,6 +126,9 @@ def get_featured_articles(product=None, locale=settings.WIKI_DEFAULT_LANGUAGE):
if product:
visits = visits.filter(document__products__in=[product.id])

if topic:
visits = visits.filter(document__topics__in=[topic.id])

visits = visits[:10]
documents = []

Expand Down

0 comments on commit 02ed05b

Please sign in to comment.