Merge pull request #1806 from SchrodingersGat/manufacturer-part-api-form

Refactor ManufacturerPartCreate form
This commit is contained in:
Oliver 2021-07-12 22:23:15 +10:00 committed by GitHub
commit 374c3676a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 43 additions and 167 deletions

View File

@ -12,7 +12,6 @@ from django.utils.translation import ugettext_lazy as _
import django.forms import django.forms
from .models import Company from .models import Company
from .models import ManufacturerPart
from .models import SupplierPart from .models import SupplierPart
from .models import SupplierPriceBreak from .models import SupplierPriceBreak
@ -35,25 +34,6 @@ class CompanyImageDownloadForm(HelperForm):
] ]
class EditManufacturerPartForm(HelperForm):
""" Form for editing a ManufacturerPart object """
field_prefix = {
'link': 'fa-link',
'MPN': 'fa-hashtag',
}
class Meta:
model = ManufacturerPart
fields = [
'part',
'manufacturer',
'MPN',
'description',
'link',
]
class EditSupplierPartForm(HelperForm): class EditSupplierPartForm(HelperForm):
""" Form for editing a SupplierPart object """ """ Form for editing a SupplierPart object """

View File

@ -53,28 +53,26 @@
{{ block.super }} {{ block.super }}
$("#manufacturer-part-create").click(function () { $("#manufacturer-part-create").click(function () {
launchModalForm(
"{% url 'manufacturer-part-create' %}", constructForm('{% url "api-manufacturer-part-list" %}', {
{ fields: {
data: { part: {},
manufacturer: {{ company.id }}, manufacturer: {
value: {{ company.pk }},
}, },
success: function() { MPN: {
icon: 'fa-hashtag',
},
description: {},
link: {
icon: 'fa-link',
},
},
method: 'POST',
title: '{% trans "Add Manufacturer Part" %}',
onSuccess: function() {
$("#part-table").bootstrapTable("refresh"); $("#part-table").bootstrapTable("refresh");
}, }
secondary: [
{
field: 'part',
label: '{% trans "New Part" %}',
title: '{% trans "Create new Part" %}',
url: "{% url 'part-create' %}"
},
{
field: 'manufacturer',
label: '{% trans "New Manufacturer" %}',
title: '{% trans "Create new Manufacturer" %}',
},
]
}); });
}); });

View File

@ -118,9 +118,13 @@ $('#edit-part').click(function () {
fields: { fields: {
part: {}, part: {},
manufacturer: {}, manufacturer: {},
MPN: {}, MPN: {
icon: 'fa-hashtag',
},
description: {}, description: {},
link: {}, link: {
icon: 'fa-link',
},
}, },
title: '{% trans "Edit Manufacturer Part" %}', title: '{% trans "Edit Manufacturer Part" %}',
reload: true, reload: true,

View File

@ -194,45 +194,6 @@ class ManufacturerPartViewTests(CompanyViewTestBase):
Tests for the ManufacturerPart views. Tests for the ManufacturerPart views.
""" """
def test_manufacturer_part_create(self):
"""
Test the ManufacturerPartCreate view.
"""
url = reverse('manufacturer-part-create')
# First check that we can GET the form
response = self.client.get(url, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(response.status_code, 200)
# How many manufaturer parts are already in the database?
n = ManufacturerPart.objects.all().count()
data = {
'part': 1,
'manufacturer': 6,
}
# MPN is required! (form should fail)
(response, errors) = self.post(url, data, valid=False)
self.assertIsNotNone(errors.get('MPN', None))
data['MPN'] = 'TEST-ME-123'
(response, errors) = self.post(url, data, valid=True)
# Check that the ManufacturerPart was created!
self.assertEqual(n + 1, ManufacturerPart.objects.all().count())
# Try to create duplicate ManufacturerPart
(response, errors) = self.post(url, data, valid=False)
self.assertIsNotNone(errors.get('__all__', None))
# Check that the ManufacturerPart count stayed the same
self.assertEqual(n + 1, ManufacturerPart.objects.all().count())
def test_supplier_part_create(self): def test_supplier_part_create(self):
""" """
Test that the SupplierPartCreate view creates Manufacturer Part. Test that the SupplierPartCreate view creates Manufacturer Part.

View File

@ -38,7 +38,6 @@ company_urls = [
] ]
manufacturer_part_urls = [ manufacturer_part_urls = [
url(r'^new/?', views.ManufacturerPartCreate.as_view(), name='manufacturer-part-create'),
url(r'^(?P<pk>\d+)/', include([ url(r'^(?P<pk>\d+)/', include([
url(r'^suppliers/', views.ManufacturerPartDetail.as_view(template_name='company/manufacturer_part_suppliers.html'), name='manufacturer-part-suppliers'), url(r'^suppliers/', views.ManufacturerPartDetail.as_view(template_name='company/manufacturer_part_suppliers.html'), name='manufacturer-part-suppliers'),

View File

@ -29,7 +29,6 @@ from .models import SupplierPart
from part.models import Part from part.models import Part
from .forms import EditManufacturerPartForm
from .forms import EditSupplierPartForm from .forms import EditSupplierPartForm
from .forms import CompanyImageDownloadForm from .forms import CompanyImageDownloadForm
@ -242,74 +241,6 @@ class ManufacturerPartDetail(DetailView):
return ctx return ctx
class ManufacturerPartCreate(AjaxCreateView):
""" Create view for making new ManufacturerPart """
model = ManufacturerPart
form_class = EditManufacturerPartForm
ajax_template_name = 'company/manufacturer_part_create.html'
ajax_form_title = _('Create New Manufacturer Part')
context_object_name = 'part'
def get_context_data(self):
"""
Supply context data to the form
"""
ctx = super().get_context_data()
# Add 'part' object
form = self.get_form()
part = form['part'].value()
try:
part = Part.objects.get(pk=part)
except (ValueError, Part.DoesNotExist):
part = None
ctx['part'] = part
return ctx
def get_form(self):
""" Create Form instance to create a new ManufacturerPart object.
Hide some fields if they are not appropriate in context
"""
form = super(AjaxCreateView, self).get_form()
if form.initial.get('part', None):
# Hide the part field
form.fields['part'].widget = HiddenInput()
return form
def get_initial(self):
""" Provide initial data for new ManufacturerPart:
- If 'manufacturer_id' provided, pre-fill manufacturer field
- If 'part_id' provided, pre-fill part field
"""
initials = super(ManufacturerPartCreate, self).get_initial().copy()
manufacturer_id = self.get_param('manufacturer')
part_id = self.get_param('part')
if manufacturer_id:
try:
initials['manufacturer'] = Company.objects.get(pk=manufacturer_id)
except (ValueError, Company.DoesNotExist):
pass
if part_id:
try:
initials['part'] = Part.objects.get(pk=part_id)
except (ValueError, Part.DoesNotExist):
pass
return initials
class SupplierPartDetail(DetailView): class SupplierPartDetail(DetailView):
""" Detail view for SupplierPart """ """ Detail view for SupplierPart """
model = SupplierPart model = SupplierPart

View File

@ -148,20 +148,23 @@
}); });
$('#manufacturer-create').click(function () { $('#manufacturer-create').click(function () {
launchModalForm(
"{% url 'manufacturer-part-create' %}", constructForm('{% url "api-manufacturer-part-list" %}', {
{ fields: {
reload: true, part: {
data: { value: {{ part.pk }},
part: {{ part.id }} hidden: true,
}, },
secondary: [ manufacturer: {},
{ MPN: {},
field: 'manufacturer', description: {},
label: '{% trans "New Manufacturer" %}', link: {},
title: '{% trans "Create new manufacturer" %}', },
method: 'POST',
title: '{% trans "Add Manufacturer Part" %}',
onSuccess: function() {
$("#manufacturer-table").bootstrapTable("refresh");
} }
]
}); });
}); });