Skip to content

Commit

Permalink
Merge pull request #1364 from laws-africa/taxonomy-queries
Browse files Browse the repository at this point in the history
 Fix consecutive db queries
  • Loading branch information
actlikewill authored Jul 12, 2023
2 parents 373f631 + 54f993c commit 8024853
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
5 changes: 2 additions & 3 deletions liiweb/views/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ def get_context_data(self, **kwargs):
context["recent_legislation"] = Legislation.objects.filter(
metadata_json__stub=False
).order_by("-date")[:10]
context["taxonomies"] = Taxonomy.get_tree()
context["taxonomies"] = Taxonomy.get_root_nodes()
context["recent_articles"] = (
Article.objects.prefetch_related("topics")
Article.objects.prefetch_related("topics", "author")
.filter(published=True)
.select_related("author")
.order_by("-date")[:5]
)
return context
Expand Down
2 changes: 1 addition & 1 deletion peachjam/templates/peachjam/_taxonomy_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% if taxonomies|length > 0 %}
<div class="row">
{% for taxonomy in taxonomies %}
{% if taxonomy.is_root and taxonomy.get_children|length > 0 %}
{% if taxonomy.is_root and taxonomy.get_children_count > 0 %}
<div class="col-12 col-lg-6 mb-4">
<div class="card">
<div class="card-header">{{ taxonomy }}</div>
Expand Down
35 changes: 22 additions & 13 deletions peachjam/views/article.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@

class ArticleListView(ListView):
model = Article
queryset = Article.objects.filter(published=True).order_by("-date")
queryset = (
Article.objects.filter(published=True)
.select_related("author")
.prefetch_related("topics")
.order_by("-date")
)
template_name = "peachjam/article_list.html"
context_object_name = "articles"
navbar_link = "articles"
paginate_by = 10

def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
years = sorted(
list(
self.model.objects.filter(published=True)
.order_by()
.values_list("date__year", flat=True)
.distinct()
),
reverse=True,
years = (
self.model.objects.filter(published=True)
.dates("date", "year", order="DESC")
.values_list("date__year", flat=True)
.distinct()
)

context["years"] = [
Expand Down Expand Up @@ -69,6 +71,8 @@ def get_context_data(self, **kwargs):
)
context["more_articles"] = (
Article.objects.filter(author=self.object.author, published=True)
.select_related("author")
.prefetch_related("topics")
.exclude(pk=self.object.pk)
.order_by("-date")[:5]
)
Expand All @@ -92,7 +96,12 @@ def get_context_data(self, **kwargs):


class ArticleYearArchiveView(YearArchiveView):
queryset = Article.objects.filter(published=True).order_by("-date")
queryset = (
Article.objects.select_related("author")
.prefetch_related("topics")
.filter(published=True)
.order_by("-date")
)
date_field = "date"
make_object_list = True
allow_future = True
Expand All @@ -109,15 +118,15 @@ def get_queryset(self):

def get_context_data(self, *, object_list=None, **kwargs):
context = super().get_context_data(**kwargs)

years = (
self.queryset.order_by("-date__year")
.dates("date", "year", order="DESC")
.values_list("date__year", flat=True)
.distinct()
)

context["years"] = [
{"url": reverse("article_year_archive", args=[year]), "year": year}
for year in years
{"url": reverse("article_year_archive", args=[y]), "year": y} for y in years
]

context["all_years_url"] = reverse("article_list")
Expand Down

0 comments on commit 8024853

Please sign in to comment.