2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-04 20:51:00 +00:00

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 """