diff --git a/liiweb/views/general.py b/liiweb/views/general.py index 0b49118f3..dfcccc0d2 100644 --- a/liiweb/views/general.py +++ b/liiweb/views/general.py @@ -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 diff --git a/peachjam/templates/peachjam/_taxonomy_list.html b/peachjam/templates/peachjam/_taxonomy_list.html index 470979b1e..a3bcf8d41 100644 --- a/peachjam/templates/peachjam/_taxonomy_list.html +++ b/peachjam/templates/peachjam/_taxonomy_list.html @@ -3,7 +3,7 @@ {% if taxonomies|length > 0 %}
{% for taxonomy in taxonomies %} - {% if taxonomy.is_root and taxonomy.get_children|length > 0 %} + {% if taxonomy.is_root and taxonomy.get_children_count > 0 %}
{{ taxonomy }}
diff --git a/peachjam/views/article.py b/peachjam/views/article.py index 3a408319b..cced40e07 100644 --- a/peachjam/views/article.py +++ b/peachjam/views/article.py @@ -9,7 +9,12 @@ 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" @@ -17,14 +22,11 @@ class ArticleListView(ListView): 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"] = [ @@ -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] ) @@ -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 @@ -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")