From 1232467f92fd143ecaa6b0a11a810238c3cc910b Mon Sep 17 00:00:00 2001 From: TubbDoose Date: Fri, 31 May 2024 08:03:29 +0000 Subject: [PATCH] Reduced query time on suggested articles for article page (same method) --- article/models.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/article/models.py b/article/models.py index ccb04ef3f..d5f8ffc28 100644 --- a/article/models.py +++ b/article/models.py @@ -1055,20 +1055,20 @@ def get_authors_with_roles(self) -> str: return authors_with_roles authors_with_roles = property(fget=get_authors_with_roles) - def get_category_articles(self, order='-explicit_published_at') -> QuerySet: + def get_category_articles(self, order='-first_published_at') -> QuerySet: """ Returns a list of articles within the Article's category """ - category_articles = ArticlePage.objects.live().public().filter(category=self.category).not_page(self).order_by(order) + category_articles = ArticlePage.objects.live().filter(category=self.category).not_page(self).order_by(order) return category_articles - def get_section_articles(self, order='-explicit_published_at') -> QuerySet: + def get_section_articles(self, order='-first_published_at') -> QuerySet: """ Returns a list of articles within the Article's section """ - section_articles = ArticlePage.objects.live().public().descendant_of(self.get_parent()).not_page(self).order_by(order) + section_articles = ArticlePage.objects.live().child_of(self.get_parent()).not_page(self).order_by(order) return section_articles @@ -1076,19 +1076,22 @@ def get_suggested(self, number_suggested=6): """ Defines the title and articles in the suggested box """ + suggested = {} + if self.category != None: + category_articles = self.get_category_articles() + if len(category_articles) > 0: + suggested = {} + suggested['title'] = "From " + self.get_parent().title + " - " + self.category.title + suggested['articles'] = category_articles[:number_suggested] + + if not suggested: + section_articles = self.get_section_articles() + if len(section_articles) > 0: + suggested = {} + suggested['title'] = "From " + self.get_parent().title + suggested['articles'] = section_articles[:number_suggested] - category_articles = self.get_category_articles() - section_articles = self.get_section_articles() - - if self.category == None or len(category_articles) == 0: - suggested = {} - suggested['title'] = "From " + self.get_parent().title - suggested['articles'] = section_articles[:number_suggested] - elif len(section_articles) > 0: - suggested = {} - suggested['title'] = "From " + self.get_parent().title + " - " + self.category.title - suggested['articles'] = category_articles[:number_suggested] - else: + if not suggested: suggested = False return suggested