Skip to content

Commit

Permalink
Merge branch 'master' into TP2000-553-edit-multiple-measures
Browse files Browse the repository at this point in the history
  • Loading branch information
eadpearce authored Nov 18, 2022
2 parents 3e9faec + 7b2c147 commit 7ad54e3
Show file tree
Hide file tree
Showing 47 changed files with 802 additions and 232 deletions.
8 changes: 8 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ Open another terminal and start a Celery worker:
celery -A common.celery worker --loglevel=info
To monitor celery workers or individual tasks run:

.. code:: sh
celery flower
See `flower docs <https://flower.readthedocs.io/en/latest/>`_ for more details


Manually trigger the upload to s3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion commodities/assets/commodities_taric3.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@
</xs:simpleType>
<xs:simpleType name="ShortDescription">
<xs:restriction base="xs:string">
<xs:maxLength value="500"/>
<xs:maxLength value="1000"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="StoppedFlag">
Expand Down
2 changes: 2 additions & 0 deletions commodities/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from crispy_forms_gds.layout import Size
from crispy_forms_gds.layout import Submit
from django import forms
from django.conf import settings
from django.contrib.auth.models import User
from django.db import transaction

Expand Down Expand Up @@ -35,6 +36,7 @@ class CommodityImportForm(ImportForm):
help_text="",
label="Select an XML file",
)
xsd_file = settings.PATH_XSD_COMMODITIES_TARIC

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down
1 change: 1 addition & 0 deletions commodities/models/orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def footnote_application_codes(self) -> Set[ApplicationCode]:
business_rules = (
business_rules.NIG1,
business_rules.NIG5,
business_rules.NIG11,
business_rules.NIG12,
business_rules.NIG30,
business_rules.NIG31,
Expand Down
32 changes: 32 additions & 0 deletions commodities/tests/test_files/quota_definition.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<env:envelope id="22001" xmlns="urn:publicid:-:DGTAXUD:TARIC:MESSAGE:1.0" xmlns:env="urn:publicid:-:DGTAXUD:GENERAL:ENVELOPE:1.0">
<env:transaction id="392602">
<env:app.message id="1">
<oub:transmission xmlns:env="urn:publicid:-:DGTAXUD:GENERAL:ENVELOPE:1.0" xmlns:oub="urn:publicid:-:DGTAXUD:TARIC:MESSAGE:1.0">
<oub:record>
<oub:transaction.id>19802301</oub:transaction.id>
<oub:record.code>370</oub:record.code>
<oub:subrecord.code>00</oub:subrecord.code>
<oub:record.sequence.number>450</oub:record.sequence.number>
<oub:update.type>3</oub:update.type>
<oub:quota.definition>
<oub:quota.definition.sid>21830</oub:quota.definition.sid>
<oub:quota.order.number.id>091528</oub:quota.order.number.id>
<oub:validity.start.date>2023-01-01</oub:validity.start.date>
<oub:validity.end.date>2023-12-31</oub:validity.end.date>
<oub:quota.order.number.sid>2554</oub:quota.order.number.sid>
<oub:volume>2550000</oub:volume>
<oub:initial.volume>2550000</oub:initial.volume>
<oub:measurement.unit.code>LTR</oub:measurement.unit.code>
<oub:maximum.precision>3</oub:maximum.precision>
<oub:critical.state>N</oub:critical.state>
<oub:critical.threshold>90</oub:critical.threshold>
<oub:description>Commission Implementation Regulation (EU) No 343/2011 - Opening and providing for the administration of Union tariff quotas for wines originating in Bosnia and Herzegovina
L 96/12 of 9/04/2011
Increase of volumes with COMMISSION IMPLEMENTING REGULATION (EU) 2017/822 of 15 May 2017 amending Implementing Regulation (EU) No 343/2011 opening and providing for the administration of Union tariff quotas for wines originating in Bosnia and Herzegovina (see OJ L 123/1)</oub:description>
</oub:quota.definition>
</oub:record>
</oub:transmission>
</env:app.message>
</env:transaction>
</env:envelope>
17 changes: 17 additions & 0 deletions commodities/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,20 @@ def test_import_form_non_xml_file():

assert not form.is_valid()
assert "The selected file must be XML" in form.errors["taric_file"]


# https://uktrade.atlassian.net/browse/TP2000-571
def test_import_form_long_definition_description():
"""Tests that form is valid when provided with QuotaDefinition description
longer than 500 characters."""
with open(f"{TEST_FILES_PATH}/quota_definition.xml", "rb") as upload_file:
file_data = {
"taric_file": SimpleUploadedFile(
upload_file.name,
upload_file.read(),
content_type="text/xml",
),
}
form = forms.CommodityImportForm({}, file_data)

assert form.is_valid()
24 changes: 24 additions & 0 deletions commodities/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from os import path
from unittest.mock import patch

Expand Down Expand Up @@ -145,3 +146,26 @@ def test_commodities_detail_views(
don't return an error."""

assert_model_view_renders(view, url_pattern, valid_user_client)


def test_goods_nomenclature(valid_user_client, date_ranges):
past_good = factories.GoodsNomenclatureFactory.create(
valid_between=date_ranges.earlier,
)
present_good = factories.GoodsNomenclatureFactory.create(
valid_between=date_ranges.normal,
)
future_good = factories.GoodsNomenclatureFactory.create(
valid_between=date_ranges.later,
)
url = reverse("goodsnomenclature-list")
response = valid_user_client.get(url)

assert response.status_code == 200

goods = json.loads(response.content)["results"]
pks = [good["value"] for good in goods]

assert past_good.pk not in pks
assert present_good.pk in pks
assert future_good.pk in pks
2 changes: 1 addition & 1 deletion commodities/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def get_queryset(self):
tx,
)
.prefetch_related("descriptions")
.as_at(date.today())
.as_at_and_beyond(date.today())
.filter(suffix=80)
)

Expand Down
2 changes: 1 addition & 1 deletion common/assets/envelope.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<xs:attribute name="id" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{6}"/>
<xs:pattern value="[0-9]{5,6}"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
Expand Down
2 changes: 1 addition & 1 deletion common/assets/taric3.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@
</xs:simpleType>
<xs:simpleType name="ShortDescription">
<xs:restriction base="xs:string">
<xs:maxLength value="500"/>
<xs:maxLength value="1000"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="StoppedFlag">
Expand Down
14 changes: 14 additions & 0 deletions common/business_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,20 @@ def validate(self, model):
raise self.violation(model)


class MustExistNotNull(BusinessRule):
"""Rule enforcing a referenced record exists, Null values raise
violation."""

reference_field_name: Optional[str] = None

def validate(self, model):
try:
if getattr(model, self.reference_field_name) is None:
raise self.violation(model)
except ObjectDoesNotExist:
raise self.violation(model)


class ValidityStartDateRules(BusinessRule):
"""Repeated rule pattern for descriptions."""

Expand Down
71 changes: 71 additions & 0 deletions common/jinja2/common/app_info.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{% extends "layouts/layout.jinja" %}

{% from "components/inset-text/macro.njk" import govukInsetText %}
{% from "components/table/macro.njk" import govukTable %}


{% set page_title = "Application information" %}

{% block breadcrumb %}
<div class="govuk-breadcrumbs">
<ol class="govuk-breadcrumbs__list">
<li class="govuk-breadcrumbs__list-item">
<a class="govuk-breadcrumbs__link" href="{{ url('home') }}">Home</a>
</li>
<li class="govuk-breadcrumbs__list-item">
{{ page_title }}
</li>
</ol>
</div>
{% endblock %}

{% block content %}
<h1 class="govuk-heading-xl">
{% block page_title %}
{{ page_title }}
{% endblock %}
</h1>

<h2 class="govuk-heading-m">
Active business rule checks
</h2>

{% set table_head = [
{"text": "Workbasket ID"},
{"text": "Time started"},
{"text": "Celery task ID"},
] %}
{% set table_rows = [] %}

{% if celery_healthy %}
{% if active_checks %}
{% for check in active_checks %}
{{ table_rows.append([
{"text": check.workbasket_id},
{"text": check.date_time_start},
{"text": check.task_id},
]) or "" }}
{% endfor %}
{% else %}
{{ table_rows.append([
{
"text": "No active checks.",
"colspan": 3,
}
]) or "" }}
{% endif %}
{% else %}
{{ table_rows.append([
{
"text": "Business rule check details are currently unavailable.",
"colspan": 3,
}
]) or "" }}
{% endif %}

{{ govukTable({
"head": table_head,
"rows": table_rows
}) }}

{% endblock %}
12 changes: 12 additions & 0 deletions common/migrations/0006_auto_20221114_1000.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Generated by Django 3.1.14 on 2022-02-21 14:57

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("common", "0005_transaction_index"),
("measures", "0010_add_requires_to_action_and_accepts_to_condition_code"),
]

operations = []
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Generated by Django 3.1.14 on 2022-02-21 14:57

from django.db import migrations


def current_versions_without_tracked_models(apps):
from common.models.transactions import TransactionPartition
from workbaskets.validators import WorkflowStatus

""":return: Queryset of tracked models with no current version, but are published, and on the correct partition."""

TrackedModel = apps.get_model("common", "TrackedModel")

return (
TrackedModel.objects.select_related("version_group")
.filter(
version_group__current_version_id__isnull=True,
)
.select_related("transaction")
.filter(
transaction__partition=TransactionPartition.REVISION,
transaction__workbasket__status=WorkflowStatus.PUBLISHED,
)
.order_by("transaction__order")
)


def fix_current_versions(apps, schemaeditor):
"""Update affected models and run through in transaction order, to correctly
update the missing correct current version."""

results = current_versions_without_tracked_models(apps)

for result in results:
version_group = result.version_group
version_group.current_version_id = result.id
version_group.save()


class Migration(migrations.Migration):
dependencies = [
("common", "0006_auto_20221114_1000"),
]

operations = [
migrations.RunPython(
fix_current_versions,
lambda apps, schema: None,
),
]
Loading

0 comments on commit 7ad54e3

Please sign in to comment.