Skip to content

Commit

Permalink
TP2000-959 Add sorting to the MeasuresList filter results table (#1034)
Browse files Browse the repository at this point in the history
* Add sorting to results table

* Add sorting for end dates

* Fix existing tests

* Add tests for Sort by

* Change query param arg to bool

* Remove the string check for ordering in get queryset

* Rename anchor to fragment

* Rename sort_by to create-sortable_anchor

* Fix broken tests
  • Loading branch information
LaurenMullally authored Oct 3, 2023
1 parent 1695287 commit 949f547
Show file tree
Hide file tree
Showing 20 changed files with 231 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from "components/sort_by.jinja" import sort_by %}
{% from "components/create_sortable_anchor.jinja" import create_sortable_anchor %}
{% from "components/table/macro.njk" import govukTable %}

{% set table_rows = [] %}
Expand Down Expand Up @@ -37,4 +37,4 @@
{% endif %}
</div>
{% include "includes/common/actions.jinja"%}
</div>
</div>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "commodities/detail.jinja" %}
{% from "components/sort_by.jinja" import sort_by %}
{% from "components/create_sortable_anchor.jinja" import create_sortable_anchor %}

{% set extra_tabs_links = [
{
Expand Down Expand Up @@ -65,19 +65,19 @@
{% set base_url = url('commodity-ui-detail-measures-declarable', args=[commodity.sid]) %}

{% set comm_code %}
{{ sort_by(request, "commodity", "Commodity code", base_url) }}
{{ create_sortable_anchor(request, "commodity", "Commodity code", base_url) }}
{% endset %}

{% set measure_type %}
{{ sort_by(request, "measure_type", "Measure type", base_url) }}
{{ create_sortable_anchor(request, "measure_type", "Measure type", base_url) }}
{% endset %}

{% set geo_area %}
{{ sort_by(request, "geo_area", "Geographical area", base_url) }}
{{ create_sortable_anchor(request, "geo_area", "Geographical area", base_url) }}
{% endset %}

{% set start_date %}
{{ sort_by(request, "start_date", "Start date", base_url) }}
{{ create_sortable_anchor(request, "start_date", "Start date", base_url) }}
{% endset %}

{% if paginator.count > 0 %}
Expand All @@ -100,4 +100,4 @@
{% endif %}
{% include "includes/common/pagination.jinja" %}

{% endblock %}
{% endblock %}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "commodities/detail.jinja" %}
{% from "components/sort_by.jinja" import sort_by %}
{% from "components/create_sortable_anchor.jinja" import create_sortable_anchor %}

{% set extra_tabs_links = [
{
Expand Down Expand Up @@ -59,15 +59,15 @@
{% set base_url = url('commodity-ui-detail-measures-as-defined', args=[commodity.sid]) %}

{% set measure_type %}
{{ sort_by(request, "measure_type", "Measure type", base_url) }}
{{ create_sortable_anchor(request, "measure_type", "Measure type", base_url) }}
{% endset %}

{% set geo_area %}
{{ sort_by(request, "geo_area", "Geographical area", base_url) }}
{{ create_sortable_anchor(request, "geo_area", "Geographical area", base_url) }}
{% endset %}

{% set start_date %}
{{ sort_by(request, "start_date", "Start date", base_url) }}
{{ create_sortable_anchor(request, "start_date", "Start date", base_url) }}
{% endset %}

{% if paginator.count > 0 %}
Expand All @@ -89,4 +89,4 @@
<p class="govuk-body">There are no measures for this commodity code.</p>
{% endif %}
{% include "includes/common/pagination.jinja" %}
{% endblock %}
{% endblock %}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "commodities/detail.jinja" %}
{% from "components/sort_by.jinja" import sort_by %}
{% from "components/create_sortable_anchor.jinja" import create_sortable_anchor %}

{% set extra_tabs_links = [
{
Expand Down Expand Up @@ -81,4 +81,4 @@
<p class="govuk-body">There are no VAT or excise measures for this commodity code.</p>
{% endif %}

{% endblock %}
{% endblock %}
12 changes: 6 additions & 6 deletions commodities/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def test_commodity_measures_sorting_geo_area(
url_name,
kwargs={"sid": commodity.sid},
)
response = valid_user_client.get(f"{url}?sort_by=geo_area&order=desc")
response = valid_user_client.get(f"{url}?sort_by=geo_area&ordered=desc")
assert response.status_code == 200

soup = BeautifulSoup(response.content.decode(response.charset), "html.parser")
Expand All @@ -259,7 +259,7 @@ def test_commodity_measures_sorting_geo_area(
"commodity-ui-detail-measures-as-defined",
kwargs={"sid": commodity.sid},
)
response = valid_user_client.get(f"{url}?sort_by=geo_area&order=asc")
response = valid_user_client.get(f"{url}?sort_by=geo_area&ordered=asc")
assert response.status_code == 200

soup = BeautifulSoup(response.content.decode(response.charset), "html.parser")
Expand Down Expand Up @@ -298,7 +298,7 @@ def test_commodity_measures_sorting_start_date(
url_name,
kwargs={"sid": commodity.sid},
)
response = valid_user_client.get(f"{url}?sort_by=start_date&order=desc")
response = valid_user_client.get(f"{url}?sort_by=start_date&ordered=desc")
assert response.status_code == 200

soup = BeautifulSoup(response.content.decode(response.charset), "html.parser")
Expand All @@ -311,7 +311,7 @@ def test_commodity_measures_sorting_start_date(
"commodity-ui-detail-measures-as-defined",
kwargs={"sid": commodity.sid},
)
response = valid_user_client.get(f"{url}?sort_by=start_date&order=asc")
response = valid_user_client.get(f"{url}?sort_by=start_date&ordered=asc")
assert response.status_code == 200

soup = BeautifulSoup(response.content.decode(response.charset), "html.parser")
Expand Down Expand Up @@ -353,7 +353,7 @@ def test_commodity_measures_sorting_measure_type(
url_name,
kwargs={"sid": commodity.sid},
)
response = valid_user_client.get(f"{url}?sort_by=measure_type&order=desc")
response = valid_user_client.get(f"{url}?sort_by=measure_type&ordered=desc")
assert response.status_code == 200

soup = BeautifulSoup(response.content.decode(response.charset), "html.parser")
Expand All @@ -366,7 +366,7 @@ def test_commodity_measures_sorting_measure_type(
"commodity-ui-detail-measures-as-defined",
kwargs={"sid": commodity.sid},
)
response = valid_user_client.get(f"{url}?sort_by=measure_type&order=asc")
response = valid_user_client.get(f"{url}?sort_by=measure_type&ordered=asc")
assert response.status_code == 200

soup = BeautifulSoup(response.content.decode(response.charset), "html.parser")
Expand Down
21 changes: 21 additions & 0 deletions common/jinja2/components/create_sortable_anchor.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

{% macro create_sortable_anchor(request, sort_by, title, base_url, query_params=False, fragment="") %}

{% if query_params %}
{% set query_string_prefix = "&"%}
{% else %}
{% set query_string_prefix = "?"%}
{% endif %}

{% if request.GET.sort_by == sort_by and request.GET.ordered == "desc" %}

<a class="govuk-link govuk-link--no-visited-state sort-icon--down" href="{{ base_url }}{{query_string_prefix}}sort_by={{ sort_by }}&ordered=asc{{ fragment }}">
{{ title }} <img src="{{ static('common/images/sort_icon.svg') }}" alt="">
</a>
{% else %}
<a class="govuk-link govuk-link--no-visited-state sort-icon--up" href="{{ base_url }}{{query_string_prefix}}sort_by={{ sort_by }}&ordered=desc{{ fragment }}">
{{ title }} <img src="{{ static('common/images/sort_icon.svg') }}" alt="">
</a>
{% endif %}

{% endmacro %}
14 changes: 0 additions & 14 deletions common/jinja2/components/sort_by.jinja

This file was deleted.

4 changes: 2 additions & 2 deletions common/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ def get_queryset(self):

def get_ordering(self):
sort_by = self.request.GET.get("sort_by")
order = self.request.GET.get("order")
ordered = self.request.GET.get("ordered")
assert hasattr(
self,
"sort_by_fields",
Expand All @@ -446,7 +446,7 @@ def get_ordering(self):
if hasattr(self, "custom_sorting") and self.custom_sorting.get(sort_by):
sort_by = self.custom_sorting.get(sort_by)

if order == "desc":
if ordered == "desc":
sort_by = f"-{sort_by}"

return sort_by
Expand Down
8 changes: 4 additions & 4 deletions geo_areas/jinja2/includes/geo_areas/tabs/measures.jinja
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "geo_areas/detail.jinja" %}

{% from "components/sort_by.jinja" import sort_by %}
{% from "components/create_sortable_anchor.jinja" import create_sortable_anchor %}
{% from "components/table/macro.njk" import govukTable %}

{% set actions_menu %}
Expand Down Expand Up @@ -43,11 +43,11 @@
{% set base_url = url('geo_area-ui-detail-measures', kwargs={"sid": object.sid}) %}

{% set commodity_code %}
{{ sort_by(request, "goods_nomenclature", "Commodity code", base_url) }}
{{ create_sortable_anchor(request, "goods_nomenclature", "Commodity code", base_url) }}
{% endset %}

{% set start_date %}
{{ sort_by(request, "start_date", "Start date", base_url) }}
{{ create_sortable_anchor(request, "start_date", "Start date", base_url) }}
{% endset %}

{% if paginator.count > 0 %}
Expand All @@ -68,4 +68,4 @@
<p class="govuk-body">There are no measures for this regulation.</p>
{% endif %}
{% include "includes/common/pagination.jinja" %}
{% endblock %}
{% endblock %}
8 changes: 4 additions & 4 deletions geo_areas/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ def test_geo_area_detail_measures_view_sorting_commodity(valid_user_client):
"sid": geo_area.sid,
},
)
response = valid_user_client.get(f"{url}?sort_by=goods_nomenclature&order=asc")
response = valid_user_client.get(f"{url}?sort_by=goods_nomenclature&ordered=asc")
page = BeautifulSoup(
response.content.decode(response.charset),
"html.parser",
Expand All @@ -596,7 +596,7 @@ def test_geo_area_detail_measures_view_sorting_commodity(valid_user_client):
]
assert table_commodity_codes == commodity_codes

response = valid_user_client.get(f"{url}?sort_by=goods_nomenclature&order=desc")
response = valid_user_client.get(f"{url}?sort_by=goods_nomenclature&ordered=desc")
page = BeautifulSoup(
response.content.decode(response.charset),
"html.parser",
Expand Down Expand Up @@ -635,7 +635,7 @@ def test_geo_area_detail_measures_view_sorting_start_date(
"sid": geo_area.sid,
},
)
response = valid_user_client.get(f"{url}?sort_by=start_date&order=asc")
response = valid_user_client.get(f"{url}?sort_by=start_date&ordered=asc")
page = BeautifulSoup(
response.content.decode(response.charset),
"html.parser",
Expand All @@ -645,7 +645,7 @@ def test_geo_area_detail_measures_view_sorting_start_date(
]
assert table_measure_sids == [measure1.sid, measure2.sid, measure3.sid]

response = valid_user_client.get(f"{url}?sort_by=start_date&order=desc")
response = valid_user_client.get(f"{url}?sort_by=start_date&ordered=desc")
page = BeautifulSoup(
response.content.decode(response.charset),
"html.parser",
Expand Down
34 changes: 28 additions & 6 deletions measures/jinja2/includes/measures/list.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
{% from 'macros/footnotes_display.jinja' import footnotes_display %}
{% from "includes/measures/conditions.jinja" import conditions_list %}
{% from "components/button/macro.njk" import govukButton %}
{% from "components/create_sortable_anchor.jinja" import create_sortable_anchor %}
{% from "components/warning-text/macro.njk" import govukWarningText %}


{# Sets out checkbox #}
{% macro checkbox_item(field) -%}
{% set id = field.id_for_label %}
Expand All @@ -26,6 +28,26 @@
<div id="check-all-checkbox"></div>
{%- endset %}

{% set sid %}
{{ create_sortable_anchor(request, "sid", "ID", base_url, query_params) }}
{% endset %}

{% set measure_type %}
{{ create_sortable_anchor(request, "measure_type", "Type", base_url, query_params) }}
{% endset %}

{% set geo_area %}
{{ create_sortable_anchor(request, "geo_area", "Geographical area", base_url, query_params) }}
{% endset %}

{% set start_date %}
{{ create_sortable_anchor(request, "start_date", "Start date", base_url, query_params) }}
{% endset %}

{% set end_date %}
{{ create_sortable_anchor(request, "end_date", "End date", base_url, query_params) }}
{% endset %}

{# sets out form #}
<form method="post">
<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
Expand Down Expand Up @@ -67,14 +89,14 @@
{{ govukTable({
"head": [
{"html": checkbox_check_all},
{"text": "ID"},
{"text": "Type"},
{"text": "Commodity code"},
{"text": "Start date"},
{"text": "End date"},
{"text": sid},
{"text": measure_type},
{"text": "Commodity"},
{"text": start_date},
{"text": end_date},
{"text": "Duties"},
{"text": "Additional code"},
{"text": "Geographical area"},
{"text": geo_area},
{"text": "Quota"},
{"text": "Footnote"},
{"text": "Conditions"},
Expand Down
Loading

0 comments on commit 949f547

Please sign in to comment.