Skip to content

Commit

Permalink
Add ability to cache court dates in the django postgres database (again)
Browse files Browse the repository at this point in the history
Re-applying this commit after reverting it -before merging we will need to make sure that migrations are run on deploy.

This stems from a discussion with Nick about ways of improving the current situation where we need to manually update the court metadata in the utils library every time a new judgment is published for a particular court in 2023.

Nick propose a cron job that populates the database with the start and end years for each court's current ingested range  on a daily or weekly basis - this handles the persistence layer in the app and the logic to choose the correct date range when displaying a court.
  • Loading branch information
timcowlishaw authored and dragon-dxw committed Feb 9, 2023
1 parent 0d8d162 commit 87a1a96
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 25 deletions.
13 changes: 3 additions & 10 deletions ds_judgements_public_ui/templates/includes/browse_by_court.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% load query_filters court_utils %}
{% block content %}
<div class="judgment-browse">
<h2 class="judgment-browse__header">Find judgments</h2>
Expand All @@ -8,11 +9,7 @@ <h3 id="browse-by-court" class="judgment-browse__sub-header">By court</h3>
<li class="judgment-browse__list-item">
<a class="judgment-browse__link" href="/judgments/advanced_search?court={{ court.canonical_param }}">{{ court.list_name }}</a>
<span class="judgment-browse__year-range">
{% if court.start_year == court.end_year %}
{{court.start_year}}
{% else %}
{{court.start_year}} &ndash; {{court.end_year}}
{% endif %}
{{ court|get_court_date_range }}
</span>
</li>
{% endfor %}
Expand All @@ -26,11 +23,7 @@ <h3 id="browse-by-tribunal" class="judgment-browse__sub-header">By tribunal</h3>
<li class="judgment-browse__list-item">
<a class="judgment-browse__link" href="/judgments/advanced_search?court={{ tribunal.canonical_param }}">{{ tribunal.list_name }}</a>
<span class="judgment-browse__year-range">
{% if tribunal.start_year == tribunal.end_year %}
{{tribunal.start_year}}
{% else %}
{{tribunal.start_year}} &ndash; {{tribunal.end_year}}
{% endif %}
{{ tribunal|get_court_date_range }}
</span>
</li>
{% endfor %}
Expand Down
14 changes: 3 additions & 11 deletions ds_judgements_public_ui/templates/pages/what_to_expect.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "base.html" %}
{% load i18n static %}

{% load query_filters court_utils %}

{% block robots %}
{% endblock robots %}
Expand Down Expand Up @@ -145,21 +145,13 @@ <h3>Date ranges for our coverage of courts and tribunals:</h3>
<span>{{group.name}}</span>
<ul>
{% for court in group.courts %}
{% if court.start_year == court.end_year %}
<li>{{court.list_name}} {{court.start_year}}</li>
{% else %}
<li>{{court.list_name}} {{court.start_year}} – {{court.end_year}}</li>
{% endif %}
<li>{{court.list_name}} {{court|get_court_date_range}}</li>
{% endfor %}
</ul>
</li>
{% else %}
{% for court in group.courts %}
{% if court.start_year == court.end_year %}
<li>{{court.list_name}} {{court.start_year}}</li>
{% else %}
<li>{{court.list_name}} {{court.start_year}} – {{court.end_year}}</li>
{% endif %}
<li>{{court.list_name}} {{court|get_court_date_range}}</li>
{% endfor %}
{% endif %}
{% endfor %}
Expand Down
22 changes: 22 additions & 0 deletions judgments/migrations/0001_create_court_dates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.2.14 on 2023-01-06 07:11

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='CourtDates',
fields=[
('param', models.CharField(max_length=64, primary_key=True, serialize=False)),
('start_year', models.IntegerField()),
('end_year', models.IntegerField()),
],
),
]
8 changes: 7 additions & 1 deletion judgments/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# from django.db import models
from os.path import dirname, join

from caselawclient.Client import api_client
from django.db import models
from djxml import xmlmodels
from lxml import etree

Expand Down Expand Up @@ -119,3 +119,9 @@ class Meta:
namespaces = {"search": "http://marklogic.com/appservices/search"}

transform_to_html = xmlmodels.XsltField(join(dirname(__file__), "search_match.xsl"))


class CourtDates(models.Model):
param = models.CharField(max_length=64, primary_key=True)
start_year = models.IntegerField(blank=False)
end_year = models.IntegerField(blank=False)
18 changes: 18 additions & 0 deletions judgments/templatetags/court_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from django import template
from django.utils.safestring import mark_safe
from ds_caselaw_utils import courts as all_courts

from judgments.models import CourtDates

register = template.Library()


Expand All @@ -10,3 +13,18 @@ def get_court_name(court):
if court_object is None:
return ""
return court_object.name


@register.filter
def get_court_date_range(court):
try:
court_dates = CourtDates.objects.get(pk=court.canonical_param)
start_year = court_dates.start_year
end_year = court_dates.end_year
except CourtDates.DoesNotExist:
start_year = court.start_year
end_year = court.end_year
if start_year == end_year:
return str(start_year)
else:
return mark_safe("%s &ndash; %s" % (start_year, end_year))
40 changes: 37 additions & 3 deletions judgments/tests.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import re
from unittest import skip
from unittest.mock import patch
from unittest.mock import Mock, patch

from django.test import TestCase
from lxml import etree

from judgments import converters, utils
from judgments.models import SearchResult, SearchResults
from judgments.templatetags.court_utils import get_court_name
from judgments.models import CourtDates, SearchResult, SearchResults
from judgments.templatetags.court_utils import get_court_date_range, get_court_name
from judgments.utils import as_integer, display_back_link, paginator


Expand Down Expand Up @@ -379,3 +379,37 @@ def test_get_court_name():

def test_get_court_name_non_existent():
assert get_court_name("ffff") == ""


@patch("judgments.templatetags.court_utils.CourtDates.objects.get")
class TestCourtDatesHelper(TestCase):
def mock_court_dates(self, start_year, end_year):
mock = Mock()
mock.configure_mock(start_year=start_year, end_year=end_year)
return mock

def test_when_court_with_param_exists_and_no_dates_in_db_and_start_end_same(
self, get
):
get.side_effect = CourtDates.DoesNotExist
court = self.mock_court_dates(2011, 2011)
self.assertEqual(get_court_date_range(court), "2011")

def test_when_court_with_param_exists_and_no_dates_in_db_and_start_end_different(
self, get
):
get.side_effect = CourtDates.DoesNotExist
court = self.mock_court_dates(2011, 2012)
self.assertEqual(get_court_date_range(court), "2011 &ndash; 2012")

def test_when_court_with_param_exists_and_dates_in_db_and_start_end_same(self, get):
get.return_value = self.mock_court_dates(2013, 2013)
court = self.mock_court_dates(2011, 2012)
self.assertEqual(get_court_date_range(court), "2013")

def test_when_court_with_param_exists_and_dates_in_db_and_start_end_different(
self, get
):
get.return_value = self.mock_court_dates(2013, 2015)
court = self.mock_court_dates(2011, 2012)
self.assertEqual(get_court_date_range(court), "2013 &ndash; 2015")

0 comments on commit 87a1a96

Please sign in to comment.