Skip to content

Commit

Permalink
Merge branch 'TP2000-1129-reference-documents-megabranch' of github.c…
Browse files Browse the repository at this point in the history
…om:uktrade/tamato into TP2000-1232-reference-doc-ui-management

# Conflicts:
#	reference_documents/forms/preferential_quota_forms.py
  • Loading branch information
mattjamc committed Mar 18, 2024
2 parents 4c27d3a + 531695c commit adfd87e
Show file tree
Hide file tree
Showing 20 changed files with 472 additions and 150 deletions.
90 changes: 75 additions & 15 deletions reference_documents/forms/preferential_quota_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,55 @@ class PreferentialQuotaCreateUpdateForm(
class Meta:
model = PreferentialQuota
fields = [
"quota_order_number",
"preferential_quota_order_number",
"commodity_code",
"quota_duty_rate",
"volume",
"measurement",
"valid_between",
]

def __init__(self, reference_document_version, *args, **kwargs):
def __init__(
self,
reference_document_version,
preferential_quota_order_number,
*args,
**kwargs,
):
super().__init__(*args, **kwargs)

if preferential_quota_order_number:
self.initial[
"preferential_quota_order_number"
] = preferential_quota_order_number

self.fields[
"quota_order_number"
"preferential_quota_order_number"
].queryset = reference_document_version.preferential_quota_order_numbers.all()

self.reference_document_version = reference_document_version
self.quota_order_number = preferential_quota_order_number
self.helper = FormHelper(self)
self.helper.label_size = Size.SMALL
self.helper.legend_size = Size.SMALL
self.helper.layout = Layout(
"quota_order_number",
"commodity_code",
"quota_duty_rate",
"volume",
"measurement",
"preferential_quota_order_number",
Field.text(
"commodity_code",
field_width=Fixed.TEN,
),
Field.text(
"quota_duty_rate",
field_width=Fixed.THIRTY,
),
Field.text(
"volume",
field_width=Fixed.TWENTY,
),
Field.text(
"measurement",
field_width=Fixed.TWENTY,
),
"start_date",
"end_date",
Submit(
Expand All @@ -61,12 +87,27 @@ def __init__(self, reference_document_version, *args, **kwargs):
),
)

def clean_quota_duty_rate(self):
data = self.cleaned_data["quota_duty_rate"]
if len(data) < 1:
raise ValidationError("Quota duty Rate is not valid - it must have a value")
return data

def clean_preferential_quota_order_number(self):
data = self.cleaned_data["preferential_quota_order_number"]
if not data:
raise ValidationError(
"Quota Order Number is not valid - it must have a value",
)
return data

commodity_code = forms.CharField(
help_text="Commodity Code",
max_length=10,
help_text="Enter the 10 digit commodity code",
validators=[commodity_code_validator],
error_messages={
"invalid": "Commodity code should be 10 digits",
"required": "Commodity code is required",
"required": "Enter the commodity code",
},
)

Expand Down Expand Up @@ -109,11 +150,9 @@ def __init__(self, reference_document_version, *args, **kwargs):
},
)

def clean_quota_duty_rate(self):
data = self.cleaned_data["quota_duty_rate"]
if len(data) < 1:
raise ValidationError("Quota duty Rate is not valid - it must have a value")
return data
end_date = DateInputFieldFixed(
label="End date",
)


class PreferentialQuotaBulkCreate(forms.Form):
Expand Down Expand Up @@ -350,3 +389,24 @@ def clean_validity_period(
month=int(month),
year=int(year),
)


class PreferentialQuotaDeleteForm(forms.Form):
def __init__(self, *args, **kwargs) -> None:
self.instance = kwargs.pop("instance")
super().__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.label_size = Size.SMALL
self.helper.legend_size = Size.SMALL
self.helper.layout = Layout(
Submit(
"submit",
"Confirm Delete",
data_module="govuk-button",
data_prevent_double_click="true",
),
)

class Meta:
model = PreferentialQuotaOrderNumber
fields = []
46 changes: 36 additions & 10 deletions reference_documents/forms/preferential_quota_order_number_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.core.exceptions import ValidationError

from common.forms import ValidityPeriodForm
from reference_documents.models import PreferentialQuota
from reference_documents.models import PreferentialQuotaOrderNumber


Expand Down Expand Up @@ -48,6 +49,14 @@ def __init__(self, reference_document_version, *args, **kwargs):
),
)

def clean_coefficient(self):
cleaned_data = super().clean()
coefficient = cleaned_data.get("coefficient")
if coefficient == "":
return None

return coefficient

def clean(self):
cleaned_data = super().clean()
coefficient = cleaned_data.get("coefficient")
Expand All @@ -65,10 +74,12 @@ def clean(self):

def clean_quota_order_number(self):
data = self.cleaned_data["quota_order_number"]
if self.reference_document_version.preferential_quota_order_numbers.filter(
quota_order_number=data,
).exists():
raise ValidationError("Quota Order Number Already Exists")
if self.instance._state.adding:
if self.reference_document_version.preferential_quota_order_numbers.filter(
quota_order_number=data,
).exists():
raise ValidationError("Quota Order Number Already Exists")

return data

quota_order_number = forms.CharField(
Expand Down Expand Up @@ -111,12 +122,9 @@ def clean_quota_order_number(self):
)


class PreferentialQuotaOrderNumberDeleteForm(forms.ModelForm):
class Meta:
model = PreferentialQuotaOrderNumber
fields = []

def __init__(self, *args, **kwargs):
class PreferentialQuotaOrderNumberDeleteForm(forms.Form):
def __init__(self, *args, **kwargs) -> None:
self.instance = kwargs.pop("instance")
super().__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.label_size = Size.SMALL
Expand All @@ -129,3 +137,21 @@ def __init__(self, *args, **kwargs):
data_prevent_double_click="true",
),
)

def clean(self):
cleaned_data = super().clean()
quota_order_number = self.instance
versions = PreferentialQuota.objects.all().filter(
preferential_quota_order_number=quota_order_number,
)
if versions:
raise forms.ValidationError(
f"Quota Order Number {quota_order_number} cannot be deleted as it has"
f" associated Preferential Quotas.",
)

return cleaned_data

class Meta:
model = PreferentialQuotaOrderNumber
fields = []
1 change: 1 addition & 0 deletions reference_documents/forms/preferential_rate_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class PreferentialRateCreateUpdateForm(
forms.ModelForm,
):
commodity_code = forms.CharField(
max_length=10,
help_text="Enter the 10 digit commodity code",
validators=[commodity_code_validator],
error_messages={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
{% endif %}
</div>
<div class="govuk-body govuk-!-margin-bottom-2">
<a href="{{ url('reference_documents:preferential_quota_order_number_edit', args=[value['ref_doc_order_number'].id]) }}">Edit</a>
<a href="{{ url('reference_documents:preferential_quota_order_number_delete', args=[value['ref_doc_order_number'].id]) }}">Delete</a>
<a class="order_number_link" href="{{ url('reference_documents:preferential_quota_order_number_edit', args=[value['ref_doc_order_number'].id]) }}">Edit</a>
<a class="order_number_link" href="{{ url('reference_documents:preferential_quota_order_number_delete', args=[value['ref_doc_order_number'].id, value['ref_doc_order_number'].reference_document_version.id]) }}">Delete</a>
<a class="order_number_link" href="{{ url('reference_documents:preferential_quotas_create_for_order', args=[object.pk, value['ref_doc_order_number'].pk]) }}">Add Quota to Order</a>
<a class="order_number_link" href="{{ url('reference_documents:preferential_quota_order_number_delete', args=[value['ref_doc_order_number'].id, value['ref_doc_order_number'].reference_document_version.id]) }}">Bulk Add Quotas</a>
</div>
{% if value['data_rows'] != [] %}
{{ govukTable({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{% extends "layouts/layout.jinja" %}

{% from "components/panel/macro.njk" import govukPanel %}
{% from "components/button/macro.njk" import govukButton %}
{% from "components/breadcrumbs/macro.njk" import govukBreadcrumbs %}

{% set area_id = request.session['deleted_version']['area_id'] %}
{% set version = request.session['deleted_version']['version'] %}
{% set ref_doc_pk = request.session['deleted_version']['ref_doc_pk'] %}

{% set page_title = "Reference Document " ~ area_id ~ " version " ~ version ~ " successfully deleted" %}


{% block breadcrumb %}
{{ govukBreadcrumbs({
"items": [{"text": "Home", "href": url("home")},
{"text": "View reference documents", "href": url("reference_documents:index")},
{"text": "Reference Document " ~ area_id, "href": url("reference_documents:details", kwargs={"pk":ref_doc_pk})},
{"text": "Delete Reference Document " ~ area_id ~ " version " ~ version},
{"text": page_title}]
}) }}
{% endblock %}

{% block content %}
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
{{ govukPanel({
"titleText": "Reference Document " ~ request.session['deleted_version']['area_id'] ~ " version " ~ request.session['deleted_version']['version'] ~ " has been deleted",
"text": "This change has taken immediate effect",
"classes": "govuk-!-margin-bottom-7"
}) }}
</div>
</div>
{{ govukButton({
"text": "Back to View reference documents",
"href": url("reference_documents:index"),
"classes": "govuk-button--secondary"
}) }}
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{% set page_title = "todo : Delete preferential quota order number" %}

{% block breadcrumb %}
{{ govukBreadcrumbs({
{{ govukBreadcrumbs({
"items": [{"text": "Home", "href": url("home")},
{"text": "View reference documents", "href": url("reference_documents:index")},
{"text": "Reference document " ~ object.reference_document_version.reference_document.area_id, "href": url("reference_documents:details", kwargs={"pk":object.reference_document_version.reference_document.pk})},
Expand All @@ -18,58 +18,61 @@
{% endblock %}

{% block content %}
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-xl">{{ page_title }}</h1>
</div>
</div>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-xl">{{ page_title }}</h1>
</div>
</div>

<div class="govuk-grid-row govuk-!-margin-bottom-5">
<div class="govuk-grid-column-two-thirds">
<p class="govuk-body">Are you sure you want to permanently delete preferential duty rate for commodity {{ object.commodity_code }} reference document {{ object.reference_document_version.reference_document.area_id }}?</p>
<div class="govuk-grid-row govuk-!-margin-bottom-5">
<div class="govuk-grid-column-two-thirds">
<p class="govuk-body">
Are you sure you want to permanently delete preferential quota order number {{ object.quota_order_number }},
reference document {{ object.reference_document_version.reference_document.area_id }}?
</p>

{{ govukWarningText({
"text": "Deleted preferential rates cannot be recovered.",
"iconFallbackText": "Warning"
}) }}
{{ govukWarningText({
"text": "Deleted preferential quota order number cannot be recovered.",
"iconFallbackText": "Warning"
}) }}

<form method="post">
<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
<form method="post">
<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">

{% set error_list = [] %}
{% set error_list = [] %}

{% for field, errors in form.errors.items() %}
{% for error in errors.data %}
{% if error.message|length > 1 %}
{{ error_list.append({
{% for field, errors in form.errors.items() %}
{% for error in errors.data %}
{% if error.message|length > 1 %}
{{ error_list.append({
"text": error.message,
"href": "#" ~ (form.prefix ~ "-" if form.prefix else "") ~ field ~ ("_" ~ error.subfield if error.subfield is defined else ""),
}) or "" }}
{% endif %}
{% endfor %}
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}

{% if error_list|length > 0 %}
{{ govukErrorSummary({
{% if error_list|length > 0 %}
{{ govukErrorSummary({
"titleText": "There is a problem",
"errorList": error_list
}) }}
{% endif %}
{% endif %}

<div class="govuk-button-group">
{{ govukButton({
<div class="govuk-button-group">
{{ govukButton({
"text": "Delete",
"classes": "govuk-button--warning",
"name": "action",
"value": "delete"
}) }}
{{ govukButton({
{{ govukButton({
"text": "Cancel",
"href": url("reference_documents:index"),
"classes": "govuk-button--secondary"
}) }}
</div>
</form>
</div>
</div>
</div>
</form>
</div>
</div>
{% endblock %}
Loading

0 comments on commit adfd87e

Please sign in to comment.