Skip to content

Commit

Permalink
Fix transaction error
Browse files Browse the repository at this point in the history
  • Loading branch information
eadpearce committed Dec 20, 2024
1 parent 8e1c336 commit 1b0147a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 33 deletions.
11 changes: 8 additions & 3 deletions quotas/forms/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class QuotaDefinitionCreateForm(
class Meta:
model = models.QuotaDefinition
fields = [
"order_number",
"valid_between",
"description",
"volume",
Expand All @@ -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={
Expand All @@ -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",
Expand All @@ -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"},
)
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion quotas/jinja2/includes/quotas/actions.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{% endif %}
<br>
<strong class="govuk-heading-xs">Create quota data</strong>
<li><a class="govuk-link" href="{{ url('quota_definition-ui-create', args=[object.sid]) }}">Create definition period</a></li>
<li><a class="govuk-link" href="{{ url('quota_definition-ui-create', args=[object.sid]) }}?order_number={{object.order_number}}">Create definition period</a></li>
<li><a class="govuk-link" href="{{ url('quota_definition-ui-bulk-create', args=[object.sid]) }}">The bulk creator</a></li>
<li><a class="govuk-link" href="{{ url('sub_quota_definitions-ui-create') }}">Create quota associations</a></li>
<li><a class="govuk-link" href="{{ url('quota_suspension_or_blocking-ui-create', args=[object.sid]) }}">Create suspension or blocking period</a></li>
Expand Down
4 changes: 2 additions & 2 deletions quotas/jinja2/quota-definitions/create.jinja
Original file line number Diff line number Diff line change
@@ -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}
])
}}
Expand Down
29 changes: 12 additions & 17 deletions quotas/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 3 additions & 10 deletions quotas/views/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,30 +204,23 @@ 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",
"link": "/workbaskets/current",
}
return kwargs

def form_valid(self, form):
form.instance.order_number = self.quota
return super().form_valid(form)


class QuotaDefinitionConfirmCreate(
QuotaDefinitionMixin,
Expand Down

0 comments on commit 1b0147a

Please sign in to comment.