diff --git a/additional_codes/jinja2/additional_codes/detail.jinja b/additional_codes/jinja2/additional_codes/detail.jinja index f7e927052..1b2075176 100644 --- a/additional_codes/jinja2/additional_codes/detail.jinja +++ b/additional_codes/jinja2/additional_codes/detail.jinja @@ -6,6 +6,7 @@ {% set core_data_tab_html %}{% include "includes/additional_codes/tabs/core_data.jinja" %}{% endset %} {% set description_tab_html %}{% include "includes/common/tabs/descriptions.jinja" %}{% endset %} +{% set measures_html %}{% include "includes/additional_codes/tabs/measures.jinja" %}{% endset %} {% set version_control_tab_html %}{% include "includes/common/tabs/version_control.jinja" %}{% endset %} {% set tabs = { @@ -24,6 +25,13 @@ "html": description_tab_html } }, + { + "label": "Measures", + "id": "measures", + "panel": { + "html": measures_html + } + }, { "label": "Version control", "id": "version-control", @@ -34,4 +42,4 @@ ] }%} -{%- include "layouts/detail.jinja" -%} +{%- include "layouts/detail.jinja" -%} \ No newline at end of file diff --git a/additional_codes/jinja2/includes/additional_codes/tabs/core_data.jinja b/additional_codes/jinja2/includes/additional_codes/tabs/core_data.jinja index 7f28db193..6548f99c0 100644 --- a/additional_codes/jinja2/includes/additional_codes/tabs/core_data.jinja +++ b/additional_codes/jinja2/includes/additional_codes/tabs/core_data.jinja @@ -35,4 +35,4 @@ }, ] })}} -{% endblock %} +{% endblock %} \ No newline at end of file diff --git a/additional_codes/jinja2/includes/additional_codes/tabs/measures.jinja b/additional_codes/jinja2/includes/additional_codes/tabs/measures.jinja new file mode 100644 index 000000000..e029cc2c8 --- /dev/null +++ b/additional_codes/jinja2/includes/additional_codes/tabs/measures.jinja @@ -0,0 +1,40 @@ +{% from "components/sort_by.jinja" import sort_by %} +{% from "components/table/macro.njk" import govukTable %} + +{% set table_rows = [] %} +{% for object in measures %} + {% set measure_link -%} + {{ object.sid }} + {%- endset %} + {% set commodity_link -%} + {{ object.goods_nomenclature.item_id|wordwrap(2) }} + {%- endset %} + {{ table_rows.append([ + {"html": measure_link}, + {"html": commodity_link}, + {"text": "{:%d %b %Y}".format(object.valid_between.lower)}, + {"text": "{:%d %b %Y}".format(object.effective_end_date) if object.effective_end_date else "-"} + ]) or "" }} +{% endfor %} + + +
+
+

Details

+ {% if measures %} +

Showing currently active measures.

+ {{ govukTable({ + "head": [ + {"text": "Measure SID"}, + {"text": "Commodity code"}, + {"text": "Start date"}, + {"text": "End date"}, + ], + "rows": table_rows + }) }} + {% else %} +

No active measures.

+ {% endif %} +
+ {% include "includes/common/actions.jinja"%} +
\ No newline at end of file diff --git a/additional_codes/tests/test_views.py b/additional_codes/tests/test_views.py index 2906be63b..6c8e60cfe 100644 --- a/additional_codes/tests/test_views.py +++ b/additional_codes/tests/test_views.py @@ -2,6 +2,7 @@ import factory import pytest +from bs4 import BeautifulSoup from dateutil.relativedelta import relativedelta from django.core.exceptions import ValidationError from django.urls import reverse @@ -192,6 +193,41 @@ def test_additional_code_type_api_list_view(valid_user_client): ) +def test_additional_code_details_list_current_measures( + valid_user_client, + date_ranges, +): + additional_code = factories.AdditionalCodeFactory() + old_measures = factories.MeasureFactory.create_batch( + 5, + valid_between=date_ranges.adjacent_earlier_big, + additional_code=additional_code, + ) + current_measures = factories.MeasureFactory.create_batch( + 4, + valid_between=date_ranges.normal, + additional_code=additional_code, + ) + url = reverse("additional_code-ui-detail", kwargs={"sid": additional_code.sid}) + response = valid_user_client.get(url) + soup = BeautifulSoup(response.content.decode(response.charset), "html.parser") + num_measures = len( + soup.select("#measures table tbody > tr > td:first-child"), + ) + assert num_measures == 4 + + +def test_additional_code_details_list_no_measures(valid_user_client): + additional_code = factories.AdditionalCodeFactory() + url = reverse("additional_code-ui-detail", kwargs={"sid": additional_code.sid}) + response = valid_user_client.get(url) + soup = BeautifulSoup(response.content.decode(response.charset), "html.parser") + num_measures = len( + soup.select("#measures table tbody > tr > td:first-child"), + ) + assert num_measures == 0 + + def test_additional_code_description_create(valid_user_client): """Tests that `AdditionalCodeDescriptionCreate` view returns 200 and creates a description for the current version of an additional code.""" diff --git a/additional_codes/views.py b/additional_codes/views.py index 3219817eb..0b2cabd71 100644 --- a/additional_codes/views.py +++ b/additional_codes/views.py @@ -1,7 +1,11 @@ +from datetime import datetime from typing import Type +from urllib.parse import urlencode from django.db import transaction +from django.db.models import Q from django.http import HttpResponseRedirect +from django.urls import reverse from rest_framework import permissions from rest_framework import viewsets @@ -26,6 +30,7 @@ from common.views import TamatoListView from common.views import TrackedModelDetailMixin from common.views import TrackedModelDetailView +from measures.models import Measure from workbaskets.models import WorkBasket from workbaskets.views.generic import CreateTaricCreateView from workbaskets.views.generic import CreateTaricDeleteView @@ -134,6 +139,29 @@ class AdditionalCodeConfirmCreate(AdditionalCodeMixin, TrackedModelDetailView): class AdditionalCodeDetail(AdditionalCodeMixin, TrackedModelDetailView): template_name = "additional_codes/detail.jinja" + def get_context_data(self, *args, **kwargs): + measures = ( + Measure.objects.with_effective_valid_between() + .filter( + ( + Q(db_effective_end_date__isnull=True) + | Q(db_effective_end_date__gte=datetime.today()) + ) + & Q(is_current__isnull=False), + ) + .filter(additional_code=self.object) + ) + url_params = urlencode({"additional_code": self.object.pk}) + measures_url = f"{reverse('measure-ui-list')}?{url_params}" + + return super().get_context_data( + measures=measures, + url_params=url_params, + measures_url=measures_url, + *args, + **kwargs, + ) + class AdditionalCodeUpdateMixin( AdditionalCodeMixin, diff --git a/common/jinja2/includes/common/actions.jinja b/common/jinja2/includes/common/actions.jinja index 2b0ebc3c8..ff94759c0 100644 --- a/common/jinja2/includes/common/actions.jinja +++ b/common/jinja2/includes/common/actions.jinja @@ -3,6 +3,9 @@

Actions