Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TP2000-1537 Bulk create QuotaDefinitionPeriods #1320

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions common/static/common/scss/_quota-definitions.scss
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@

.definition-original {
color: $govuk-secondary-text-colour;
}

.bulk-create-review {
details.govuk-details[open] {
height: 420px !important;
}
}
16 changes: 16 additions & 0 deletions common/templates/common/widgets/decimal_suffix.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div class="govuk-input__wrapper">
<input
class="numberinput govuk-input govuk-input govuk-input--width-2"
type="{{ widget.type }}"
name="{{ widget.name }}"
{% if widget.value != None %}
value="{{ widget.value|stringformat:'s' }}"
{% endif %}
{% include "django/forms/widgets/attrs.html" %}>
{% if widget.suffix %}
<div class="govuk-input__suffix" aria-hidden="true">{{widget.suffix}}</div>
{% endif %}
<label{% if widget.attrs.id %} id="{{ widget.attrs.id }}-label" for="{{ widget.attrs.id }}"{% endif %} class="govuk-label">
{{ widget.label }}
</label>
</div>
1 change: 1 addition & 0 deletions common/tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ def assert_model_view_renders(
assert factory is not None, f"Factory not found: factories.{factory_class_name}"

instance = factory.create()

if isinstance(instance, GoodsNomenclature):
requests_mock.get(
url=f"{Endpoints.COMMODITIES.value}{instance.item_id}",
Expand Down
20 changes: 20 additions & 0 deletions common/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,23 @@ class MultipleFileInput(FileInput):
"""Enable multiple files to be selected using FileInput widget."""

allow_multiple_selected = True


class DecimalSuffix(widgets.NumberInput):
"""Identical to the Decimal widget but allows the addition of a suffix."""

template_name = "common/widgets/decimal_suffix.html"

def __init__(self, attrs=None, suffix="", **kwargs):
self.suffix = suffix
super().__init__(attrs, **kwargs)

def render(self, name, value, attrs=None, renderer=None):
if attrs is None:
attrs = {}
context = self.get_context(name, value, attrs)
if self.suffix:
context["widget"]["suffix"] = self.suffix

template = loader.get_template(self.template_name).render(context)
return mark_safe(template)
116 changes: 79 additions & 37 deletions quotas/forms/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from crispy_forms_gds.helper import FormHelper
from crispy_forms_gds.layout import HTML
from crispy_forms_gds.layout import Accordion
from crispy_forms_gds.layout import AccordionSection
from crispy_forms_gds.layout import Div
from crispy_forms_gds.layout import Field
from crispy_forms_gds.layout import Layout
Expand All @@ -14,6 +12,7 @@

from common.forms import ValidityPeriodForm
from common.serializers import deserialize_date
from common.widgets import DecimalSuffix
from measures.models import MeasurementUnit
from quotas import business_rules
from quotas import models
Expand Down Expand Up @@ -159,6 +158,8 @@ class Meta:
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={
"invalid": "Volume must be a number",
Expand All @@ -167,20 +168,24 @@ 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",
"required": "Enter the initial volume",
},
)
measurement_unit = forms.ModelChoiceField(
queryset=MeasurementUnit.objects.current(),
required=True,
empty_label="Choose measurement unit",
error_messages={"required": "Select the measurement unit"},
)

quota_critical_threshold = forms.DecimalField(
label="Threshold",
widget=DecimalSuffix(suffix="%"),
help_text="The point at which this quota definition period becomes critical, as a percentage of the total volume.",
widget=forms.TextInput(),
error_messages={
"invalid": "Critical threshold must be a number",
"required": "Enter the critical threshold",
Expand All @@ -199,6 +204,7 @@ class Meta:
)

def __init__(self, *args, **kwargs):
self.buttons = kwargs.pop("buttons", None)
super().__init__(*args, **kwargs)
self.init_layout()
self.init_fields()
Expand Down Expand Up @@ -230,50 +236,86 @@ def init_fields(self):
lambda obj: f"{obj.code} - {obj.description}"
)

def init_layout(self):
self.fields["end_date"].help_text = ""
self.fields["measurement_unit_qualifier"].help_text = (
"A measurement unit qualifier is not always required."
)
self.fields["measurement_unit_qualifier"].empty_label = (
"Choose measurement unit qualifier."
)

def init_layout(self, *args, **kwargs):
self.helper = FormHelper(self)
self.helper.label_size = Size.SMALL
self.helper.legend_size = Size.SMALL

link_text = self.buttons["link_text"]
link = self.buttons["link"]
self.helper.layout = Layout(
Accordion(
AccordionSection(
"Description",
HTML.p("Adding a description is optional."),
"description",
"order_number",
Div(
HTML(
'<h3 class="govuk-heading">Validity period</h3>',
),
AccordionSection(
"Validity period",
"start_date",
"end_date",
"start_date",
"end_date",
),
HTML(
"<br />",
),
Div(
HTML(
'<h3 class="govuk-heading">Measurements</h3>',
),
AccordionSection(
"Measurements",
HTML.p("A measurement unit qualifier is not always required."),
Field("measurement_unit", css_class="govuk-!-width-full"),
Field("measurement_unit_qualifier", css_class="govuk-!-width-full"),
Field("measurement_unit", css_class="govuk-!-width-two-thirds"),
Field(
"measurement_unit_qualifier",
css_class="govuk-!-width-two-thirds",
),
AccordionSection(
"Volume",
HTML.p(
"The initial volume is the legal balance applied to the definition period.<br><br>The current volume is the starting balance for the quota.",
),
"initial_volume",
"volume",
"maximum_precision",
),
HTML(
"<br />",
),
Div(
HTML(
'<h3 class="govuk-heading">Volume</h3>',
),
AccordionSection(
"Criticality",
"quota_critical_threshold",
"quota_critical",
Field("initial_volume", css_class="govuk-!-width-one-third"),
Field("volume", css_class="govuk-!-width-one-third"),
"maximum_precision",
),
HTML(
"<br />",
),
Div(
HTML(
'<h3 class="govuk-heading">Criticality</h3>',
),
"quota_critical_threshold",
"quota_critical",
),
Submit(
"submit",
"Save",
data_module="govuk-button",
data_prevent_double_click="true",
HTML(
"<br />",
),
Div(
HTML(
'<h3 class="govuk-heading">Description</h3>',
),
HTML.p("Adding a description is optional."),
Field("description", css_class="govuk-!-width-two-thirds"),
),
HTML(
"<br />",
),
Div(
Submit(
"submit",
self.buttons["submit"],
data_module="govuk-button",
data_prevent_double_click="true",
),
HTML(
f'<a class="govuk-link" href={link}>{link_text}</a>',
),
css_class="govuk-button-group",
),
)

Expand Down
Loading
Loading