Refactor deletion of multiple manufacturer part objects

- issues multiple DELETE requests via the API
This commit is contained in:
Oliver 2021-07-01 17:01:30 +10:00
parent 206d7bd96a
commit 9bd71c1184
6 changed files with 74 additions and 61 deletions

View File

@ -25,7 +25,7 @@
{% endif %}
<div class='btn-group'>
<div class="dropdown" style="float: right;">
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">{% trans "Options" %}
<button class="btn btn-primary dropdown-toggle" id='table-options', type="button" data-toggle="dropdown">{% trans "Options" %}
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
@ -59,7 +59,9 @@
data: {
manufacturer: {{ company.id }},
},
reload: true,
success: function() {
$("#part-table").bootstrapTable("refresh");
},
secondary: [
{
field: 'part',
@ -88,22 +90,15 @@
}
);
linkButtonsToSelection($("#manufacturer-table"), ['#table-options']);
$("#multi-part-delete").click(function() {
var selections = $("#part-table").bootstrapTable("getSelections");
var parts = [];
selections.forEach(function(item) {
parts.push(item.pk);
});
var url = "{% url 'manufacturer-part-delete' %}"
launchModalForm(url, {
data: {
parts: parts,
},
reload: true,
deleteManufacturerParts(selections, {
onSuccess: function() {
$("#part-table").bootstrapTable("refresh");
}
});
});

View File

@ -258,34 +258,4 @@ class ManufacturerPartViewTests(CompanyViewTestBase):
(response, errors) = self.post(url, data, valid=True)
# Check that the ManufacturerPart was created!
self.assertEqual(n + 1, ManufacturerPart.objects.all().count())
def test_manufacturer_part_delete(self):
"""
Test the ManufacturerPartDelete view
"""
url = reverse('manufacturer-part-delete')
# Get form using 'part' argument
response = self.client.get(url, {'part': '2'}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(response.status_code, 200)
# POST to delete manufacturer part
n = ManufacturerPart.objects.count()
m = SupplierPart.objects.count()
response = self.client.post(
url,
{
'manufacturer-part-2': 'manufacturer-part-2',
'confirm_delete': True
},
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(response.status_code, 200)
# Check that the ManufacturerPart was deleted
self.assertEqual(n - 1, ManufacturerPart.objects.count())
# Check that the SupplierParts were deleted
self.assertEqual(m - 2, SupplierPart.objects.count())
self.assertEqual(n + 1, ManufacturerPart.objects.all().count())

View File

@ -123,8 +123,8 @@
});
// Wait for *all* the requests to complete
$.when.apply($, requests).then(function() {
$('#bom-table').bootstrapTable('refresh');
$.when(requests).then(function() {
location.reload();
});
}
}

View File

@ -60,17 +60,10 @@
var selections = $("#manufacturer-table").bootstrapTable("getSelections");
var parts = [];
selections.forEach(function(item) {
parts.push(item.pk);
});
launchModalForm("{% url 'manufacturer-part-delete' %}", {
data: {
parts: parts,
},
reload: true,
deleteManufacturerParts(selections, {
onSuccess: function() {
$("#manufacturer-table").bootstrapTable("refresh");
}
});
});
@ -80,7 +73,7 @@
{
params: {
part: {{ part.id }},
part_detail: false,
part_detail: true,
manufacturer_detail: true,
},
}

View File

@ -170,6 +170,61 @@ function loadCompanyTable(table, url, options={}) {
}
function deleteManufacturerParts(selections, options={}) {
if (selections.length == 0) {
return;
}
var parts = [];
var text = `
<div class='alert alert-block alert-danger'>
<p>{% trans "The following manufacturer parts will be deleted" %}:</p>
<ul>`;
selections.forEach(function(item) {
parts.push(item.pk);
text += `
<li>
<p>${item.MPN} - ${item.part_detail.full_name}</p>
</li>`;
});
text += `
</ul>
</div>`;
showQuestionDialog(
'{% trans "Delete Manufacturer Parts" %}',
text,
{
accept_text: '{% trans "Delete" %}',
accept: function() {
// 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(requests).then(function() {
if (options.onSuccess) {
options.onSuccess();
}
})
}
}
);
}
function loadManufacturerPartTable(table, url, options) {
/*
* Load manufacturer part table

View File

@ -906,7 +906,7 @@ function loadStockTable(table, options) {
);
});
$.when.apply($, requests).then(function() {
$.when(requests).then(function() {
$("#stock-table").bootstrapTable('refresh');
});
})