Skip to content

Commit

Permalink
Reduced query time on suggested articles for article page (same method)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelmdLow committed May 31, 2024
1 parent 848ecdd commit 1232467
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions article/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,40 +1055,43 @@ 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

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
Expand Down

0 comments on commit 1232467

Please sign in to comment.