diff --git a/quotas/forms/definitions.py b/quotas/forms/definitions.py index f01cea9a3..9ddd8a1b5 100644 --- a/quotas/forms/definitions.py +++ b/quotas/forms/definitions.py @@ -144,6 +144,7 @@ class QuotaDefinitionCreateForm( class Meta: model = models.QuotaDefinition fields = [ + "order_number", "valid_between", "description", "volume", @@ -155,10 +156,13 @@ class Meta: "maximum_precision", ] + order_number = forms.ModelChoiceField( + queryset=models.QuotaOrderNumber.objects.current(), + widget=forms.HiddenInput(), + ) description = forms.CharField(label="", widget=forms.Textarea(), required=False) volume = forms.DecimalField( label="Current volume", - required=True, help_text="The current volume is the starting balance for the quota", widget=forms.TextInput(), error_messages={ @@ -168,7 +172,6 @@ class Meta: ) initial_volume = forms.DecimalField( widget=forms.TextInput(), - required=True, help_text="The initial volume is the legal balance applied to the definition period", error_messages={ "invalid": "Initial volume must be a number", @@ -177,7 +180,6 @@ class Meta: ) measurement_unit = forms.ModelChoiceField( queryset=MeasurementUnit.objects.current(), - required=True, empty_label="Choose measurement unit", error_messages={"required": "Select the measurement unit"}, ) @@ -205,6 +207,7 @@ class Meta: def __init__(self, *args, **kwargs): self.buttons = kwargs.pop("buttons", None) + self.order_number = kwargs.pop("order_number", None) super().__init__(*args, **kwargs) self.init_layout() self.init_fields() @@ -218,6 +221,8 @@ def init_fields(self): # see https://uktrade.github.io/tariff-data-manual/documentation/data-structures/quotas.html#the-quota-definition-table self.fields["maximum_precision"].initial = 3 + self.fields["order_number"].initial = self.order_number + # Set these as the default values self.fields["quota_critical"].initial = False self.fields["quota_critical_threshold"].initial = 90 diff --git a/quotas/jinja2/includes/quotas/actions.jinja b/quotas/jinja2/includes/quotas/actions.jinja index c4dde5f8a..3798eac1f 100644 --- a/quotas/jinja2/includes/quotas/actions.jinja +++ b/quotas/jinja2/includes/quotas/actions.jinja @@ -13,7 +13,7 @@ {% endif %}
Create quota data -
  • Create definition period
  • +
  • Create definition period
  • The bulk creator
  • Create quota associations
  • Create suspension or blocking period
  • diff --git a/quotas/jinja2/quota-definitions/create.jinja b/quotas/jinja2/quota-definitions/create.jinja index d2bed6c1a..f063e9f6e 100644 --- a/quotas/jinja2/quota-definitions/create.jinja +++ b/quotas/jinja2/quota-definitions/create.jinja @@ -1,11 +1,11 @@ {% extends 'layouts/form.jinja' %} {% set page_title = "Create a new quota definition period" %} -{% set page_subtitle = "Quota order number: "~ quota.order_number %} +{% set page_subtitle = "Quota order number: "~ quota_order_number %} {% block breadcrumb %} {{ breadcrumbs(request, [ {"text": "Find and edit quotas", "href": url("quota-ui-list")}, - {"text": "Quota order number", "href": url("quota-ui-detail", args=[quota.sid]) ~ "#definitions"}, + {"text": "Quota order number", "href": url("quota-ui-detail", args=[quota_sid]) ~ "#definitions"}, {"text": page_title} ]) }} diff --git a/quotas/tests/test_views.py b/quotas/tests/test_views.py index 9aec99a64..27fc2846d 100644 --- a/quotas/tests/test_views.py +++ b/quotas/tests/test_views.py @@ -4,8 +4,6 @@ import pytest from bs4 import BeautifulSoup from django.contrib.humanize.templatetags.humanize import intcomma -from django.db import IntegrityError -from django.db import transaction from django.urls import reverse from common.models.transactions import Transaction @@ -1367,24 +1365,21 @@ def test_create_new_quota_definition_business_rule_violation( } url = reverse("quota_definition-ui-create", kwargs={"sid": quota.sid}) - try: - with transaction.atomic(): - response = client_with_current_workbasket.post(url, form_data) + response = client_with_current_workbasket.post( + f"{url}?order_number={quota.order_number}", + form_data, + ) - assert response.status_code == 200 + assert response.status_code == 200 - soup = BeautifulSoup(response.content.decode(response.charset), "html.parser") + soup = BeautifulSoup(response.content.decode(response.charset), "html.parser") - assert soup.select(".govuk-error-summary") - errors = [ - el.text.strip() for el in soup.select(".govuk-error-summary__list li") - ] - assert ( - "The validity period of the quota definition must be spanned by one of the validity periods of the referenced quota order number." - in errors - ) - except IntegrityError: - pass + assert soup.select(".govuk-error-summary") + errors = [el.text.strip() for el in soup.select(".govuk-error-summary__list li")] + assert ( + "The validity period of the quota definition must be spanned by one of the validity periods of the referenced quota order number." + in errors + ) @pytest.mark.django_db diff --git a/quotas/views/definitions.py b/quotas/views/definitions.py index 7a78fc200..ba9af6c5a 100644 --- a/quotas/views/definitions.py +++ b/quotas/views/definitions.py @@ -204,19 +204,16 @@ class QuotaDefinitionCreate(CreateTaricCreateView): def quota(self): return models.QuotaOrderNumber.objects.current().get(sid=self.kwargs["sid"]) - @transaction.atomic - def get_result_object(self, form): - object = super().get_result_object(form) - return object - def get_context_data(self, **kwargs): return super().get_context_data( - quota=self.quota, + quota_order_number=self.request.GET["order_number"], + quota_sid=self.kwargs["sid"], **kwargs, ) def get_form_kwargs(self): kwargs = super().get_form_kwargs() + kwargs["order_number"] = self.quota kwargs["buttons"] = { "submit": "Submit", "link_text": "Cancel", @@ -224,10 +221,6 @@ def get_form_kwargs(self): } return kwargs - def form_valid(self, form): - form.instance.order_number = self.quota - return super().form_valid(form) - class QuotaDefinitionConfirmCreate( QuotaDefinitionMixin,