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
  • Loading branch information
mattjamc committed Mar 13, 2024
2 parents 8b39d1a + 0fbeeb4 commit 4c05783
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 49 deletions.
60 changes: 33 additions & 27 deletions reference_documents/forms/preferential_quota_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from reference_documents.models import PreferentialQuota
from reference_documents.models import PreferentialQuotaOrderNumber
from reference_documents.validators import commodity_code_validator
from reference_documents.validators import order_number_validator


class PreferentialQuotaCreateUpdateForm(
Expand All @@ -31,6 +30,31 @@ class Meta:
"valid_between",
]

def __init__(self, reference_document_version, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields[
"quota_order_number"
].queryset = reference_document_version.preferential_quota_order_numbers.all()
self.reference_document_version = reference_document_version
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",
"start_date",
"end_date",
Submit(
"submit",
"Save",
data_module="govuk-button",
data_prevent_double_click="true",
),
)

commodity_code = forms.CharField(
help_text="Commodity Code",
validators=[commodity_code_validator],
Expand All @@ -49,13 +73,16 @@ class Meta:
},
)

quota_order_number = forms.CharField(
help_text="Quota Order Number",
validators=[order_number_validator],
quota_order_number = forms.ModelChoiceField(
label="Quota Order Number",
help_text="Select Quota order number",
queryset=PreferentialQuotaOrderNumber.objects.all(),
validators=[],
error_messages={
"invalid": "Quota Order Number is invalid",
"required": "Quota Order Number is required",
"invalid": "Quota Order number is invalid",
},
required=False,
widget=forms.Select(attrs={"class": "form-control"}),
)

volume = forms.CharField(
Expand All @@ -82,27 +109,6 @@ def clean_quota_duty_rate(self):
raise ValidationError("Quota duty Rate is not valid - it must have a value")
return data

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
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",
"start_date",
"end_date",
Submit(
"submit",
"Save",
data_module="govuk-button",
data_prevent_double_click="true",
),
)


class PreferentialQuotaBulkCreate(ValidityPeriodForm, forms.ModelForm):
commodity_codes = forms.CharField(
Expand Down
25 changes: 25 additions & 0 deletions reference_documents/forms/preferential_quota_order_number_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from crispy_forms_gds.layout import Size
from crispy_forms_gds.layout import Submit
from django import forms
from django.core.exceptions import ValidationError

from common.forms import ValidityPeriodForm
from reference_documents.models import PreferentialQuotaOrderNumber
Expand All @@ -27,6 +28,7 @@ def __init__(self, reference_document_version, *args, **kwargs):
self.fields[
"main_order_number"
].queryset = reference_document_version.preferential_quota_order_numbers.all()
self.reference_document_version = reference_document_version
self.helper = FormHelper(self)
self.helper.label_size = Size.SMALL
self.helper.legend_size = Size.SMALL
Expand All @@ -46,6 +48,29 @@ def __init__(self, reference_document_version, *args, **kwargs):
),
)

def clean(self):
cleaned_data = super().clean()
coefficient = cleaned_data.get("coefficient")
main_order_number = cleaned_data.get("main_order_number")

# cant have one without the other
if coefficient and not main_order_number:
raise ValidationError(
"Coefficient specified without main order number",
)
elif not coefficient and main_order_number:
raise ValidationError(
"Main order number specified without coefficient",
)

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")
return data

quota_order_number = forms.CharField(
label="Order number",
help_text="Enter a six digit number",
Expand Down
24 changes: 20 additions & 4 deletions reference_documents/jinja2/includes/tabs/preferential_quotas.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,26 @@
{% else %}
<h2 class="govuk-heading-m" id="subsection-title">Order Number {{ value["quota_order_number_text"] }}</h2>
{% endif %}
{{ govukTable({
"head": reference_document_version_quotas_headers,
"rows": value['data_rows']
}) }}
<div class="govuk-body govuk-!-margin-bottom-2">
Valid between: {{ value['ref_doc_order_number'].valid_between }}
{% if value['ref_doc_order_number'].main_order_number %}
Sub Quota to {{ value['ref_doc_order_number'].main_order_number.quota_order_number }}
{% 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>
</div>
{% if value['data_rows'] != [] %}
{{ govukTable({
"head": reference_document_version_quotas_headers,
"rows": value['data_rows']
}) }}
{% else %}
<div class="govuk-body govuk-!-margin-bottom-2">
No Quota definitions defined
</div>
{% endif %}
{% endfor %}
</div>

Expand Down
18 changes: 11 additions & 7 deletions reference_documents/views/preferential_quota_order_number_views.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.urls import reverse
from django.views.generic import CreateView
from django.views.generic import DeleteView
from django.views.generic import UpdateView

from reference_documents.forms.preferential_quota_order_number_forms import (
PreferentialQuotaOrderNumberCreateUpdateForm,
)
from reference_documents.models import PreferentialQuota
from reference_documents.models import PreferentialQuotaOrderNumber
from reference_documents.models import ReferenceDocumentVersion


class PreferentialQuotaOrderNumberEditView(PermissionRequiredMixin, UpdateView):
template_name = "reference_documents/preferential_quota_order_numbers/edit.jinja"
permission_required = "reference_documents.edit_reference_document"
model = PreferentialQuota
model = PreferentialQuotaOrderNumber
form_class = PreferentialQuotaOrderNumberCreateUpdateForm

def get_form_kwargs(self):
kwargs = super(PreferentialQuotaOrderNumberEditView, self).get_form_kwargs()
kwargs["reference_document_version"] = PreferentialQuotaOrderNumber.objects.get(
id=self.kwargs["pk"],
).reference_document_version
return kwargs

def get_success_url(self):
Expand All @@ -30,7 +34,7 @@ def get_success_url(self):
class PreferentialQuotaOrderNumberCreateView(PermissionRequiredMixin, CreateView):
template_name = "reference_documents/preferential_quota_order_numbers/edit.jinja"
permission_required = "reference_documents.edit_reference_document"
model = PreferentialQuota
model = PreferentialQuotaOrderNumber
form_class = PreferentialQuotaOrderNumberCreateUpdateForm

def get_form_kwargs(self):
Expand All @@ -53,7 +57,7 @@ def form_valid(self, form):
def get_success_url(self):
return (
reverse(
"reference_documents:version_details",
"reference_documents:version-details",
args=[self.object.reference_document_version.pk],
)
+ "#tariff-quotas"
Expand All @@ -71,7 +75,7 @@ def get_success_url(self):
# )


class PreferentialQuotaOrderNumberDeleteView(PermissionRequiredMixin, UpdateView):
template_name = "preferential_quota_order_numbers/delete.jinja"
class PreferentialQuotaOrderNumberDeleteView(PermissionRequiredMixin, DeleteView):
template_name = "reference_documents/preferential_quota_order_numbers/delete.jinja"
permission_required = "reference_documents.edit_reference_document"
model = PreferentialQuota
model = PreferentialQuotaOrderNumber
26 changes: 15 additions & 11 deletions reference_documents/views/preferential_quota_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
PreferentialQuotaCreateUpdateForm,
)
from reference_documents.models import PreferentialQuota
from reference_documents.models import PreferentialQuotaOrderNumber
from reference_documents.models import ReferenceDocumentVersion


Expand All @@ -24,6 +25,13 @@ class PreferentialQuotaEditView(PermissionRequiredMixin, UpdateView):
model = PreferentialQuota
form_class = PreferentialQuotaCreateUpdateForm

def get_form_kwargs(self):
kwargs = super(PreferentialQuotaEditView, self).get_form_kwargs()
kwargs["reference_document_version"] = PreferentialQuotaOrderNumber.objects.get(
id=self.kwargs["pk"],
).reference_document_version
return kwargs

def post(self, request, *args, **kwargs):
quota = self.get_object()
quota.save()
Expand All @@ -42,6 +50,13 @@ class PreferentialQuotaCreateView(PermissionRequiredMixin, CreateView):
model = PreferentialQuota
form_class = PreferentialQuotaCreateUpdateForm

def get_form_kwargs(self):
kwargs = super(PreferentialQuotaCreateView, self).get_form_kwargs()
kwargs["reference_document_version"] = ReferenceDocumentVersion.objects.get(
id=self.kwargs["pk"],
)
return kwargs

def form_valid(self, form):
instance = form.instance
reference_document_version = ReferenceDocumentVersion.objects.get(
Expand All @@ -61,17 +76,6 @@ def get_success_url(self):
+ "#tariff-quotas"
)

# def post(self, request, *args, **kwargs):
# quota = self.get_object()
# quota.save()
# return redirect(
# reverse(
# "reference_documents:version_details",
# args=[quota.reference_document_version.pk],
# )
# + "#tariff-quotas",
# )


class PreferentialQuotaBulkCreateView(PermissionRequiredMixin, FormView):
template_name = "reference_documents/preferential_quotas/bulk_create.jinja"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ def quotas_data_orders_and_rows(self):
data[ref_doc_order_number.quota_order_number] = {
"data_rows": [],
"quota_order_number": tap_quota_order_number,
"ref_doc_order_number": ref_doc_order_number,
"quota_order_number_text": ref_doc_order_number.quota_order_number,
"failure_count": failure_count,
"check_count": check_count,
Expand Down

0 comments on commit 4c05783

Please sign in to comment.