Skip to content

Commit

Permalink
Update localized_featured_blog_topics method to retreive en slug when…
Browse files Browse the repository at this point in the history
… not en locale (#12370)
  • Loading branch information
robdivincenzo authored May 24, 2024
1 parent 72fc98b commit fc10d26
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
36 changes: 23 additions & 13 deletions network-api/networkapi/nav/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,27 +151,37 @@ def blog_index_page(self):

@property
def localized_featured_blog_topics(self):
en_locale = wagtail_models.Locale.objects.get(language_code="en")
active_locale = wagtail_models.Locale.get_active()

# Get localized blog_topics
featured_topics_relationships = self.featured_blog_topics.all().select_related("topic", "icon")

# Build a cache of the local topics:
topic_ids = list(featured_topics_relationships.values_list("topic_id", flat=True))
topics = BlogPageTopic.objects.filter(id__in=topic_ids).order_by("nav_menu_featured_topics__sort_order")
topics = localize_queryset(topics, preserve_order=True)
topics_cache = {topic.translation_key: topic for topic in topics}
# If not in the en locale, build a cache of en_topics to efficiently get the topics's en slug
# @TODO Localize Slugs TP1-690 / #12367
if active_locale != en_locale:
# Get topics from the en_menu
en_featured_topics_relationships = self.get_translation(en_locale).featured_blog_topics

# Replace topics with localized versions:
for relationship in featured_topics_relationships:
if relationship.topic:
local_topic = topics_cache.get(relationship.topic.translation_key)
if local_topic:
relationship.topic = local_topic
# Build a cache of the en topics:
en_topic_ids = list(en_featured_topics_relationships.values_list("topic_id", flat=True))
en_topics = BlogPageTopic.objects.filter(id__in=en_topic_ids).order_by(
"nav_menu_featured_topics__sort_order"
)
en_topics_cache = {en_topic.translation_key: en_topic for en_topic in en_topics}

# Annotate with its url:
# Build featured topics slugs
blog_index_page = self.blog_index_page
for relationship in featured_topics_relationships:
if relationship.topic:
# Use the en slug only until slugs are localized @TODO TP1-690 / #12367
if active_locale != en_locale:
topic_slug = en_topics_cache.get(relationship.topic.translation_key).slug
else:
topic_slug = relationship.topic.slug

relationship.topic.url = blog_index_page.url + blog_index_page.reverse_subpage(
"entries_by_topic", args=[relationship.topic.slug]
"entries_by_topic", args=[topic_slug]
)

return featured_topics_relationships
Expand Down
4 changes: 2 additions & 2 deletions network-api/networkapi/nav/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_localized_featured_topics(self) -> None:
)

# Get the localised topics:
with self.assertNumQueries(7):
with self.assertNumQueries(5):
relationships = self.menu.localized_featured_blog_topics
topics = [relationship.topic for relationship in relationships]
icons = [relationship.icon for relationship in relationships]
Expand Down Expand Up @@ -101,7 +101,7 @@ def test_number_of_queries_for_localized_featured_topics(self) -> None:
sort_order=idx,
)

with self.assertNumQueries(7):
with self.assertNumQueries(5):
# Make sure that this property won't blow up to N+1 queries
relationships = self.menu.localized_featured_blog_topics
topics = [relationship.topic for relationship in relationships]
Expand Down

0 comments on commit fc10d26

Please sign in to comment.