From 5bacb671b656d7251c9c47af28c0f75740612742 Mon Sep 17 00:00:00 2001 From: Greg Kempe Date: Mon, 15 Jul 2024 12:57:36 +0200 Subject: [PATCH] common grouping mechanism for courts --- liiweb/views/legislation.py | 28 ------------- .../peachjam/_document_table_row.html | 1 + .../peachjam/_grouped_judgments_table.html | 21 ---------- .../templates/peachjam/_judgment_table.html | 22 ----------- peachjam/templates/peachjam/court_detail.html | 11 ------ peachjam/views/courts.py | 15 ++----- peachjam/views/generic_views.py | 39 +++++++++++++++++++ 7 files changed, 44 insertions(+), 93 deletions(-) delete mode 100644 peachjam/templates/peachjam/_grouped_judgments_table.html delete mode 100644 peachjam/templates/peachjam/_judgment_table.html diff --git a/liiweb/views/legislation.py b/liiweb/views/legislation.py index 8c363c2d8..cd5fcd5f2 100644 --- a/liiweb/views/legislation.py +++ b/liiweb/views/legislation.py @@ -1,7 +1,6 @@ import datetime from collections import defaultdict from datetime import timedelta -from itertools import groupby from django.shortcuts import get_object_or_404 from django.utils.translation import gettext_lazy as _ @@ -112,33 +111,6 @@ def add_children(self, queryset): for parent in queryset: parent.children = children.get(parent.work_id, []) - def group_documents(self, documents): - # TODO: move this down into the base class - - # determine what to group by - ordering = documents.query.order_by[0] - if ordering.startswith("-"): - ordering = ordering[1:] - - def grouper(d): - if ordering == "date": - return d.date.year - else: - return d.title[0].upper() - - class Group: - is_group = True - - def __init__(self, title): - self.title = title - - docs = [] - for key, group in groupby(documents, grouper): - docs.append(Group(key)) - docs.extend(group) - - return docs - class LocalityLegislationView(TemplateView): template_name = "liiweb/locality_legislation.html" diff --git a/peachjam/templates/peachjam/_document_table_row.html b/peachjam/templates/peachjam/_document_table_row.html index 189ac1ea3..aac3d4499 100644 --- a/peachjam/templates/peachjam/_document_table_row.html +++ b/peachjam/templates/peachjam/_document_table_row.html @@ -26,6 +26,7 @@ {% endif %} + {% include 'peachjam/_labels.html' with labels=document.labels.all %} {% endif %} {% if doc_table_citations %}{{ document.citation|default_if_none:'' }}{% endif %} diff --git a/peachjam/templates/peachjam/_grouped_judgments_table.html b/peachjam/templates/peachjam/_grouped_judgments_table.html deleted file mode 100644 index 8aacfde20..000000000 --- a/peachjam/templates/peachjam/_grouped_judgments_table.html +++ /dev/null @@ -1,21 +0,0 @@ -{% extends 'peachjam/_judgment_table.html' %} -{% block table-body %} - - {% for item in grouped_documents %} - {% if item.judgments %} - - {{ item.key }} - - {% for judgment in item.judgments %} - - - {{ judgment.title }} - {% include 'peachjam/_labels.html' with labels=judgment.labels.all %} - - {{ judgment.date }} - - {% endfor %} - {% endif %} - {% endfor %} - -{% endblock %} diff --git a/peachjam/templates/peachjam/_judgment_table.html b/peachjam/templates/peachjam/_judgment_table.html deleted file mode 100644 index 88b15509f..000000000 --- a/peachjam/templates/peachjam/_judgment_table.html +++ /dev/null @@ -1,22 +0,0 @@ -{% load i18n %} - - - - - - - - {% block table-body %} - - {% for document in documents %} - - - - - {% endfor %} - - {% endblock %} -
{% trans 'Title' %}{% trans 'Date' %}
- {{ document.title }} - {% include 'peachjam/_labels.html' with labels=document.labels.all %} - {{ document.date }}
diff --git a/peachjam/templates/peachjam/court_detail.html b/peachjam/templates/peachjam/court_detail.html index 5eed9cbdb..7bc1ba7f6 100644 --- a/peachjam/templates/peachjam/court_detail.html +++ b/peachjam/templates/peachjam/court_detail.html @@ -51,14 +51,3 @@

{{ page_title }}

{% include 'peachjam/_court_months_list.html' %} {% endblock %} {% endblock %} -{% block content %} - {% if grouped_documents %} - {% include 'peachjam/_grouped_judgments_table.html' %} - {% include 'peachjam/_pagination.html' %} - {% elif documents %} - {% include 'peachjam/_judgment_table.html' %} - {% include 'peachjam/_pagination.html' %} - {% else %} -

{% trans 'No documents found.' %}

- {% endif %} -{% endblock %} diff --git a/peachjam/views/courts.py b/peachjam/views/courts.py index 584531aef..f9a02b20a 100644 --- a/peachjam/views/courts.py +++ b/peachjam/views/courts.py @@ -1,5 +1,4 @@ from functools import cached_property -from itertools import groupby from math import ceil from django.http import Http404 @@ -22,6 +21,7 @@ class FilteredJudgmentView(FilteredDocumentListView): "judges", "labels", "attorneys", "outcomes" ) exclude_facets = [] + group_by_date = "month-year" def base_view_name(self): return _("Judgments") @@ -35,13 +35,13 @@ def get_context_data(self, **kwargs): context["doc_type"] = "Judgment" context["page_title"] = self.page_title() context["labels"].update({"judge": Judge.model_label_plural}) - - if not self.form.cleaned_data.get("alphabet"): - context["grouped_documents"] = self.grouped_judgments(context["documents"]) + context["doc_table_show_jurisdiction"] = False self.populate_years(context) self.populate_facets(context) + context["documents"] = self.group_documents(context["documents"]) + return context def populate_facets(self, context): @@ -93,13 +93,6 @@ def populate_years(self, context): "date", "year", order="DESC" ) - def grouped_judgments(self, documents): - """Group the judgments by month and return a list of dicts with the month name and judgments for that month""" - # Group documents by month - groups = groupby(documents, lambda d: f"{MONTHS[d.date.month]} {d.date.year}") - - return [{"key": key, "judgments": list(group)} for key, group in groups] - class CourtDetailView(FilteredJudgmentView): template_name = "peachjam/court_detail.html" diff --git a/peachjam/views/generic_views.py b/peachjam/views/generic_views.py index ebc97e0e6..4e665a1fc 100644 --- a/peachjam/views/generic_views.py +++ b/peachjam/views/generic_views.py @@ -3,6 +3,7 @@ from django.http.response import HttpResponse from django.middleware.csrf import get_token from django.shortcuts import get_object_or_404 +from django.utils.dates import MONTHS from django.views.generic import DetailView, ListView, View from lxml import html @@ -34,6 +35,9 @@ class DocumentListView(ListView): "nature", "work", "jurisdiction", "locality" ) + # when grouping by date, group by year, or month and year? ("year" and "month-year" are the only options) + group_by_date = "year" + def get_base_queryset(self, *args, **kwargs): qs = self.queryset if self.queryset is not None else self.model.objects return qs.filter(published=True) @@ -47,6 +51,32 @@ def get_context_data(self, *args, **kwargs): doc_table_show_jurisdiction=True, *args, **kwargs ) + def group_documents(self, documents, group_by): + if not group_by: + return documents + + def grouper(d): + if group_by == "date": + if self.group_by_date == "month-year": + return f"{MONTHS[d.date.month]} {d.date.year}" + else: + return d.date.year + elif group_by == "title": + return d.title[0].upper() + + class Group: + is_group = True + + def __init__(self, title): + self.title = title + + docs = [] + for key, group in itertools.groupby(documents, grouper): + docs.append(Group(key)) + docs.extend(group) + + return docs + class FilteredDocumentListView(DocumentListView): """Generic list view for filtered document lists.""" @@ -133,6 +163,15 @@ def add_facets(self, context): "natures": natures, } + def group_documents(self, documents, group_by=None): + # determine what to group by + if group_by is None: + group_by = documents.query.order_by[0] + if group_by.startswith("-"): + group_by = group_by[1:] + + return super().group_documents(documents, group_by) + class BaseDocumentDetailView(DetailView): slug_field = "expression_frbr_uri"