diff --git a/network-api/networkapi/nav/models.py b/network-api/networkapi/nav/models.py index bb47dc8dc4b..8b225254d9a 100644 --- a/network-api/networkapi/nav/models.py +++ b/network-api/networkapi/nav/models.py @@ -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 diff --git a/network-api/networkapi/nav/tests/test_models.py b/network-api/networkapi/nav/tests/test_models.py index 01a9908dcaa..f6fd61a9915 100644 --- a/network-api/networkapi/nav/tests/test_models.py +++ b/network-api/networkapi/nav/tests/test_models.py @@ -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] @@ -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]