Skip to content

Commit

Permalink
fixes court view mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
actlikewill committed Apr 24, 2024
1 parent f675b93 commit 5a22f26
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 58 deletions.
6 changes: 6 additions & 0 deletions peachjam/models/judgment.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ class Meta:
def __str__(self):
return self.name

def get_absolute_url(self):
return reverse("court_class", args=[self.slug])

def save(self, *args, **kwargs):
self.slug = slugify(self.name)
return super().save(*args, **kwargs)
Expand Down Expand Up @@ -134,6 +137,9 @@ class Meta:
def __str__(self):
return self.name

def get_absolute_url(self):
return reverse("court", args=[self.code])


class CourtRegistryManager(models.Manager):
def get_queryset(self):
Expand Down
10 changes: 6 additions & 4 deletions peachjam/templates/peachjam/_court_class_month_list.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{% extends "peachjam/_court_months_list.html" %}
{% load i18n %}
{% block all_months_link %}
<a class="{% if not month %} active{% endif %}"
href="{% url 'court_class_year' court_class.slug year %}">{% trans 'All months' %}</a>
{% endblock %}
{% block month_link %}
<a href="{% url "court_class_month" court_class.slug year m|date:'n' %}">{{ m|date:"F" }}</a>
{% endblock %}
{% block month_option %}
<option value="{% url "court_class_month" court_class.slug year m|date:'n' %}"
{% if m|date:"F" == month %} selected{% endif %}>
{{ m|date:"F" }}
</option>
{% endblock %}
10 changes: 6 additions & 4 deletions peachjam/templates/peachjam/_court_class_years_list.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{% extends "peachjam/_court_years_list.html" %}
{% load i18n %}
{% block all_years_link %}
<a class="{% if not year %} active{% endif %}"
href="{% url 'court_class' court_class.slug %}">{% trans 'All years' %}</a>
{% endblock %}
{% block year_link %}
<a href="{% url "court_class_year" court_class.slug y.year %}">{{ y.year }}</a>
{% endblock %}
{% block year_option %}
<option value="{% url "court_class_year" court_class.slug y.year %}"
{% if y.year == year %} selected{% endif %}>
{{ y.year }}
</option>
{% endblock %}
4 changes: 2 additions & 2 deletions peachjam/templates/peachjam/_court_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ <h4 class="mb-2">{{ court_class }}</h4>
{% endfor %}
</ul>
{% if court_class.show_listing_page %}
<a class="btn btn-sm btn-info"
href="{% url 'court_class' court_class.slug %}">View All</a>
<a class="btn btn-sm btn-primary"
href="{% url 'court_class' court_class.slug %}">{% trans 'View all' %}</a>
{% endif %}
{% else %}
<div>{% trans 'No courts found.' %}</div>
Expand Down
15 changes: 8 additions & 7 deletions peachjam/templates/peachjam/_court_months_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
class="pl-0 mt-2 mb-3 align-items-center d-none d-lg-flex">
<i class="bi bi-calendar me-2"></i>
<div>
{% block all_months_link %}
<a class="{% if not month %} active{% endif %}"
href="{% url 'court_year' court.code year %}">{% trans 'All months' %}</a>
{% endblock %}
<a class="{% if not month %} active{% endif %}"
href="{{ all_months_url }}">{% trans 'All months' %}</a>
</div>
<ul class="year-nav mb-0 ms-2">
{% for m in months %}
Expand All @@ -29,9 +27,12 @@
{% trans 'All months' %}
</option>
{% for m in months %}
<option value="{{ m.url }}" {% if m.month == month %} selected{% endif %}>
{{ m.month }}
</option>
{% block month_option %}
<option value="{% url "court_month" court.code year m|date:'n' %}"
{% if m|date:"F" == month %} selected{% endif %}>
{{ m|date:"F" }}
</option>
{% endblock %}
{% endfor %}
</select>
{% endif %}
10 changes: 6 additions & 4 deletions peachjam/templates/peachjam/_court_registries_months_list.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{% extends "peachjam/_court_months_list.html" %}
{% load i18n %}
{% block all_months_link %}
<a class="{% if not month %} active{% endif %}"
href="{% url 'court_registry_year' court.code registry.code year %}">{% trans 'All months' %}</a>
{% endblock %}
{% block month_link %}
<a href="{% url "court_registry_month" court.code registry.code year m|date:'n' %}">{{ m|date:"F" }}</a>
{% endblock %}
{% block month_option %}
<option value="{% url "court_registry_month" court.code registry.code year m|date:'n' %}"
{% if m|date:"F" == month %} selected{% endif %}>
{{ m|date:"F" }}
</option>
{% endblock %}
10 changes: 6 additions & 4 deletions peachjam/templates/peachjam/_court_registries_years_list.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{% extends "peachjam/_court_years_list.html" %}
{% load i18n %}
{% block all_years_link %}
<a class="{% if not month %} active{% endif %}"
href="{% url 'court_registry' court.code registry.code %}">{% trans 'All months' %}</a>
{% endblock %}
{% block year_link %}
<a href="{% url "court_registry_year" court.code registry.code y.year %}">{{ y.year }}</a>
{% endblock %}
{% block year_option %}
<option value="{% url "court_registry_year" court.code registry.code y.year %}"
{% if y.year == year %} selected{% endif %}>
{{ y.year }}
</option>
{% endblock %}
15 changes: 8 additions & 7 deletions peachjam/templates/peachjam/_court_years_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
<div class="pl-0 mt-2 mb-3 align-items-center d-none d-lg-flex">
<i class="bi bi-calendar me-2"></i>
<div>
{% block all_years_link %}
<a class="{% if not year %} active{% endif %}"
href="{% url 'court' court.code %}">{% trans 'All years' %}</a>
{% endblock %}
<a class="{% if not year %} active{% endif %}"
href="{{ all_years_url }}">{% trans 'All years' %}</a>
</div>
<ul class="year-nav mb-0 ms-2">
{% for y in years %}
Expand All @@ -28,9 +26,12 @@
{% trans 'All years' %}
</option>
{% for y in years %}
<option value="{{ y.url }}" {% if y.year == year %} selected{% endif %}>
{{ y.year }}
</option>
{% block year_option %}
<option value="{% url "court_year" court.code y.year %}"
{% if y.year == year %} selected{% endif %}>
{{ y.year }}
</option>
{% endblock %}
{% endfor %}
</select>
{% endif %}
18 changes: 4 additions & 14 deletions peachjam/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,8 @@ def test_court_listing(self):
"Ababacar and Ors vs Senegal [2018] ECOWASCJ 17 (29 June 2018)",
documents,
)
self.assertEqual(
response.context["years"],
[
{"url": "/judgments/ECOWASCJ/2018/", "year": 2018},
{"url": "/judgments/ECOWASCJ/2016/", "year": 2016},
],
)
self.assertContains(response, "/judgments/ECOWASCJ/2018/")
self.assertContains(response, "/judgments/ECOWASCJ/2016/")
self.assertNotIn("years", response.context["facet_data"], [2016, 2018])

def test_court_year_listing(self):
Expand All @@ -79,13 +74,8 @@ def test_court_year_listing(self):
documents,
)
self.assertEqual(response.context["year"], 2016)
self.assertEqual(
response.context["years"],
[
{"url": "/judgments/ECOWASCJ/2018/", "year": 2018},
{"url": "/judgments/ECOWASCJ/2016/", "year": 2016},
],
)
self.assertContains(response, "/judgments/ECOWASCJ/2018/")
self.assertContains(response, "/judgments/ECOWASCJ/2016/")
self.assertNotIn("years", response.context["facet_data"], [2016, 2018])

def test_judgment_detail(self):
Expand Down
47 changes: 35 additions & 12 deletions peachjam/views/courts.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
from functools import cached_property
from itertools import groupby

from django.http import Http404
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.utils.dates import MONTHS
from django.utils.text import gettext_lazy as _

from peachjam.helpers import chunks, lowercase_alphabet
from peachjam.models import Court, CourtClass, CourtRegistry, Judgment
from peachjam.views.generic_views import FilteredDocumentListView


class BaseJudgmentFilterView(FilteredDocumentListView):
class FilteredJudgmentView(FilteredDocumentListView):
"""Base List View class for filtering judgments."""

model = Judgment
navbar_link = "judgments"
queryset = Judgment.objects.prefetch_related("judges", "labels")

def base_view_name(self):
return "Judgments"
return _("Judgments")

def page_title(self):
return self.base_view_name()
Expand Down Expand Up @@ -90,10 +93,10 @@ def grouped_judgments(self, documents):
return [{"key": key, "judgments": list(group)} for key, group in groups]


class CourtDetailView(BaseJudgmentFilterView):
class CourtDetailView(FilteredJudgmentView):
template_name = "peachjam/court_detail.html"

@property
@cached_property
def court(self):
return get_object_or_404(Court, code=self.kwargs.get("code"))

Expand All @@ -112,6 +115,7 @@ def get_context_data(self, **kwargs):
judgments__isnull=True
) # display registries with judgments only
context["registry_groups"] = list(chunks(context["registries"], 2))
context["all_years_url"] = self.court.get_absolute_url()
return context


Expand Down Expand Up @@ -144,7 +148,12 @@ def populate_months(self, context):


class CourtYearView(YearMixin, CourtDetailView):
pass
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["all_months_url"] = reverse(
"court_year", args=[self.court.code, self.year]
)
return context


class MonthMixin:
Expand Down Expand Up @@ -181,7 +190,7 @@ class RegistryMixin:
def base_view_name(self):
return self.registry.name

@property
@cached_property
def registry(self):
return get_object_or_404(CourtRegistry, code=self.kwargs.get("registry_code"))

Expand All @@ -191,28 +200,34 @@ def get_base_queryset(self, *args, **kwargs):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["registry"] = self.registry
context["all_years_url"] = self.registry.get_absolute_url()
return context


class CourtRegistryDetailView(RegistryMixin, CourtDetailView):
pass


class CourtRegistryYearView(RegistryMixin, CourtYearView):
pass
class CourtRegistryYearView(YearMixin, CourtRegistryDetailView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["all_months_url"] = reverse(
"court_registry_year", args=[self.court.code, self.registry.code, self.year]
)
return context


class CourtRegistryMonthView(RegistryMixin, CourtMonthView):
class CourtRegistryMonthView(MonthMixin, CourtRegistryYearView):
pass


class CourtClassDetailView(BaseJudgmentFilterView):
class CourtClassDetailView(FilteredJudgmentView):
template_name = "peachjam/court_class_detail.html"

def base_view_name(self):
return self.court_class.name

@property
@cached_property
def court_class(self):
return get_object_or_404(CourtClass, slug=self.kwargs["court_class"])

Expand All @@ -227,12 +242,20 @@ def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["court_class"] = self.court_class
context["registries"] = Court.objects.filter(court_class=self.court_class)
context["registry_label_plural"] = _("Courts")
context["registry_groups"] = list(chunks(context["registries"], 2))
context["all_years_url"] = self.court_class.get_absolute_url()

return context


class CourtClassYearView(YearMixin, CourtClassDetailView):
pass
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["all_months_url"] = reverse(
"court_class_year", args=[self.court_class.slug, self.year]
)
return context


class CourtClassMonthView(MonthMixin, CourtClassYearView):
Expand Down

0 comments on commit 5a22f26

Please sign in to comment.