Skip to content

Commit

Permalink
Merge branch 'master' into TP2000-1506--script-data-creation-perf-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dalecannon committed Oct 2, 2024
2 parents ded37e2 + 21653dd commit 3fb8f05
Show file tree
Hide file tree
Showing 33 changed files with 1,944 additions and 285 deletions.
10 changes: 8 additions & 2 deletions commodities/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from commodities.models.orm import GoodsNomenclature
from common.filters import ActiveStateMixin
from common.filters import CurrentWorkBasketMixin
from common.filters import EndDateMixin
from common.filters import TamatoFilter
from common.filters import TamatoFilterBackend
from common.validators import AlphanumericValidator
Expand All @@ -33,9 +34,14 @@ def search_queryset(self, queryset, search_term):
return super().search_queryset(queryset, search_term)


class CommodityFilter(ActiveStateMixin, TamatoFilter, CurrentWorkBasketMixin):
class CommodityFilter(
ActiveStateMixin,
TamatoFilter,
CurrentWorkBasketMixin,
EndDateMixin,
):
item_id = CharFilter(
label="Code",
label="Commodity code",
widget=forms.TextInput(),
lookup_expr="startswith",
validators=[NumericValidator],
Expand Down
19 changes: 16 additions & 3 deletions commodities/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,28 @@ def __init__(self, *args, **kwargs):
self.helper.layout = Layout(
Field.text("item_id", label_size=Size.SMALL),
Field.text("descriptions__description", label_size=Size.SMALL),
Field.text("active_state", label_size=Size.SMALL),
Field.checkboxes(
"active_state",
legend_size=Size.SMALL,
),
Fieldset(
Field.text("with_footnotes", label_size=Size.SMALL),
legend="Footnotes",
legend_size=Size.SMALL,
),
Fieldset(
Field.text("with_end_date", label_size=Size.SMALL),
legend="End date",
legend_size=Size.SMALL,
),
Fieldset(
Field.text("current_work_basket", label_size=Size.SMALL),
legend="Workbasket",
legend_size=Size.SMALL,
),
Field.text("current_work_basket", label_size=Size.SMALL),
Button(
"submit",
"Search and Filter",
"Search and filter",
),
HTML(
f'<a class="govuk-button govuk-button--secondary" href="{self.clear_url}"> Clear </a>',
Expand Down
4 changes: 4 additions & 0 deletions commodities/jinja2/includes/commodities/table.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
{"text": commodity.suffix},
{"text": commodity.get_indent_as_at(today).indent},
{"text": commodity.get_description().description},
{"text": "{:%d %b %Y}".format(commodity.valid_between.lower)},
{"text": "{:%d %b %Y}".format(commodity.valid_between.upper) if commodity.valid_between.upper else "-"},
{"text": commodity_footnotes},
]) or "" }}
{% endfor %}
Expand All @@ -24,6 +26,8 @@
{"text": "Suffix"},
{"text": "Indent"},
{"text": "Commodity description"},
{"text": "Start date"},
{"text": "End date"},
{"text": "Footnotes"},

],
Expand Down
16 changes: 16 additions & 0 deletions commodities/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

def test_commodity_list_displays_commodity_suffix_indent_and_description(
valid_user_client,
date_ranges,
):
"""Test that a list of commodity codes with links and their suffixes,
indents and descriptions are displayed on the list view template."""
Expand All @@ -37,6 +38,9 @@ def test_commodity_list_displays_commodity_suffix_indent_and_description(
description="A second commodity code description",
).described_goods_nomenclature

commodity2.valid_between = date_ranges.normal
commodity2.save(force_write=True)

url = reverse("commodity-ui-list")
response = valid_user_client.get(url)
page = BeautifulSoup(
Expand All @@ -51,6 +55,10 @@ def test_commodity_list_displays_commodity_suffix_indent_and_description(
text=commodity1.get_indent_as_at(datetime.date.today()).indent,
)
assert page.find("tbody").find("td", text="A commodity code description")
assert page.find("tbody").find(
"td",
text=f"{commodity1.valid_between.lower:%d %b %Y}",
)

assert page.find("tbody").find("td", text=commodity2.item_id)
assert page.find("tbody").find(href=f"/commodities/{commodity2.sid}/")
Expand All @@ -60,6 +68,14 @@ def test_commodity_list_displays_commodity_suffix_indent_and_description(
text=commodity2.get_indent_as_at(datetime.date.today()).indent,
)
assert page.find("tbody").find("td", text="A second commodity code description")
assert page.find("tbody").find(
"td",
text=f"{commodity2.valid_between.lower:%d %b %Y}",
)
assert page.find("tbody").find(
"td",
text=f"{commodity2.valid_between.upper:%d %b %Y}",
)


def test_commodity_list_queryset():
Expand Down
18 changes: 17 additions & 1 deletion common/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class ActiveStateMixin(FilterSet):
widget=forms.CheckboxSelectMultiple,
method="filter_active_state",
label="Active state",
help_text="Select all that apply",
help_text="Select one to filter by active or terminated items",
required=False,
)

Expand Down Expand Up @@ -348,3 +348,19 @@ def filter_work_basket(self, queryset, name, value):
id__in=wanted_objects_id,
)
return queryset


class EndDateMixin(FilterSet):
"""A filter to only show objects which have an end date."""

with_end_date = BooleanFilter(
label="Show items with an end date",
widget=forms.CheckboxInput(),
method="filter_end_date",
required=False,
)

def filter_end_date(self, queryset, name, value):
if value:
queryset = queryset.filter(valid_between__upper_inf=False)
return queryset
4 changes: 4 additions & 0 deletions common/static/common/scss/_components.scss
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,7 @@
.homepage-workbasket-action {
width: 100%;
}

.govuk-checkboxes__label:before {
background-color: white
}
118 changes: 118 additions & 0 deletions measures/editors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
from typing import Dict
from typing import List
from typing import Type

from django.db.transaction import atomic

from workbaskets import models as workbasket_models
from measures import models as measure_models
from common.util import TaricDateRange
from common.validators import UpdateType
from common.models.utils import override_current_transaction
from measures.util import update_measure_components
from measures.util import update_measure_condition_components
from measures.util import update_measure_excluded_geographical_areas
from measures.util import update_measure_footnote_associations


class MeasuresEditor:
"""Utility class used to edit measures from measures wizard accumulated
data."""

workbasket: Type["workbasket_models.WorkBasket"]
"""The workbasket with which created measures will be associated."""

selected_measures: List
""" The measures in which the edits will apply to."""

data: Dict
"""Validated, cleaned and accumulated data created by the Form instances of
`MeasureEditWizard`."""

def __init__(
self,
workbasket: Type["workbasket_models.WorkBasket"],
selected_measures: List,
data: Dict,
):
self.workbasket = workbasket
self.selected_measures = selected_measures
self.data = data

@atomic
def edit_measures(self) -> List["measure_models.Measure"]:
"""
Returns a list of the edited measures.
`data` must be a dictionary
of the accumulated cleaned / validated data created from the
`MeasureEditWizard`.
"""

with override_current_transaction(
transaction=self.workbasket.current_transaction,
):
new_start_date = self.data.get("start_date", None)
new_end_date = self.data.get("end_date", False)
new_quota_order_number = self.data.get("order_number", None)
new_generating_regulation = self.data.get("generating_regulation", None)
new_duties = self.data.get("duties", None)
new_exclusions = [
e["excluded_area"]
for e in self.data.get("formset-geographical_area_exclusions", [])
]

edited_measures = []

if self.selected_measures:
for measure in self.selected_measures:
new_measure = measure.new_version(
workbasket=self.workbasket,
update_type=UpdateType.UPDATE,
valid_between=TaricDateRange(
lower=(
new_start_date
if new_start_date
else measure.valid_between.lower
),
upper=(
new_end_date
if new_end_date
else measure.valid_between.upper
),
),
order_number=(
new_quota_order_number
if new_quota_order_number
else measure.order_number
),
generating_regulation=(
new_generating_regulation
if new_generating_regulation
else measure.generating_regulation
),
)
update_measure_components(
measure=new_measure,
duties=new_duties,
workbasket=self.workbasket,
)
update_measure_condition_components(
measure=new_measure,
workbasket=self.workbasket,
)
update_measure_excluded_geographical_areas(
edited="geographical_area_exclusions"
in self.data.get("fields_to_edit", []),
measure=new_measure,
exclusions=new_exclusions,
workbasket=self.workbasket,
)
update_measure_footnote_associations(
measure=new_measure,
workbasket=self.workbasket,
)

edited_measures.append(new_measure.id)

return edited_measures
Loading

0 comments on commit 3fb8f05

Please sign in to comment.