Refactor PriceBreakCreate form

- Handle non_field_errors
This commit is contained in:
Oliver 2021-06-30 14:07:15 +10:00
parent 8f47035a7b
commit 09fff5b644
5 changed files with 55 additions and 67 deletions

View File

@ -2,6 +2,8 @@
JSON serializers for Company app
"""
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers
from sql_util.utils import SubqueryCount
@ -249,6 +251,12 @@ class SupplierPriceBreakSerializer(InvenTreeModelSerializer):
price = serializers.CharField()
price_currency = serializers.ChoiceField(
choices=djmoney.settings.CURRENCY_CHOICES,
default=currency_code_default,
label=_('Currency'),
)
class Meta:
model = SupplierPriceBreak
fields = [
@ -256,4 +264,5 @@ class SupplierPriceBreakSerializer(InvenTreeModelSerializer):
'part',
'quantity',
'price',
'price_currency',
]

View File

@ -98,12 +98,23 @@ $('#price-break-table').inventreeTable({
});
$('#new-price-break').click(function() {
launchModalForm("{% url 'price-break-create' %}",
constructForm(
'{% url "api-part-supplier-price-list" %}',
{
reload: true,
data: {
part: {{ part.id }},
}
method: 'POST',
fields: {
quantity: {},
part: {
value: {{ part.pk }},
hidden: true,
},
price: {},
price_currency: {
},
},
title: '{% trans "Add Price Break" %}',
reload: true
}
);
});

View File

@ -40,7 +40,6 @@ company_urls = [
]
price_break_urls = [
url('^new/', views.PriceBreakCreate.as_view(), name='price-break-create'),
url(r'^(?P<pk>\d+)/edit/', views.PriceBreakEdit.as_view(), name='price-break-edit'),
url(r'^(?P<pk>\d+)/delete/', views.PriceBreakDelete.as_view(), name='price-break-delete'),

View File

@ -761,67 +761,6 @@ class SupplierPartDelete(AjaxDeleteView):
return self.renderJsonResponse(self.request, data=data, form=self.get_form())
class PriceBreakCreate(AjaxCreateView):
""" View for creating a supplier price break """
model = SupplierPriceBreak
form_class = EditPriceBreakForm
ajax_form_title = _('Add Price Break')
ajax_template_name = 'modal_form.html'
def get_data(self):
return {
'success': _('Added new price break')
}
def get_part(self):
"""
Attempt to extract SupplierPart object from the supplied data.
"""
try:
supplier_part = SupplierPart.objects.get(pk=self.request.GET.get('part'))
return supplier_part
except (ValueError, SupplierPart.DoesNotExist):
pass
try:
supplier_part = SupplierPart.objects.get(pk=self.request.POST.get('part'))
return supplier_part
except (ValueError, SupplierPart.DoesNotExist):
pass
return None
def get_form(self):
form = super(AjaxCreateView, self).get_form()
form.fields['part'].widget = HiddenInput()
return form
def get_initial(self):
initials = super(AjaxCreateView, self).get_initial()
supplier_part = self.get_part()
initials['part'] = self.get_part()
if supplier_part is not None:
currency_code = supplier_part.supplier.currency_code
else:
currency_code = common.settings.currency_code_default()
# Extract the currency object associated with the code
currency = CURRENCIES.get(currency_code, None)
if currency:
initials['price'] = [1.0, currency]
return initials
class PriceBreakEdit(AjaxUpdateView):
""" View for editing a supplier price break """

View File

@ -302,6 +302,8 @@ function constructFormBody(fields, options) {
var html = '';
html += `<div id='non-field-errors'><!-- Empty div for displaying errors --></div>`;
// Client must provide set of fields to be displayed,
// otherwise *all* fields will be displayed
var displayed_fields = options.fields || fields;
@ -653,6 +655,9 @@ function clearFormErrors(options) {
// Remove the "has error" class
$(options.modal).find('.has-error').removeClass('has-error');
// Clear the 'non field errors'
$(options.modal).find('#non-field-errors').html('');
}
@ -669,6 +674,31 @@ function handleFormErrors(errors, fields, options) {
// Remove any existing error messages from the form
clearFormErrors(options);
var non_field_errors = $(options.modal).find('#non-field-errors');
non_field_errors.append(
`<div class='alert alert-block alert-danger'>
<b>{% trans "Form errors exist" %}</b>
</div>`
);
// Non-field errors?
if ('non_field_errors' in errors) {
var nfe = errors.non_field_errors;
for (var idx = 0; idx < nfe.length; idx++) {
var err = nfe[idx];
var html = `
<div class='alert alert-block alert-danger'>
${err}
</div>`;
non_field_errors.append(html);
}
}
for (field_name in errors) {
if (field_name in fields) {