Skip to content

Commit

Permalink
common grouping mechanism for courts
Browse files Browse the repository at this point in the history
  • Loading branch information
longhotsummer committed Jul 15, 2024
1 parent 6ef34ca commit 5bacb67
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 93 deletions.
28 changes: 0 additions & 28 deletions liiweb/views/legislation.py
Original file line number Diff line number Diff line change
@@ -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 _
Expand Down Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions peachjam/templates/peachjam/_document_table_row.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<i class="bi bi-translate"></i>
</span>
{% endif %}
{% include 'peachjam/_labels.html' with labels=document.labels.all %}
</td>
{% endif %}
{% if doc_table_citations %}<td class="cell-citation">{{ document.citation|default_if_none:'' }}</td>{% endif %}
Expand Down
21 changes: 0 additions & 21 deletions peachjam/templates/peachjam/_grouped_judgments_table.html

This file was deleted.

22 changes: 0 additions & 22 deletions peachjam/templates/peachjam/_judgment_table.html

This file was deleted.

11 changes: 0 additions & 11 deletions peachjam/templates/peachjam/court_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,3 @@ <h1 class="mt-4">{{ page_title }}</h1>
{% 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 %}
<p>{% trans 'No documents found.' %}</p>
{% endif %}
{% endblock %}
15 changes: 4 additions & 11 deletions peachjam/views/courts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from functools import cached_property
from itertools import groupby
from math import ceil

from django.http import Http404
Expand All @@ -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")
Expand All @@ -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):
Expand Down Expand Up @@ -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"
Expand Down
39 changes: 39 additions & 0 deletions peachjam/views/generic_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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."""
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 5bacb67

Please sign in to comment.