From 09fff5b64403812e69b35658e3bb0eb88856d7f2 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 30 Jun 2021 14:07:15 +1000 Subject: [PATCH] Refactor PriceBreakCreate form - Handle non_field_errors --- InvenTree/company/serializers.py | 9 +++ .../company/supplier_part_pricing.html | 21 +++++-- InvenTree/company/urls.py | 1 - InvenTree/company/views.py | 61 ------------------- InvenTree/templates/js/forms.js | 30 +++++++++ 5 files changed, 55 insertions(+), 67 deletions(-) diff --git a/InvenTree/company/serializers.py b/InvenTree/company/serializers.py index 9e64fbad0a..9f40365757 100644 --- a/InvenTree/company/serializers.py +++ b/InvenTree/company/serializers.py @@ -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', ] diff --git a/InvenTree/company/templates/company/supplier_part_pricing.html b/InvenTree/company/templates/company/supplier_part_pricing.html index 89b33049c6..5d2d0c5e31 100644 --- a/InvenTree/company/templates/company/supplier_part_pricing.html +++ b/InvenTree/company/templates/company/supplier_part_pricing.html @@ -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 } ); }); diff --git a/InvenTree/company/urls.py b/InvenTree/company/urls.py index 51685215b6..840f95b771 100644 --- a/InvenTree/company/urls.py +++ b/InvenTree/company/urls.py @@ -40,7 +40,6 @@ company_urls = [ ] price_break_urls = [ - url('^new/', views.PriceBreakCreate.as_view(), name='price-break-create'), url(r'^(?P\d+)/edit/', views.PriceBreakEdit.as_view(), name='price-break-edit'), url(r'^(?P\d+)/delete/', views.PriceBreakDelete.as_view(), name='price-break-delete'), diff --git a/InvenTree/company/views.py b/InvenTree/company/views.py index 1b817fe66d..cc046173f9 100644 --- a/InvenTree/company/views.py +++ b/InvenTree/company/views.py @@ -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 """ diff --git a/InvenTree/templates/js/forms.js b/InvenTree/templates/js/forms.js index d568e40fe7..a4baa8707f 100644 --- a/InvenTree/templates/js/forms.js +++ b/InvenTree/templates/js/forms.js @@ -302,6 +302,8 @@ function constructFormBody(fields, options) { var html = ''; + html += `
`; + // 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( + `
+ {% trans "Form errors exist" %} +
` + ); + + // 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 = ` +
+ ${err} +
`; + + non_field_errors.append(html); + } + } + for (field_name in errors) { if (field_name in fields) {