From 87a1a962d599ba305094cb766c60747bb4763ec8 Mon Sep 17 00:00:00 2001 From: Tim Cowlishaw Date: Fri, 6 Jan 2023 09:26:01 +0100 Subject: [PATCH] Add ability to cache court dates in the django postgres database (again) 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. --- .../templates/includes/browse_by_court.html | 13 ++---- .../templates/pages/what_to_expect.html | 14 ++----- .../migrations/0001_create_court_dates.py | 22 ++++++++++ judgments/models.py | 8 +++- judgments/templatetags/court_utils.py | 18 +++++++++ judgments/tests.py | 40 +++++++++++++++++-- 6 files changed, 90 insertions(+), 25 deletions(-) create mode 100644 judgments/migrations/0001_create_court_dates.py diff --git a/ds_judgements_public_ui/templates/includes/browse_by_court.html b/ds_judgements_public_ui/templates/includes/browse_by_court.html index eed8030a1..5713f9ac7 100644 --- a/ds_judgements_public_ui/templates/includes/browse_by_court.html +++ b/ds_judgements_public_ui/templates/includes/browse_by_court.html @@ -1,3 +1,4 @@ +{% load query_filters court_utils %} {% block content %}

Find judgments

@@ -8,11 +9,7 @@

By court

  • {{ court.list_name }} - {% if court.start_year == court.end_year %} - {{court.start_year}} - {% else %} - {{court.start_year}} – {{court.end_year}} - {% endif %} + {{ court|get_court_date_range }}
  • {% endfor %} @@ -26,11 +23,7 @@

    By tribunal

  • {{ tribunal.list_name }} - {% if tribunal.start_year == tribunal.end_year %} - {{tribunal.start_year}} - {% else %} - {{tribunal.start_year}} – {{tribunal.end_year}} - {% endif %} + {{ tribunal|get_court_date_range }}
  • {% endfor %} diff --git a/ds_judgements_public_ui/templates/pages/what_to_expect.html b/ds_judgements_public_ui/templates/pages/what_to_expect.html index 9bd8c5dd4..229ce225b 100644 --- a/ds_judgements_public_ui/templates/pages/what_to_expect.html +++ b/ds_judgements_public_ui/templates/pages/what_to_expect.html @@ -1,6 +1,6 @@ {% extends "base.html" %} {% load i18n static %} - +{% load query_filters court_utils %} {% block robots %} {% endblock robots %} @@ -145,21 +145,13 @@

    Date ranges for our coverage of courts and tribunals:

    {{group.name}} {% else %} {% for court in group.courts %} - {% if court.start_year == court.end_year %} -
  • {{court.list_name}} {{court.start_year}}
  • - {% else %} -
  • {{court.list_name}} {{court.start_year}} – {{court.end_year}}
  • - {% endif %} +
  • {{court.list_name}} {{court|get_court_date_range}}
  • {% endfor %} {% endif %} {% endfor %} diff --git a/judgments/migrations/0001_create_court_dates.py b/judgments/migrations/0001_create_court_dates.py new file mode 100644 index 000000000..eb409a63f --- /dev/null +++ b/judgments/migrations/0001_create_court_dates.py @@ -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()), + ], + ), + ] diff --git a/judgments/models.py b/judgments/models.py index ff7c6f0a5..df96a8cdb 100644 --- a/judgments/models.py +++ b/judgments/models.py @@ -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 @@ -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) diff --git a/judgments/templatetags/court_utils.py b/judgments/templatetags/court_utils.py index c9efc0fb4..ed7e4309a 100644 --- a/judgments/templatetags/court_utils.py +++ b/judgments/templatetags/court_utils.py @@ -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() @@ -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 – %s" % (start_year, end_year)) diff --git a/judgments/tests.py b/judgments/tests.py index 5b4b0bb3e..a2a0e8209 100644 --- a/judgments/tests.py +++ b/judgments/tests.py @@ -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 @@ -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 – 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 – 2015")