diff --git a/liiweb/views/legislation.py b/liiweb/views/legislation.py index 63c2c6187..8c363c2d8 100644 --- a/liiweb/views/legislation.py +++ b/liiweb/views/legislation.py @@ -1,6 +1,7 @@ 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 _ @@ -72,7 +73,7 @@ def filter_queryset(self, qs): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) - context["documents"] = self.add_children(context["documents"]) + self.add_children(context["documents"]) site_jurisdictions = pj_settings().document_jurisdictions.all() if site_jurisdictions.count() == 1: @@ -89,6 +90,8 @@ def get_context_data(self, **kwargs): context["doc_table_show_author"] = False context["doc_table_show_jurisdiction"] = False + context["documents"] = self.group_documents(context["documents"]) + return context def add_children(self, queryset): @@ -108,7 +111,33 @@ def add_children(self, queryset): # fold in children for parent in queryset: parent.children = children.get(parent.work_id, []) - return queryset + + 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): diff --git a/peachjam/templates/peachjam/_document_table_row.html b/peachjam/templates/peachjam/_document_table_row.html index 0d7ffd2ed..189ac1ea3 100644 --- a/peachjam/templates/peachjam/_document_table_row.html +++ b/peachjam/templates/peachjam/_document_table_row.html @@ -14,16 +14,20 @@ {% endif %} {% endif %} - - {{ document.title }} - {% if document.work.languages|length > 1 %} - - - - {% endif %} - + {% if document.is_group %} + {{ document.title }} + {% else %} + + {{ document.title }} + {% if document.work.languages|length > 1 %} + + + + {% endif %} + + {% endif %} {% if doc_table_citations %}{{ document.citation|default_if_none:'' }}{% endif %} {% if doc_table_show_jurisdiction %} {% if MULTIPLE_JURISDICTIONS %} @@ -38,5 +42,5 @@ {% if doc_table_show_author %}{{ document.author|default_if_none:'' }}{% endif %} {% if doc_table_show_court %}{{ document.court|default_if_none:'' }}{% endif %} {% if doc_table_show_doc_type %}{{ document.get_doc_type_display }}{% endif %} - {{ document.date }} + {{ document.date|default_if_none:'' }}