mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Refactor BuildOrderCreate form
This commit is contained in:
parent
5016d44b83
commit
004b36b1df
@ -196,26 +196,7 @@ src="{% static 'img/blank_image.png' %}"
|
||||
});
|
||||
|
||||
$("#build-edit").click(function () {
|
||||
|
||||
constructForm('{% url "api-build-detail" build.pk %}', {
|
||||
fields: {
|
||||
reference: {
|
||||
prefix: "{% settings_value 'BUILDORDER_REFERENCE_PREFIX' %}",
|
||||
},
|
||||
title: {},
|
||||
part: {},
|
||||
quantity: {},
|
||||
batch: {},
|
||||
target_date: {},
|
||||
take_from: {},
|
||||
destination: {},
|
||||
link: {},
|
||||
issued_by: {},
|
||||
responsible: {},
|
||||
},
|
||||
title: '{% trans "Edit Build Order" %}',
|
||||
reload: true,
|
||||
});
|
||||
editBuildOrder({{ build.pk }});
|
||||
});
|
||||
|
||||
$("#build-cancel").click(function() {
|
||||
|
@ -252,23 +252,6 @@ class TestBuildViews(TestCase):
|
||||
|
||||
self.assertIn(build.title, content)
|
||||
|
||||
def test_build_create(self):
|
||||
""" Test the build creation view (ajax form) """
|
||||
|
||||
url = reverse('build-create')
|
||||
|
||||
# Create build without specifying part
|
||||
response = self.client.get(url, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
# Create build with valid part
|
||||
response = self.client.get(url, {'part': 1}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
# Create build with invalid part
|
||||
response = self.client.get(url, {'part': 9999}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_build_allocate(self):
|
||||
""" Test the part allocation view for a Build """
|
||||
|
||||
|
@ -35,8 +35,6 @@ build_urls = [
|
||||
url('^new/', views.BuildItemCreate.as_view(), name='build-item-create'),
|
||||
])),
|
||||
|
||||
url(r'new/', views.BuildCreate.as_view(), name='build-create'),
|
||||
|
||||
url(r'^(?P<pk>\d+)/', include(build_detail_urls)),
|
||||
|
||||
url(r'.*$', views.BuildIndex.as_view(), name='build-index'),
|
||||
|
@ -667,83 +667,6 @@ class BuildAllocate(InvenTreeRoleMixin, DetailView):
|
||||
return context
|
||||
|
||||
|
||||
class BuildCreate(AjaxCreateView):
|
||||
"""
|
||||
View to create a new Build object
|
||||
"""
|
||||
|
||||
model = Build
|
||||
context_object_name = 'build'
|
||||
form_class = forms.EditBuildForm
|
||||
ajax_form_title = _('New Build Order')
|
||||
ajax_template_name = 'modal_form.html'
|
||||
|
||||
def get_form(self):
|
||||
form = super().get_form()
|
||||
|
||||
if form['part'].value():
|
||||
form.fields['part'].widget = HiddenInput()
|
||||
|
||||
return form
|
||||
|
||||
def get_initial(self):
|
||||
""" Get initial parameters for Build creation.
|
||||
|
||||
If 'part' is specified in the GET query, initialize the Build with the specified Part
|
||||
"""
|
||||
|
||||
initials = super(BuildCreate, self).get_initial().copy()
|
||||
|
||||
initials['parent'] = self.request.GET.get('parent', None)
|
||||
|
||||
# User has provided a SalesOrder ID
|
||||
initials['sales_order'] = self.request.GET.get('sales_order', None)
|
||||
|
||||
initials['quantity'] = self.request.GET.get('quantity', 1)
|
||||
|
||||
part = self.request.GET.get('part', None)
|
||||
|
||||
if part:
|
||||
|
||||
try:
|
||||
part = Part.objects.get(pk=part)
|
||||
# User has provided a Part ID
|
||||
initials['part'] = part
|
||||
initials['destination'] = part.get_default_location()
|
||||
|
||||
to_order = part.quantity_to_order
|
||||
|
||||
if to_order < 1:
|
||||
to_order = 1
|
||||
|
||||
initials['quantity'] = to_order
|
||||
except (ValueError, Part.DoesNotExist):
|
||||
pass
|
||||
|
||||
initials['reference'] = Build.getNextBuildNumber()
|
||||
|
||||
# Pre-fill the issued_by user
|
||||
initials['issued_by'] = self.request.user
|
||||
|
||||
return initials
|
||||
|
||||
def get_data(self):
|
||||
return {
|
||||
'success': _('Created new build'),
|
||||
}
|
||||
|
||||
def validate(self, build, form, **kwargs):
|
||||
"""
|
||||
Perform extra form validation.
|
||||
|
||||
- If part is trackable, check that either batch or serial numbers are calculated
|
||||
|
||||
By this point form.is_valid() has been executed
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class BuildDelete(AjaxDeleteView):
|
||||
""" View to delete a build """
|
||||
|
||||
|
@ -43,8 +43,10 @@ def get_next_po_number():
|
||||
|
||||
attempts = set([order.reference])
|
||||
|
||||
reference = order.reference
|
||||
|
||||
while 1:
|
||||
reference = increment(order.reference)
|
||||
reference = increment(reference)
|
||||
|
||||
if reference in attempts:
|
||||
# Escape infinite recursion
|
||||
@ -70,8 +72,10 @@ def get_next_so_number():
|
||||
|
||||
attempts = set([order.reference])
|
||||
|
||||
reference = order.reference
|
||||
|
||||
while 1:
|
||||
reference = increment(order.reference)
|
||||
reference = increment(reference)
|
||||
|
||||
if reference in attempts:
|
||||
# Escape infinite recursion
|
||||
|
@ -34,9 +34,7 @@
|
||||
{{ block.super }}
|
||||
$("#start-build").click(function() {
|
||||
newBuildOrder({
|
||||
data: {
|
||||
part: {{ part.id }},
|
||||
}
|
||||
part: {{ part.pk }},
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -1,34 +1,59 @@
|
||||
{% load i18n %}
|
||||
{% load inventree_extras %}
|
||||
|
||||
|
||||
function buildFormFields() {
|
||||
return {
|
||||
reference: {
|
||||
prefix: "{% settings_value 'BUILDORDER_REFERENCE_PREFIX' %}",
|
||||
},
|
||||
title: {},
|
||||
part: {},
|
||||
quantity: {},
|
||||
batch: {},
|
||||
target_date: {},
|
||||
take_from: {},
|
||||
destination: {},
|
||||
link: {
|
||||
icon: 'fa-link',
|
||||
},
|
||||
issued_by: {
|
||||
icon: 'fa-user',
|
||||
},
|
||||
responsible: {
|
||||
icon: 'fa-users',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
function editBuildOrder(pk, options={}) {
|
||||
|
||||
var fields = buildFormFields();
|
||||
|
||||
constructForm(`/api/build/${pk}/`, {
|
||||
fields: fields,
|
||||
reload: true,
|
||||
title: '{% trans "Edit Build Order" %}',
|
||||
});
|
||||
}
|
||||
|
||||
function newBuildOrder(options={}) {
|
||||
/* Launch modal form to create a new BuildOrder.
|
||||
*/
|
||||
|
||||
launchModalForm(
|
||||
"{% url 'build-create' %}",
|
||||
{
|
||||
follow: true,
|
||||
data: options.data || {},
|
||||
callback: [
|
||||
{
|
||||
field: 'part',
|
||||
action: function(value) {
|
||||
inventreeGet(
|
||||
`/api/part/${value}/`, {},
|
||||
{
|
||||
success: function(response) {
|
||||
var fields = buildFormFields();
|
||||
|
||||
//enableField('serial_numbers', response.trackable);
|
||||
//clearField('serial_numbers');
|
||||
}
|
||||
}
|
||||
);
|
||||
},
|
||||
}
|
||||
],
|
||||
}
|
||||
)
|
||||
if (options.part) {
|
||||
fields.part.value = options.part;
|
||||
}
|
||||
|
||||
constructForm(`/api/build/`, {
|
||||
fields: fields,
|
||||
follow: true,
|
||||
method: 'POST',
|
||||
title: '{% trans "Create Build Order" %}'
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user