2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-18 04:55:44 +00:00

Refactor 'DeleteManufacturerPart' form (#3067)

* Refactor 'DeleteManufacturerPart' form

- Remove duplicated forms
- Update style to match other forms
- Block on each deletion before progressing to the next one

* PEP fix

* Adds deleteSupplierParts function

* Refactor all instances of supplier part deletion

* Refactor tractor : use convenience function for bootstraptable.getSelections

* Add deleter for manufacturerpartparameter.

Refactor existing code into a single function

* Refactor deletion for stock items

* JS linting
This commit is contained in:
Oliver
2022-05-25 23:35:53 +10:00
committed by GitHub
parent 0e9ea1be0c
commit 2d1776a151
19 changed files with 371 additions and 291 deletions

View File

@ -7,6 +7,7 @@
/* exported
inventreeGet,
inventreeDelete,
inventreeMultiDelete,
inventreeFormDataUpload,
showApiError,
*/
@ -171,6 +172,50 @@ function inventreeDelete(url, options={}) {
return inventreePut(url, {}, options);
}
/*
* Perform a 'multi delete' operation:
*
* - Items are deleted sequentially from the database, rather than simultaneous requests
* - This prevents potential overload / transaction issues in the DB backend
*
* Notes:
* - Assumes that each item in the 'items' list has a parameter 'pk'
*/
function inventreeMultiDelete(url, items, options={}) {
if (!url.endsWith('/')) {
url += '/';
}
function doNextDelete() {
if (items.length > 0) {
var item = items.shift();
inventreeDelete(`${url}${item.pk}/`, {
complete: doNextDelete
});
} else {
if (options.modal) {
$(options.modal).modal('hide');
}
if (options.success) {
options.success();
}
}
}
if (options.modal) {
showModalSpinner(options.modal);
}
// Initiate the process
doNextDelete();
}
/*
* Display a notification with error information
*/

View File

@ -693,41 +693,20 @@ function deleteBomItems(items, options={}) {
`;
constructFormBody({}, {
method: 'DELETE',
title: '{% trans "Delete selected BOM items?" %}',
fields: {},
preFormContent: html,
submitText: '{% trans "Delete" %}',
submitClass: 'danger',
confirm: true,
onSubmit: function(fields, opts) {
// Individually send DELETE requests for each BOM item
// We do *not* send these all at once, to prevent overloading the server
// Show the progress spinner
$(opts.modal).find('#modal-progress-spinner').show();
function deleteNextBomItem() {
if (items.length > 0) {
var item = items.shift();
inventreeDelete(`/api/bom/${item.pk}/`,
{
complete: deleteNextBomItem,
}
);
} else {
// Destroy this modal once all items are deleted
$(opts.modal).modal('hide');
if (options.success) {
options.success();
}
inventreeMultiDelete(
'{% url "api-bom-list" %}',
items,
{
modal: opts.modal,
success: options.success,
}
}
deleteNextBomItem();
);
},
});
}

View File

@ -834,12 +834,7 @@ function loadBuildOutputTable(build_info, options={}) {
var subtable = $(`#output-sub-table-${pk}`);
if (subtable.exists()) {
var rows = subtable.bootstrapTable('getSelections');
// None selected? Use all!
if (rows.length == 0) {
rows = subtable.bootstrapTable('getData');
}
var rows = getTableData(`#output-sub-table-${pk}`);
allocateStockToBuild(
build_info.pk,
@ -1291,11 +1286,7 @@ function loadBuildOutputTable(build_info, options={}) {
// Complete multiple outputs
$('#multi-output-complete').click(function() {
var outputs = $(table).bootstrapTable('getSelections');
if (outputs.length == 0) {
outputs = $(table).bootstrapTable('getData');
}
var outputs = getTableData(table);
completeBuildOutputs(
build_info.pk,
@ -1314,11 +1305,7 @@ function loadBuildOutputTable(build_info, options={}) {
// Delete multiple build outputs
$('#multi-output-delete').click(function() {
var outputs = $(table).bootstrapTable('getSelections');
if (outputs.length == 0) {
outputs = $(table).bootstrapTable('getData');
}
var outputs = getTableData(table);
deleteBuildOutputs(
build_info.pk,
@ -1337,11 +1324,7 @@ function loadBuildOutputTable(build_info, options={}) {
// Print stock item labels
$('#incomplete-output-print-label').click(function() {
var outputs = $(table).bootstrapTable('getSelections');
if (outputs.length == 0) {
outputs = $(table).bootstrapTable('getData');
}
var outputs = getTableData(table);
var stock_id_values = [];

View File

@ -3,12 +3,11 @@
/* globals
constructForm,
imageHoverIcon,
inventreeDelete,
inventreeMultiDelete,
loadTableFilters,
makeIconButton,
renderLink,
setupFilterList,
showQuestionDialog,
*/
/* exported
@ -16,6 +15,8 @@
createManufacturerPart,
createSupplierPart,
deleteManufacturerParts,
deleteManufacturerPartParameters,
deleteSupplierParts,
editCompany,
loadCompanyTable,
loadManufacturerPartTable,
@ -101,15 +102,6 @@ function editManufacturerPart(part, options={}) {
});
}
function deleteManufacturerPart(part, options={}) {
constructForm(`/api/company/part/manufacturer/${part}/`, {
method: 'DELETE',
title: '{% trans "Delete Manufacturer Part" %}',
onSuccess: options.onSuccess,
});
}
function supplierPartFields() {
@ -211,12 +203,76 @@ function editSupplierPart(part, options={}) {
}
function deleteSupplierPart(part, options={}) {
/*
* Delete one or more SupplierPart objects from the database.
* - User will be provided with a modal form, showing all the parts to be deleted.
* - Delete operations are performed sequentialy, not simultaneously
*/
function deleteSupplierParts(parts, options={}) {
constructForm(`/api/company/part/${part}/`, {
if (parts.length == 0) {
return;
}
function renderPart(sup_part) {
var part = sup_part.part_detail;
var thumb = thumbnailImage(part.thumbnail || part.image);
var supplier = '-';
var MPN = '-';
if (sup_part.supplier_detail) {
supplier = sup_part.supplier_detail.name;
}
if (sup_part.manufacturer_part_detail) {
MPN = sup_part.manufacturer_part_detail.MPN;
}
return `
<tr>
<td>${thumb} ${part.full_name}</td>
<td>${sup_part.SKU}</td>
<td>${supplier}</td>
<td>${MPN}</td>
</tr>`;
}
var rows = '';
parts.forEach(function(sup_part) {
rows += renderPart(sup_part);
});
var html = `
<div class='alert alert-block alert-danger'>
{% trans "All selected supplier parts will be deleted" %}
</div>
<table class='table table-striped table-condensed'>
<tr>
<th>{% trans "Part" %}</th>
<th>{% trans "SKU" %}</th>
<th>{% trans "Supplier" %}</th>
<th>{% trans "MPN" %}</th>
</tr>
${rows}
</table>
`;
constructFormBody({}, {
method: 'DELETE',
title: '{% trans "Delete Supplier Part" %}',
onSuccess: options.onSuccess,
title: '{% trans "Delete Supplier Parts" %}',
preFormContent: html,
onSubmit: function(fields, opts) {
inventreeMultiDelete(
'{% url "api-supplier-part-list" %}',
parts,
{
modal: opts.modal,
success: options.success
}
);
}
});
}
@ -393,58 +449,116 @@ function loadCompanyTable(table, url, options={}) {
}
/* Delete one or more ManufacturerPart objects from the database.
* - User will be provided with a modal form, showing all the parts to be deleted.
* - Delete operations are performed sequentialy, not simultaneously
*/
function deleteManufacturerParts(selections, options={}) {
if (selections.length == 0) {
return;
}
var parts = [];
function renderPart(man_part, opts={}) {
var part = man_part.part_detail;
var thumb = thumbnailImage(part.thumbnail || part.image);
var text = `
<div class='alert alert-block alert-danger'>
<p>{% trans "The following manufacturer parts will be deleted" %}:</p>
<ul>`;
return `
<tr>
<td>${thumb} ${part.full_name}</td>
<td>${man_part.MPN}</td>
<td>${man_part.manufacturer_detail.name}</td>
</tr>`;
}
selections.forEach(function(item) {
parts.push(item.pk);
var rows = '';
text += `
<li>
<p>${item.MPN} - ${item.part_detail.full_name}</p>
</li>`;
selections.forEach(function(man_part) {
rows += renderPart(man_part);
});
text += `
</ul>
</div>`;
var html = `
<div class='alert alert-block alert-danger'>
{% trans "All selected manufacturer parts will be deleted" %}
</div>
<table class='table table-striped table-condensed'>
<tr>
<th>{% trans "Part" %}</th>
<th>{% trans "MPN" %}</th>
<th>{% trans "Manufacturer" %}</th>
</tr>
${rows}
</table>
`;
showQuestionDialog(
'{% trans "Delete Manufacturer Parts" %}',
text,
{
accept_text: '{% trans "Delete" %}',
accept: function() {
constructFormBody({}, {
method: 'DELETE',
title: '{% trans "Delete Manufacturer Parts" %}',
preFormContent: html,
onSubmit: function(fields, opts) {
// Delete each manufacturer part
var requests = [];
parts.forEach(function(pk) {
var url = `/api/company/part/manufacturer/${pk}`;
requests.push(inventreeDelete(url));
});
// Wait for all the requests to complete
$.when.apply($, requests).done(function() {
if (options.onSuccess) {
options.onSuccess();
}
});
}
inventreeMultiDelete(
'{% url "api-manufacturer-part-list" %}',
selections,
{
modal: opts.modal,
success: options.success,
}
);
}
);
});
}
function deleteManufacturerPartParameters(selections, options={}) {
if (selections.length == 0) {
return;
}
function renderParam(param) {
return `
<tr>
<td>${param.name}</td>
<td>${param.units}</td>
</tr>`;
}
var rows = '';
selections.forEach(function(param) {
rows += renderParam(param);
});
var html = `
<div class='alert alert-block alert-danger'>
{% trans "All selected parameters will be deleted" %}
</div>
<table class='table table-striped table-condensed'>
<tr>
<th>{% trans "Name" %}</th>
<th>{% trans "Value" %}</th>
</tr>
${rows}
</table>
`;
constructFormBody({}, {
method: 'DELETE',
title: '{% trans "Delete Parameters" %}',
preFormContent: html,
onSubmit: function(fields, opts) {
inventreeMultiDelete(
'{% url "api-manufacturer-part-parameter-list" %}',
selections,
{
modal: opts.modal,
success: options.success,
}
);
}
});
}
@ -473,6 +587,7 @@ function loadManufacturerPartTable(table, url, options) {
method: 'get',
original: params,
queryParams: filters,
uniqueId: 'pk',
sidePagination: 'server',
name: 'manufacturerparts',
groupBy: false,
@ -588,11 +703,12 @@ function loadManufacturerPartTable(table, url, options) {
$(table).find('.button-manufacturer-part-delete').click(function() {
var pk = $(this).attr('pk');
var row = $(table).bootstrapTable('getRowByUniqueId', pk);
deleteManufacturerPart(
pk,
deleteManufacturerParts(
[row],
{
onSuccess: function() {
success: function() {
$(table).bootstrapTable('refresh');
}
}
@ -618,7 +734,7 @@ function loadManufacturerPartParameterTable(table, url, options) {
filters[key] = params[key];
}
// setupFilterList("manufacturer-part-parameters", $(table));
setupFilterList('manufacturer-part-parameters', $(table));
$(table).inventreeTable({
url: url,
@ -730,6 +846,7 @@ function loadSupplierPartTable(table, url, options) {
method: 'get',
original: params,
sidePagination: 'server',
uniqueId: 'pk',
queryParams: filters,
name: 'supplierparts',
groupBy: false,
@ -886,11 +1003,12 @@ function loadSupplierPartTable(table, url, options) {
$(table).find('.button-supplier-part-delete').click(function() {
var pk = $(this).attr('pk');
var row = $(table).bootstrapTable('getRowByUniqueId', pk);
deleteSupplierPart(
pk,
deleteSupplierParts(
[row],
{
onSuccess: function() {
success: function() {
$(table).bootstrapTable('refresh');
}
}

View File

@ -247,11 +247,6 @@ function constructChangeForm(fields, options) {
*/
function constructDeleteForm(fields, options) {
// Force the "confirm" property if not set
if (!('confirm' in options)) {
options.confirm = true;
}
// Request existing data from the API endpoint
// This data can be used to render some information on the form
$.ajax({
@ -430,6 +425,21 @@ function constructFormBody(fields, options) {
// otherwise *all* fields will be displayed
var displayed_fields = options.fields || fields;
// Override default option values if a 'DELETE' form is specified
if (options.method == 'DELETE') {
if (!('confirm' in options)) {
options.confirm = true;
}
if (!('submitClass' in options)) {
options.submitClass = 'danger';
}
if (!('submitText' in options)) {
options.submitText = '{% trans "Delete" %}';
}
}
// Handle initial data overrides
if (options.data) {
for (const field in options.data) {
@ -797,7 +807,7 @@ function submitFormData(fields, options) {
}
// Show the progress spinner
$(options.modal).find('#modal-progress-spinner').show();
showModalSpinner(options.modal);
// Submit data
upload_func(
@ -2625,7 +2635,7 @@ function selectImportFields(url, data={}, options={}) {
columns.push(getFormFieldValue(`column_${idx}`, {}, opts));
}
$(opts.modal).find('#modal-progress-spinner').show();
showModalSpinner(opts.modal);
inventreePut(
opts.url,

View File

@ -16,6 +16,7 @@
showModalImage,
removeRowFromModalForm,
showQuestionDialog,
showModalSpinner,
*/
/*
@ -1146,3 +1147,13 @@ function showModalImage(image_url) {
hideModalImage();
});
}
/* Show (or hide) a progress spinner icon in the dialog */
function showModalSpinner(modal, show=true) {
if (show) {
$(modal).find('#modal-progress-spinner').show();
} else {
$(modal).find('#modal-progress-spinner').hide();
}
}

View File

@ -1560,7 +1560,7 @@ function loadPartTable(table, url, options={}) {
/* Button callbacks for part table buttons */
$('#multi-part-order').click(function() {
var selections = $(table).bootstrapTable('getSelections');
var selections = getTableData(table);
var parts = [];
@ -1594,7 +1594,7 @@ function loadPartTable(table, url, options={}) {
});
$('#multi-part-print-label').click(function() {
var selections = $(table).bootstrapTable('getSelections');
var selections = getTableData(table);
var items = [];

View File

@ -11,8 +11,8 @@
global_settings,
handleFormErrors,
imageHoverIcon,
inventreeDelete,
inventreeGet,
inventreeMultiDelete,
inventreePut,
launchModalForm,
linkButtonsToSelection,
@ -1106,25 +1106,15 @@ function adjustStock(action, items, options={}) {
// Delete action is handled differently
if (action == 'delete') {
var requests = [];
item_pk_values.forEach(function(pk) {
requests.push(
inventreeDelete(
`/api/stock/${pk}/`,
)
);
});
// Wait for *all* the requests to complete
$.when.apply($, requests).done(function() {
// Destroy the modal window
$(opts.modal).modal('hide');
if (options.success) {
options.success();
inventreeMultiDelete(
'{% url "api-stock-list" %}',
items,
{
modal: opts.modal,
success: options.success,
}
});
);
return;
}
@ -1955,7 +1945,7 @@ function loadStockTable(table, options) {
);
function stockAdjustment(action) {
var items = $(table).bootstrapTable('getSelections');
var items = getTableData(table);
adjustStock(action, items, {
success: function() {
@ -1967,7 +1957,7 @@ function loadStockTable(table, options) {
// Automatically link button callbacks
$('#multi-item-print-label').click(function() {
var selections = $(table).bootstrapTable('getSelections');
var selections = getTableData(table);
var items = [];
@ -1979,7 +1969,7 @@ function loadStockTable(table, options) {
});
$('#multi-item-print-test-report').click(function() {
var selections = $(table).bootstrapTable('getSelections');
var selections = getTableData(table);
var items = [];
@ -1992,7 +1982,7 @@ function loadStockTable(table, options) {
if (global_settings.BARCODE_ENABLE) {
$('#multi-item-barcode-scan-into-location').click(function() {
var selections = $(table).bootstrapTable('getSelections');
var selections = getTableData(table);
var items = [];
@ -2021,7 +2011,7 @@ function loadStockTable(table, options) {
});
$('#multi-item-merge').click(function() {
var items = $(table).bootstrapTable('getSelections');
var items = getTableData(table);
mergeStockItems(items, {
success: function(response) {
@ -2036,7 +2026,7 @@ function loadStockTable(table, options) {
$('#multi-item-assign').click(function() {
var items = $(table).bootstrapTable('getSelections');
var items = getTableData(table);
assignStockToCustomer(items, {
success: function() {
@ -2046,7 +2036,8 @@ function loadStockTable(table, options) {
});
$('#multi-item-order').click(function() {
var selections = $(table).bootstrapTable('getSelections');
var selections = getTableData(table);
var parts = [];
@ -2063,7 +2054,7 @@ function loadStockTable(table, options) {
$('#multi-item-set-status').click(function() {
// Select and set the STATUS field for selected stock items
var selections = $(table).bootstrapTable('getSelections');
var selections = getTableData(table);
// Select stock status
var modal = '#modal-form';
@ -2149,7 +2140,7 @@ function loadStockTable(table, options) {
});
$('#multi-item-delete').click(function() {
var selections = $(table).bootstrapTable('getSelections');
var selections = getTableData(table);
var stock = [];

View File

@ -8,6 +8,7 @@
/* exported
customGroupSorter,
downloadTableData,
getTableData,
reloadtable,
renderLink,
reloadTableFilters,
@ -22,6 +23,23 @@ function reloadtable(table) {
}
/*
* Return the 'selected' data rows from a bootstrap table.
* If allowEmpty = false, and the returned dataset is empty,
* then instead try to return *all* the data
*/
function getTableData(table, allowEmpty=false) {
var data = $(table).bootstrapTable('getSelections');
if (data.length == 0 && !allowEmpty) {
data = $(table).bootstrapTable('getData');
}
return data;
}
/**
* Download data from a table, via the API.
* This requires a number of conditions to be met: