mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Refactor PriceBreakCreate form
- Handle non_field_errors
This commit is contained in:
parent
8f47035a7b
commit
09fff5b644
@ -2,6 +2,8 @@
|
|||||||
JSON serializers for Company app
|
JSON serializers for Company app
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
from sql_util.utils import SubqueryCount
|
from sql_util.utils import SubqueryCount
|
||||||
@ -249,6 +251,12 @@ class SupplierPriceBreakSerializer(InvenTreeModelSerializer):
|
|||||||
|
|
||||||
price = serializers.CharField()
|
price = serializers.CharField()
|
||||||
|
|
||||||
|
price_currency = serializers.ChoiceField(
|
||||||
|
choices=djmoney.settings.CURRENCY_CHOICES,
|
||||||
|
default=currency_code_default,
|
||||||
|
label=_('Currency'),
|
||||||
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = SupplierPriceBreak
|
model = SupplierPriceBreak
|
||||||
fields = [
|
fields = [
|
||||||
@ -256,4 +264,5 @@ class SupplierPriceBreakSerializer(InvenTreeModelSerializer):
|
|||||||
'part',
|
'part',
|
||||||
'quantity',
|
'quantity',
|
||||||
'price',
|
'price',
|
||||||
|
'price_currency',
|
||||||
]
|
]
|
||||||
|
@ -98,12 +98,23 @@ $('#price-break-table').inventreeTable({
|
|||||||
});
|
});
|
||||||
|
|
||||||
$('#new-price-break').click(function() {
|
$('#new-price-break').click(function() {
|
||||||
launchModalForm("{% url 'price-break-create' %}",
|
|
||||||
|
constructForm(
|
||||||
|
'{% url "api-part-supplier-price-list" %}',
|
||||||
{
|
{
|
||||||
reload: true,
|
method: 'POST',
|
||||||
data: {
|
fields: {
|
||||||
part: {{ part.id }},
|
quantity: {},
|
||||||
}
|
part: {
|
||||||
|
value: {{ part.pk }},
|
||||||
|
hidden: true,
|
||||||
|
},
|
||||||
|
price: {},
|
||||||
|
price_currency: {
|
||||||
|
},
|
||||||
|
},
|
||||||
|
title: '{% trans "Add Price Break" %}',
|
||||||
|
reload: true
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
@ -40,7 +40,6 @@ company_urls = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
price_break_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+)/edit/', views.PriceBreakEdit.as_view(), name='price-break-edit'),
|
||||||
url(r'^(?P<pk>\d+)/delete/', views.PriceBreakDelete.as_view(), name='price-break-delete'),
|
url(r'^(?P<pk>\d+)/delete/', views.PriceBreakDelete.as_view(), name='price-break-delete'),
|
||||||
|
@ -761,67 +761,6 @@ class SupplierPartDelete(AjaxDeleteView):
|
|||||||
return self.renderJsonResponse(self.request, data=data, form=self.get_form())
|
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):
|
class PriceBreakEdit(AjaxUpdateView):
|
||||||
""" View for editing a supplier price break """
|
""" View for editing a supplier price break """
|
||||||
|
|
||||||
|
@ -302,6 +302,8 @@ function constructFormBody(fields, options) {
|
|||||||
|
|
||||||
var html = '';
|
var html = '';
|
||||||
|
|
||||||
|
html += `<div id='non-field-errors'><!-- Empty div for displaying errors --></div>`;
|
||||||
|
|
||||||
// Client must provide set of fields to be displayed,
|
// Client must provide set of fields to be displayed,
|
||||||
// otherwise *all* fields will be displayed
|
// otherwise *all* fields will be displayed
|
||||||
var displayed_fields = options.fields || fields;
|
var displayed_fields = options.fields || fields;
|
||||||
@ -653,6 +655,9 @@ function clearFormErrors(options) {
|
|||||||
|
|
||||||
// Remove the "has error" class
|
// Remove the "has error" class
|
||||||
$(options.modal).find('.has-error').removeClass('has-error');
|
$(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
|
// Remove any existing error messages from the form
|
||||||
clearFormErrors(options);
|
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) {
|
for (field_name in errors) {
|
||||||
if (field_name in fields) {
|
if (field_name in fields) {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user