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

TP2000-1021 Measures tab for additional codes #1033

Merged
merged 6 commits into from
Sep 20, 2023
Merged
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
10 changes: 9 additions & 1 deletion additional_codes/jinja2/additional_codes/detail.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -24,6 +25,13 @@
"html": description_tab_html
}
},
{
"label": "Measures",
"id": "measures",
"panel": {
"html": measures_html
}
},
{
"label": "Version control",
"id": "version-control",
Expand All @@ -34,4 +42,4 @@
]
}%}

{%- include "layouts/detail.jinja" -%}
{%- include "layouts/detail.jinja" -%}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@
},
]
})}}
{% endblock %}
{% endblock %}
Original file line number Diff line number Diff line change
@@ -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 -%}
<a class="govuk-link govuk-!-font-weight-bold" href="{{ url('measure-ui-detail', args=[object.sid]) }}">{{ object.sid }}</a>
{%- endset %}
{% set commodity_link -%}
<a class="govuk-link govuk-!-font-weight-bold" href="{{ url('commodity-ui-detail', args=[object.goods_nomenclature.sid]) }}">{{ object.goods_nomenclature.item_id|wordwrap(2) }}</a>
{%- 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 %}


<div class="govuk-grid-row">
<div class="govuk-grid-column-three-quarters">
<h2 class="govuk-heading-l">Details</h2>
{% if measures %}
<p class="govuk-body">Showing currently active measures.</p>
{{ govukTable({
"head": [
{"text": "Measure SID"},
{"text": "Commodity code"},
{"text": "Start date"},
{"text": "End date"},
],
"rows": table_rows
}) }}
{% else %}
<p class="govuk-body">No active measures.</p>
{% endif %}
</div>
{% include "includes/common/actions.jinja"%}
</div>
36 changes: 36 additions & 0 deletions additional_codes/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -192,6 +193,41 @@ def test_additional_code_type_api_list_view(valid_user_client):
)


def test_additional_code_details_list_current_measures(
a-gleeson marked this conversation as resolved.
Show resolved Hide resolved
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."""
Expand Down
28 changes: 28 additions & 0 deletions additional_codes/views.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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 = (
a-gleeson marked this conversation as resolved.
Show resolved Hide resolved
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,
Expand Down
3 changes: 3 additions & 0 deletions common/jinja2/includes/common/actions.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<h2 class="govuk-heading-s" id="subsection-title">Actions</h2>
<ul class="govuk-list govuk-!-font-size-16">
{% set edit_url = object.get_url('edit') %}
{% if measures_url %}
<li><a class="govuk-link" href="{{ measures_url }}">View in find and edit measures</a></li>
{% endif %}
{% if edit_url %}
<li><a class="govuk-link" href="{{ edit_url }}">Edit this {{object._meta.verbose_name}}</a></li>
{% endif %}
Expand Down
Loading