Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fcl 90 Use codes not params in search. #1377

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/views/courts.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def get_context_data(self, **kwargs):

search_response = search_judgments_and_parse_response(
api_client,
SearchParameters(court=self.court.canonical_param, order="-date", page=self.page),
SearchParameters(court=self.court.code, order="-date", page=self.page),
)

context["feedback_survey_type"] = "court_or_tribunal_%s" % self.court.canonical_param
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ <h3 id="browse-by-court" class="judgment-browse__sub-header">By court</h3>
{% for court in courts %}
<li class="judgment-browse__list-item">
<a class="judgment-browse__link"
href="{% url "search" %}?court={{ court.canonical_param }}">{{ court.name }}</a>
href="{% url "search" %}?court={{ court.code }}">{{ court.name }}</a>
<span class="judgment-browse__year-range">&mdash; {{ court.canonical_param|get_court_date_range }}</span>
</li>
{% endfor %}
Expand All @@ -24,7 +24,7 @@ <h3 id="browse-by-tribunal" class="judgment-browse__sub-header">By tribunal</h3>
{% for tribunal in tribunals %}
<li class="judgment-browse__list-item">
<a class="judgment-browse__link"
href="{% url "search" %}?tribunal={{ tribunal.canonical_param }}">{{ tribunal.name }}</a>
href="{% url "search" %}?tribunal={{ tribunal.code }}">{{ tribunal.name }}</a>
<span class="judgment-browse__year-range">&mdash; {{ tribunal.canonical_param|get_court_date_range }}</span>
</li>
{% endfor %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% load court_utils %}
<div>
<h4>
<a href="{% url "search" %}?{{ type }}={{ court.canonical_param }}">{{ court.grouped_name }}</a>
<a href="{% url "search" %}?{{ type }}={{ court.code }}">{{ court.grouped_name }}</a>
</h4>
<p>
{{ court | get_court_judgments_count }} documents from
Expand Down
5 changes: 3 additions & 2 deletions judgments/forms/search_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ def _get_choices_by_group(courts):
"key2": "value2"
}
"""
# DRAGON
options: dict = {}
for group in courts:
if group.display_heading:
option = {group.name: {court.canonical_param: court.grouped_name for court in group.courts}}
option = {group.name: {court.code: court.grouped_name for court in group.courts}}
else:
option = {court.canonical_param: court.grouped_name for court in group.courts}
option = {court.code: court.grouped_name for court in group.courts}
options = options | option
return options

Expand Down
46 changes: 32 additions & 14 deletions judgments/templatetags/court_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,28 @@

from judgments.models.court_dates import CourtDates
from judgments.utils import api_client
from ds_caselaw_utils.courts import Court, courts
from typing import Optional

register = template.Library()


@register.filter
def get_court_name(court):
def get_court_name(court_param: str) -> str:
try:
court_object = all_courts.get_by_param(court)
court_object = all_courts.get_by_param(court_param)
return court_object.name
except CourtNotFoundException:
pass
try:
court_object = all_courts.get_by_code(court)
court_object = all_courts.get_by_code(court_param)
return court_object.name
except CourtNotFoundException:
return ""


@register.simple_tag
def get_first_judgment_year():
def get_first_judgment_year() -> int:
if min_year := CourtDates.min_year():
return min_year
else:
Expand All @@ -44,7 +46,7 @@ def get_first_judgment_year():


@register.simple_tag
def get_last_judgment_year():
def get_last_judgment_year() -> int:
if max_year := CourtDates.max_year():
return max_year
else:
Expand All @@ -55,45 +57,61 @@ def get_last_judgment_year():


@register.filter
def get_court_date_range(court):
def get_court_date_range(court_param: str) -> str:
# DRAGON
# Currently -- and this should be fixed -- the court_param can be a court code.
# Convert those cases. This code is not intended to be permanent.
try:
court_dates = CourtDates.objects.get(pk=court)
court = courts.get_court_by_code(court_param)
except CourtNotFoundException:
pass
else:
court_param = court.canonical_param

# END of impermanent code

try:
court_dates = CourtDates.objects.get(pk=court_param)
start_year = court_dates.start_year
end_year = court_dates.end_year
except CourtDates.DoesNotExist:
start_year = all_courts.get_by_param(court).start_year
end_year = all_courts.get_by_param(court).end_year
start_year = all_courts.get_by_param(court_param).start_year
end_year = all_courts.get_by_param(court_param).end_year
if start_year == end_year:
return str(start_year)
else:
return mark_safe("%s&nbsp;to&nbsp;%s" % (start_year, end_year))


@register.filter
def get_court_judgments_count(court):
return search_judgments_and_parse_response(api_client, SearchParameters(court=court.canonical_param)).total
def get_court_judgments_count(court: Court) -> str:
return search_judgments_and_parse_response(api_client, SearchParameters(court=court.code)).total


@register.filter
def get_court_intro_text(court):
def read_markdown(param):
def get_court_intro_text(court: Court) -> Optional[str]:
def read_markdown(param: str) -> Optional[str]:
filename = param.replace("/", "_")
base_path = r"markdown/court_descriptions/{filename}.md"
path = finders.find(base_path.format(filename=filename))
md = MarkdownIt("commonmark", {"breaks": True, "html": True}).use(attrs_plugin)
if path:
with open(path) as file:
return md.render(file.read())
return None

# DRAGON: check if this is acceptable in the future -- uses param as court proxy
return read_markdown(court.canonical_param) or read_markdown("default")


@register.filter
def get_court_crest_path(court):
def get_court_crest_path(court: Court) -> Optional[str]:
# DRAGON: check if acceptable in the future -- uses param as court proxy
param = court.canonical_param
filename = param.replace("/", "_")
base_path = r"images/court_crests/{filename}.{extension}"
for extension in ["gif", "png", "jpg", "svg"]:
path = base_path.format(filename=filename, extension=extension)
if finders.find(path):
return static(path)
return None
3 changes: 3 additions & 0 deletions judgments/tests/test_courts.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ def test_when_court_with_param_exists_and_dates_in_db_and_start_end_different(se
self.assertEqual(get_court_date_range(court), "2013&nbsp;to&nbsp;2015")


# DRAGON -- this entire test file uses params throughout, for lookups


class TestCourtContentHelpers(TestCase):
def mock_court_param(self, param):
mock = Mock()
Expand Down
26 changes: 16 additions & 10 deletions judgments/tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
assert_response_not_contains_text,
)

# DRAGON -- all these tests with ewhc/ch etc aren't testing the right thing.
# We also need to make sure that edge cases like the QB/KB work
# We also need to make sure Marklogic is updated to have the correct behaviour
# Should we support old search URLs too?
# The search form raises an error for ewhc/ch now, but doesn't highlight where (because there is nowhere)


class TestBrowseResults(TestCase):
@patch("judgments.views.browse.api_client")
Expand Down Expand Up @@ -220,7 +226,7 @@ def test_judgment_advanced_search_court_filters(
):
"""
GIVEN a client for making HTTP requests
WHEN a GET request is made to "/judgments/search?court=ewhc/ch&court=ewhc/ipec"
WHEN a GET request is made to "/judgments/search?court=EWHC-Chancery&court=EWHC-Chancery-IPEC"
THEN the response should contain the expected applied filters HTML
AND the `search_judgments_and_parse_response` function should be called with the correct court string.

Expand All @@ -239,7 +245,7 @@ def test_judgment_advanced_search_court_filters(
tabindex="0"
draggable="false"
class="results-search-component__removable-options-link"
href="/judgments/search?query=&amp;court=ewhc/ipec&amp;judge=&amp;party=&amp;order=-date&amp;page="
href="/judgments/search?query=&amp;court=EWHC-Chancery-IPEC&amp;judge=&amp;party=&amp;order=-date&amp;page="
title="High Court (Chancery Division)">
<span class="results-search-component__removable-options-value">
<span class="results-search-component__removable-options-value-text">
Expand All @@ -254,7 +260,7 @@ def test_judgment_advanced_search_court_filters(
tabindex="0"
draggable="false"
class="results-search-component__removable-options-link"
href="/judgments/search?query=&amp;court=ewhc/ch&amp;judge=&amp;party=&amp;order=-date&amp;page="
href="/judgments/search?query=&amp;court=EWHC-Chancery&amp;judge=&amp;party=&amp;order=-date&amp;page="
title="High Court (Intellectual Property Enterprise Court)">
<span class="results-search-component__removable-options-value">
<span class="results-search-component__removable-options-value-text">
Expand All @@ -265,13 +271,13 @@ def test_judgment_advanced_search_court_filters(
</li>
</ul>
"""
response = self.client.get("/judgments/search?court=ewhc/ch&court=ewhc/ipec")
response = self.client.get("/judgments/search?court=EWHC-Chancery&court=EWHC-Chancery-IPEC")

mock_search_judgments_and_parse_response.assert_called_with(
mock_api_client,
SearchParameters(
query="",
court="ewhc/ch,ewhc/ipec",
court="EWHC-Chancery,EWHC-Chancery-IPEC",
order="-date",
judge="",
party="",
Expand Down Expand Up @@ -307,7 +313,7 @@ def test_judgment_advanced_search_court_filters_with_from_date(
mock_search_judgments_and_parse_response.return_value = FakeSearchResponse()

response = self.client.get(
"/judgments/search?court=ewhc/ch&court=ewhc/ipec&from_date_0=1&from_date_1=1&from_date_2=2011"
"/judgments/search?court=EWHC-Chancery&court=EWHC-Chancery-IPEC&from_date_0=1&from_date_1=1&from_date_2=2011"
)

expected_applied_filters_html = """
Expand All @@ -317,7 +323,7 @@ def test_judgment_advanced_search_court_filters_with_from_date(
tabindex="0"
draggable="false"
class="results-search-component__removable-options-link"
href="/judgments/search?query=&amp;court=ewhc/ch&amp;court=ewhc/ipec&amp;judge=&amp;party=&amp;order=-date&amp;page=">
href="/judgments/search?query=&amp;court=EWHC-Chancery&amp;court=EWHC-Chancery-IPEC&amp;judge=&amp;party=&amp;order=-date&amp;page=">
<span class="results-search-component__removable-options-key">From:</span>
<span class="results-search-component__removable-options-value">
<span class="results-search-component__removable-options-value-text"> 01 Jan 2011</span>
Expand All @@ -329,7 +335,7 @@ def test_judgment_advanced_search_court_filters_with_from_date(
tabindex="0"
draggable="false"
class="results-search-component__removable-options-link"
href="/judgments/search?from_date_0=1&amp;from_date_1=1&amp;from_date_2=2011&amp;query=&amp;court=ewhc/ipec&amp;judge=&amp;party=&amp;order=-date&amp;page="
href="/judgments/search?from_date_0=1&amp;from_date_1=1&amp;from_date_2=2011&amp;query=&amp;court=EWHC-Chancery-IPEC&amp;judge=&amp;party=&amp;order=-date&amp;page="
title="High Court (Chancery Division)">
<span class="results-search-component__removable-options-value">
<span class="results-search-component__removable-options-value-text">
Expand All @@ -343,7 +349,7 @@ def test_judgment_advanced_search_court_filters_with_from_date(
tabindex="0"
draggable="false"
class="results-search-component__removable-options-link"
href="/judgments/search?from_date_0=1&amp;from_date_1=1&amp;from_date_2=2011&amp;query=&amp;court=ewhc/ch&amp;judge=&amp;party=&amp;order=-date&amp;page="
href="/judgments/search?from_date_0=1&amp;from_date_1=1&amp;from_date_2=2011&amp;query=&amp;court=EWHC-Chancery&amp;judge=&amp;party=&amp;order=-date&amp;page="
title="High Court (Intellectual Property Enterprise Court)">
<span class="results-search-component__removable-options-value">
<span class="results-search-component__removable-options-value-text">
Expand All @@ -359,7 +365,7 @@ def test_judgment_advanced_search_court_filters_with_from_date(
mock_api_client,
SearchParameters(
query="",
court="ewhc/ch,ewhc/ipec",
court="EWHC-Chancery,EWHC-Chancery-IPEC",
order="-date",
judge="",
party="",
Expand Down
4 changes: 2 additions & 2 deletions judgments/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ def test_paginator_5(self):
@patch("judgments.views.advanced_search.search_judgments_and_parse_response")
def test_pagination_links(self, mock_search_judgments_and_parse_response):
mock_search_judgments_and_parse_response.return_value = FakeSearchResponse()
response = self.client.get("/judgments/search?tribunal=ukut/iac&order=&page=3")
response = self.client.get("/judgments/search?tribunal=UKUT-IAC&order=&page=3")
decoded_response = response.content.decode("utf-8")
self.assertIn(
"/judgments/search?tribunal=ukut%2Fiac&amp;order=&page=4",
"/judgments/search?tribunal=UKUT-IAC&amp;order=&page=4",
decoded_response,
)

Expand Down
Loading