diff --git a/additional_codes/jinja2/includes/additional_codes/tabs/measures.jinja b/additional_codes/jinja2/includes/additional_codes/tabs/measures.jinja index e029cc2c8..fa1a5421a 100644 --- a/additional_codes/jinja2/includes/additional_codes/tabs/measures.jinja +++ b/additional_codes/jinja2/includes/additional_codes/tabs/measures.jinja @@ -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 = [] %} @@ -37,4 +37,4 @@ {% endif %} {% include "includes/common/actions.jinja"%} - \ No newline at end of file + diff --git a/commodities/jinja2/includes/commodities/tabs/measures-declarable.jinja b/commodities/jinja2/includes/commodities/tabs/measures-declarable.jinja index e2e6f8a37..af98f8a09 100644 --- a/commodities/jinja2/includes/commodities/tabs/measures-declarable.jinja +++ b/commodities/jinja2/includes/commodities/tabs/measures-declarable.jinja @@ -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 = [ { @@ -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 %} @@ -100,4 +100,4 @@ {% endif %} {% include "includes/common/pagination.jinja" %} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/commodities/jinja2/includes/commodities/tabs/measures-defined.jinja b/commodities/jinja2/includes/commodities/tabs/measures-defined.jinja index 76eec8dff..b141df0be 100644 --- a/commodities/jinja2/includes/commodities/tabs/measures-defined.jinja +++ b/commodities/jinja2/includes/commodities/tabs/measures-defined.jinja @@ -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 = [ { @@ -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 %} @@ -89,4 +89,4 @@
There are no measures for this commodity code.
{% endif %} {% include "includes/common/pagination.jinja" %} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/commodities/jinja2/includes/commodities/tabs/measures-vat-excise.jinja b/commodities/jinja2/includes/commodities/tabs/measures-vat-excise.jinja index 8f0fcb6e3..afe9458ca 100644 --- a/commodities/jinja2/includes/commodities/tabs/measures-vat-excise.jinja +++ b/commodities/jinja2/includes/commodities/tabs/measures-vat-excise.jinja @@ -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 = [ { @@ -81,4 +81,4 @@There are no VAT or excise measures for this commodity code.
{% endif %} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/commodities/tests/test_views.py b/commodities/tests/test_views.py index 9b70747fb..18534ec1f 100644 --- a/commodities/tests/test_views.py +++ b/commodities/tests/test_views.py @@ -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") @@ -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") @@ -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") @@ -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") @@ -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") @@ -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") diff --git a/common/jinja2/components/create_sortable_anchor.jinja b/common/jinja2/components/create_sortable_anchor.jinja new file mode 100644 index 000000000..b83f591dc --- /dev/null +++ b/common/jinja2/components/create_sortable_anchor.jinja @@ -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" %} + + + {{ title }} + + {% else %} + + {{ title }} + + {% endif %} + +{% endmacro %} diff --git a/common/jinja2/components/sort_by.jinja b/common/jinja2/components/sort_by.jinja deleted file mode 100644 index cbbe100ef..000000000 --- a/common/jinja2/components/sort_by.jinja +++ /dev/null @@ -1,14 +0,0 @@ - -{% macro sort_by(request, sort_by, title, base_url, anchor="") %} - - {% if request.GET.sort_by == sort_by and request.GET.order == "desc" %} - - {{ title }} - - {% else %} - - {{ title }} - - {% endif %} - -{% endmacro %} \ No newline at end of file diff --git a/common/views.py b/common/views.py index 8b95c115d..e7f162bf4 100644 --- a/common/views.py +++ b/common/views.py @@ -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", @@ -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 diff --git a/geo_areas/jinja2/includes/geo_areas/tabs/measures.jinja b/geo_areas/jinja2/includes/geo_areas/tabs/measures.jinja index 708dd7310..4edd73849 100644 --- a/geo_areas/jinja2/includes/geo_areas/tabs/measures.jinja +++ b/geo_areas/jinja2/includes/geo_areas/tabs/measures.jinja @@ -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 %} @@ -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 %} @@ -68,4 +68,4 @@There are no measures for this regulation.
{% endif %} {% include "includes/common/pagination.jinja" %} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/geo_areas/tests/test_views.py b/geo_areas/tests/test_views.py index 096f9d321..f4ca956de 100644 --- a/geo_areas/tests/test_views.py +++ b/geo_areas/tests/test_views.py @@ -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", @@ -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", @@ -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", @@ -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", diff --git a/measures/jinja2/includes/measures/list.jinja b/measures/jinja2/includes/measures/list.jinja index 86062ad32..6997d94c9 100644 --- a/measures/jinja2/includes/measures/list.jinja +++ b/measures/jinja2/includes/measures/list.jinja @@ -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 %} @@ -26,6 +28,26 @@ {%- 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 #}