Skip to content

Commit

Permalink
grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
longhotsummer committed Jul 15, 2024
1 parent 8ecb05a commit 05520ad
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
33 changes: 31 additions & 2 deletions liiweb/views/legislation.py
Original file line number Diff line number Diff line change
@@ -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 _
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand All @@ -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):
Expand Down
26 changes: 15 additions & 11 deletions peachjam/templates/peachjam/_document_table_row.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@
{% endif %}
</td>
{% endif %}
<td class="cell-title">
<a href="{{ document.get_absolute_url }}">{{ document.title }}</a>
{% if document.work.languages|length > 1 %}
<span class="d-inline-block"
data-bs-toggle="tooltip"
title="{% trans 'Multiple languages available' %}">
<i class="bi bi-translate"></i>
</span>
{% endif %}
</td>
{% if document.is_group %}
<td class="cell-group">{{ document.title }}</td>
{% else %}
<td class="cell-title">
<a href="{{ document.get_absolute_url }}">{{ document.title }}</a>
{% if document.work.languages|length > 1 %}
<span class="d-inline-block"
data-bs-toggle="tooltip"
title="{% trans 'Multiple languages available' %}">
<i class="bi bi-translate"></i>
</span>
{% endif %}
</td>
{% endif %}
{% if doc_table_citations %}<td class="cell-citation">{{ document.citation|default_if_none:'' }}</td>{% endif %}
{% if doc_table_show_jurisdiction %}
{% if MULTIPLE_JURISDICTIONS %}
Expand All @@ -38,5 +42,5 @@
{% if doc_table_show_author %}<td>{{ document.author|default_if_none:'' }}</td>{% endif %}
{% if doc_table_show_court %}<td>{{ document.court|default_if_none:'' }}</td>{% endif %}
{% if doc_table_show_doc_type %}<td style="white-space: nowrap;">{{ document.get_doc_type_display }}</td>{% endif %}
<td style="white-space: nowrap;" class="cell-date">{{ document.date }}</td>
<td style="white-space: nowrap;" class="cell-date">{{ document.date|default_if_none:'' }}</td>
</tr>

0 comments on commit 05520ad

Please sign in to comment.