2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-08-14 07:31:10 +00:00

Refactoring API endpoints

- Improved URL naming scheme
This commit is contained in:
Oliver
2022-02-16 16:57:27 +11:00
parent d7adb6959d
commit f399f4fa34
8 changed files with 295 additions and 274 deletions

View File

@@ -77,15 +77,10 @@ $('#bom-template-download').click(function() {
$('#bom-upload').click(function() {
constructForm('{% url "api-bom-extract" %}', {
constructForm('{% url "api-bom-import-upload" %}', {
method: 'POST',
fields: {
bom_file: {},
part: {
value: {{ part.pk }},
hidden: true,
},
clear_existing: {},
data_file: {},
},
title: '{% trans "Upload BOM File" %}',
onSuccess: function(response) {
@@ -96,12 +91,92 @@ $('#bom-upload').click(function() {
// Disable the "submit" button
$('#bom-submit').show();
constructBomUploadTable(response);
var fields = {};
$('#bom-submit').click(function() {
submitBomTable({{ part.pk }}, {
bom_data: response,
var choices = [];
// Add an "empty" value
choices.push({
value: '',
display_name: '-----',
});
for (const [name, field] of Object.entries(response.model_fields)) {
choices.push({
value: name,
display_name: field.label || name,
});
}
var field_names = Object.keys(response.file_fields);
for (var idx = 0; idx < field_names.length; idx++) {
var field_name = field_names[idx];
// Construct a new field
fields[`column_${idx}`] = {
type: 'choice',
label: field_name,
value: response.file_fields[field_name].value,
choices: choices,
inline: true,
};
}
constructForm('{% url "api-bom-import-extract" %}', {
method: 'POST',
title: '{% trans "Select BOM Columns" %}',
fields: fields,
onSubmit: function(fields, opts) {
var columns = [];
for (var idx = 0; idx < field_names.length; idx++) {
columns.push(
getFormFieldValue(`column_${idx}`, {}, {})
);
}
$(opts.modal).find('#modal-progress-spinner').show();
inventreePut(
opts.url,
{
columns: columns,
rows: response.rows,
},
{
method: 'POST',
success: function(r) {
handleFormSuccess(r, opts);
constructBomUploadTable(r);
$('#bom-submit').click(function() {
submitBomTable({{ part.pk }}, {
bom_data: response,
});
});
},
error: function(xhr) {
$(opts.modal).find('#modal-progress-spinner').hide();
switch (xhr.status) {
case 400:
handleFormErrors(xhr.responseJSON, fields, opts);
break;
default:
$(opts.modal).modal('hide');
console.log(`upload error at ${opts.url}`);
showApiError(xhr, opts.url);
break;
}
}
}
);
},
});
}
});