Improvements for "SupplierPartCreate" form

This commit is contained in:
Oliver Walters 2020-04-13 22:21:20 +10:00
parent 0e55911a6b
commit 47530b7d2a
2 changed files with 27 additions and 11 deletions

View File

@ -33,16 +33,29 @@
"{% url 'supplier-part-create' %}", "{% url 'supplier-part-create' %}",
{ {
data: { data: {
supplier: {{ company.id }} {% if company.is_supplier %}supplier: {{ company.id }},{% endif %}
{% if company.is_manufacturer %}manufacturer: {{ company.id }},{% endif %}
}, },
reload: true, reload: true,
secondary: [ secondary: [
{ {
field: 'part', field: 'part',
label: 'New Part', label: '{% trans "New Part" %}',
title: 'Create New Part', title: '{% trans "Create new Part" %}',
url: "{% url 'part-create' %}" url: "{% url 'part-create' %}"
}, },
{
field: 'supplier',
label: "{% trans 'New Supplier' %}",
title: "{% trans 'Create new Supplier' %}",
url: "{% url 'supplier-create' %}",
},
{
field: 'manufacturer',
label: '{% trans "New Manufacturer" %}',
title: '{% trans "Create new Manufacturer" %}',
url: "{% url 'manufacturer-create' %}",
},
] ]
}); });
}); });

View File

@ -274,10 +274,6 @@ class SupplierPartCreate(AjaxCreateView):
""" """
form = super(AjaxCreateView, self).get_form() form = super(AjaxCreateView, self).get_form()
if form.initial.get('supplier', None):
# Hide the supplier field
form.fields['supplier'].widget = HiddenInput()
if form.initial.get('part', None): if form.initial.get('part', None):
# Hide the part field # Hide the part field
form.fields['part'].widget = HiddenInput() form.fields['part'].widget = HiddenInput()
@ -292,20 +288,27 @@ class SupplierPartCreate(AjaxCreateView):
""" """
initials = super(SupplierPartCreate, self).get_initial().copy() initials = super(SupplierPartCreate, self).get_initial().copy()
manufacturer_id = self.get_param('manufacturer')
supplier_id = self.get_param('supplier') supplier_id = self.get_param('supplier')
part_id = self.get_param('part') part_id = self.get_param('part')
if supplier_id: if supplier_id:
try: try:
initials['supplier'] = Company.objects.get(pk=supplier_id) initials['supplier'] = Company.objects.get(pk=supplier_id)
except Company.DoesNotExist: except (ValueError, Company.DoesNotExist):
initials['supplier'] = None pass
if manufacturer_id:
try:
initials['manufacturer'] = Company.objects.get(pk=manufacturer_id)
except (ValueError, Company.DoesNotExist):
pass
if part_id: if part_id:
try: try:
initials['part'] = Part.objects.get(pk=part_id) initials['part'] = Part.objects.get(pk=part_id)
except Part.DoesNotExist: except (ValueError, Part.DoesNotExist):
initials['part'] = None pass
return initials return initials