mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Merge pull request #1806 from SchrodingersGat/manufacturer-part-api-form
Refactor ManufacturerPartCreate form
This commit is contained in:
commit
374c3676a3
@ -12,7 +12,6 @@ from django.utils.translation import ugettext_lazy as _
|
||||
import django.forms
|
||||
|
||||
from .models import Company
|
||||
from .models import ManufacturerPart
|
||||
from .models import SupplierPart
|
||||
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):
|
||||
""" Form for editing a SupplierPart object """
|
||||
|
||||
|
@ -53,28 +53,26 @@
|
||||
{{ block.super }}
|
||||
|
||||
$("#manufacturer-part-create").click(function () {
|
||||
launchModalForm(
|
||||
"{% url 'manufacturer-part-create' %}",
|
||||
{
|
||||
data: {
|
||||
manufacturer: {{ company.id }},
|
||||
|
||||
constructForm('{% url "api-manufacturer-part-list" %}', {
|
||||
fields: {
|
||||
part: {},
|
||||
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");
|
||||
},
|
||||
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" %}',
|
||||
},
|
||||
]
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -118,9 +118,13 @@ $('#edit-part').click(function () {
|
||||
fields: {
|
||||
part: {},
|
||||
manufacturer: {},
|
||||
MPN: {},
|
||||
MPN: {
|
||||
icon: 'fa-hashtag',
|
||||
},
|
||||
description: {},
|
||||
link: {},
|
||||
link: {
|
||||
icon: 'fa-link',
|
||||
},
|
||||
},
|
||||
title: '{% trans "Edit Manufacturer Part" %}',
|
||||
reload: true,
|
||||
|
@ -194,45 +194,6 @@ class ManufacturerPartViewTests(CompanyViewTestBase):
|
||||
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):
|
||||
"""
|
||||
Test that the SupplierPartCreate view creates Manufacturer Part.
|
||||
|
@ -38,7 +38,6 @@ company_urls = [
|
||||
]
|
||||
|
||||
manufacturer_part_urls = [
|
||||
url(r'^new/?', views.ManufacturerPartCreate.as_view(), name='manufacturer-part-create'),
|
||||
|
||||
url(r'^(?P<pk>\d+)/', include([
|
||||
url(r'^suppliers/', views.ManufacturerPartDetail.as_view(template_name='company/manufacturer_part_suppliers.html'), name='manufacturer-part-suppliers'),
|
||||
|
@ -29,7 +29,6 @@ from .models import SupplierPart
|
||||
|
||||
from part.models import Part
|
||||
|
||||
from .forms import EditManufacturerPartForm
|
||||
from .forms import EditSupplierPartForm
|
||||
from .forms import CompanyImageDownloadForm
|
||||
|
||||
@ -242,74 +241,6 @@ class ManufacturerPartDetail(DetailView):
|
||||
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):
|
||||
""" Detail view for SupplierPart """
|
||||
model = SupplierPart
|
||||
|
@ -148,20 +148,23 @@
|
||||
});
|
||||
|
||||
$('#manufacturer-create').click(function () {
|
||||
launchModalForm(
|
||||
"{% url 'manufacturer-part-create' %}",
|
||||
{
|
||||
reload: true,
|
||||
data: {
|
||||
part: {{ part.id }}
|
||||
|
||||
constructForm('{% url "api-manufacturer-part-list" %}', {
|
||||
fields: {
|
||||
part: {
|
||||
value: {{ part.pk }},
|
||||
hidden: true,
|
||||
},
|
||||
secondary: [
|
||||
{
|
||||
field: 'manufacturer',
|
||||
label: '{% trans "New Manufacturer" %}',
|
||||
title: '{% trans "Create new manufacturer" %}',
|
||||
manufacturer: {},
|
||||
MPN: {},
|
||||
description: {},
|
||||
link: {},
|
||||
},
|
||||
method: 'POST',
|
||||
title: '{% trans "Add Manufacturer Part" %}',
|
||||
onSuccess: function() {
|
||||
$("#manufacturer-table").bootstrapTable("refresh");
|
||||
}
|
||||
]
|
||||
});
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user