Skip to content

Commit

Permalink
Merge pull request #1905 from laws-africa/grouping
Browse files Browse the repository at this point in the history
Grouping and latest expression only
  • Loading branch information
longhotsummer authored Jul 15, 2024
2 parents 6ef34ca + 8440f35 commit 1816f3d
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 119 deletions.
4 changes: 2 additions & 2 deletions lawlibrary/views/legislation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@


class LegislationListView(BaseLegislationListView):
def get_queryset(self):
return super().get_queryset().filter(locality=None)
def get_base_queryset(self):
return super().get_base_queryset().filter(locality=None)


class LocalityLegislationView(BaseLocalityLegislationView):
Expand Down
50 changes: 2 additions & 48 deletions liiweb/views/legislation.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
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 _
from django.views.generic import TemplateView

from peachjam.helpers import chunks, get_language
from peachjam.helpers import chunks
from peachjam.models import JurisdictionProfile, Legislation, Locality, pj_settings
from peachjam.views import FilteredDocumentListView

Expand All @@ -20,27 +19,9 @@ class LegislationListView(FilteredDocumentListView):
queryset = Legislation.objects.prefetch_related(
"taxonomies", "taxonomies__topic", "work"
)
latest_expression_only = True
extra_context = {"doc_table_citations": True, "legislation_list_sort": "title"}

def get_queryset(self):
qs = super().get_queryset()
qs = (
qs.exclude(published=False)
.distinct(
"title",
"work_frbr_uri",
)
.order_by(
"title",
"work_frbr_uri",
"-date",
"language__pk",
)
.preferred_language(get_language(self.request))
)

return qs

def get_template_names(self):
if self.request.htmx:
return ["peachjam/_document_table.html"]
Expand Down Expand Up @@ -112,33 +93,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
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"eslint-plugin-standard": "^4.0.1",
"eslint-plugin-vue": "^8.5.0",
"flag-icons": "^6.6.6",
"htmx.org": "^2.0.1",
"i18next": "^22.0.6",
"i18next-parser": "^6.5.0",
"lodash.debounce": "^4.0.8",
Expand Down
1 change: 1 addition & 0 deletions peachjam/js/peachjam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import '@lawsafrica/law-widgets/dist/components/la-table-of-contents-controller'
import '@lawsafrica/law-widgets/dist/components/la-decorate-external-refs';
import '@lawsafrica/law-widgets/dist/components/la-decorate-internal-refs';
import '@lawsafrica/law-widgets/dist/components/la-decorate-terms';
import 'htmx.org';

export interface PeachJamConfig {
appName: string;
Expand Down
2 changes: 1 addition & 1 deletion peachjam/static/js/app-prod.js

Large diffs are not rendered by default.

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 %}
3 changes: 0 additions & 3 deletions peachjam/templates/peachjam/layouts/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
type="text/css"/>
{% endblock %}
{% block head-js %}
<script src="https://unpkg.com/[email protected]"
integrity="sha384-wS5l5IKJBvK6sPTKa2WZ1js3d947pvWXbPJ1OmWfEuxLgeHcEbjUUA5i9V5ZkpCw"
crossorigin="anonymous"></script>
{{ PEACHJAM_JS_CONFIG|json_script:"peachjam-config" }}
{% if not DEBUG %}
<!-- sentry -->
Expand Down
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 1816f3d

Please sign in to comment.