diff --git a/InvenTree/InvenTree/views.py b/InvenTree/InvenTree/views.py index 4ea1c09b97..d35284135c 100644 --- a/InvenTree/InvenTree/views.py +++ b/InvenTree/InvenTree/views.py @@ -101,3 +101,40 @@ class AjaxUpdateView(AjaxView, UpdateView): else: return response + + +class AjaxDeleteView(AjaxView, DeleteView): + + def post(self, request, *args, **kwargs): + + if request.is_ajax(): + obj = self.get_object() + pk = obj.id + obj.delete() + + data = {'id': pk, + 'delete': True} + + return JsonResponse(data) + + else: + return super(DeleteView, self).post(request, *args, **kwargs) + + def get(self, request, *args, **kwargs): + + response = super(DeleteView, self).get(request, *args, **kwargs) + + if request.is_ajax(): + + data = {'id': self.get_object().id, + 'title': self.ajax_form_title, + 'delete': False, + 'html_data': render_to_string(self.getAjaxTemplate(), + self.get_context_data(), + request=request) + } + + return JsonResponse(data) + + else: + return response diff --git a/InvenTree/part/templates/part/delete.html b/InvenTree/part/templates/part/delete.html index 5e536508d9..9e38b07be4 100644 --- a/InvenTree/part/templates/part/delete.html +++ b/InvenTree/part/templates/part/delete.html @@ -7,37 +7,6 @@ {% block del_body %} - {% if part.used_in_count %} -
This part is used in BOMs for {{ part.used_in_count }} other parts. If you delete this part, the BOMs for the following parts will be updated: -
There are {{ part.locations.all|length }} stock entries defined for this part. If you delete this part, the following stock entries will also be deleted: -
There are {{ part.supplier_parts.all|length }} suppliers defined for this part. If you delete this part, the following supplier parts will also be deleted. -
There are {{ part.serials.all|length }} unique parts tracked for '{{ part.name }}'. Deleting this part will permanently remove this tracking information.
- {% endif %} + {$ include 'part/partial_delete' with part=part %} {% endblock %} \ No newline at end of file diff --git a/InvenTree/part/templates/part/detail.html b/InvenTree/part/templates/part/detail.html index cc567c97c3..687f241b54 100644 --- a/InvenTree/part/templates/part/detail.html +++ b/InvenTree/part/templates/part/detail.html @@ -88,6 +88,7 @@ {% endblock %} + {% block javascript %} @@ -97,6 +98,13 @@ $(document).ready(function () { $("#edit-part").click(function() { launchModalForm("#modal-form", "{% url 'part-edit' part.id %}"); }); + + $('#delete-part').click(function() { + launchDeleteForm("#modal-delete", + "{% url 'part-delete' part.id %}", + {redirect: "{% url 'part-index' %}"} + ); + }); }); diff --git a/InvenTree/part/templates/part/partial_delete.html b/InvenTree/part/templates/part/partial_delete.html new file mode 100644 index 0000000000..150b986e43 --- /dev/null +++ b/InvenTree/part/templates/part/partial_delete.html @@ -0,0 +1,32 @@ +{% if part.used_in_count %} +This part is used in BOMs for {{ part.used_in_count }} other parts. If you delete this part, the BOMs for the following parts will be updated: +
There are {{ part.locations.all|length }} stock entries defined for this part. If you delete this part, the following stock entries will also be deleted: +
There are {{ part.supplier_parts.all|length }} suppliers defined for this part. If you delete this part, the following supplier parts will also be deleted. +
There are {{ part.serials.all|length }} unique parts tracked for '{{ part.name }}'. Deleting this part will permanently remove this tracking information.
+{% endif %} \ No newline at end of file diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index 159714d838..debecdf10c 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -19,7 +19,7 @@ from .forms import EditBomItemForm from .forms import EditSupplierPartForm -from InvenTree.views import AjaxCreateView, AjaxUpdateView +from InvenTree.views import AjaxCreateView, AjaxUpdateView, AjaxDeleteView class PartIndex(ListView): model = Part @@ -92,17 +92,21 @@ class PartEdit(AjaxUpdateView): ajax_form_title = 'Edit Part Properties' -class PartDelete(DeleteView): +class PartDelete(AjaxDeleteView): model = Part template_name = 'part/delete.html' + ajax_template_name = 'part/partial_delete.html' + ajax_form_title = 'Confirm Part Deletion' success_url = '/part/' + """ def post(self, request, *args, **kwargs): if 'confirm' in request.POST: return super(PartDelete, self).post(request, *args, **kwargs) else: return HttpResponseRedirect(self.get_object().get_absolute_url()) + """ class CategoryDetail(DetailView): diff --git a/InvenTree/static/script/modal_form.js b/InvenTree/static/script/modal_form.js index 1723bc7868..2107c5038a 100644 --- a/InvenTree/static/script/modal_form.js +++ b/InvenTree/static/script/modal_form.js @@ -8,7 +8,61 @@ function attachSelect(modal) { }); } -function launchModalForm(modal, url, data) { +function launchDeleteForm(modal, url, options) { + $(modal).on('shown.bs.modal', function() { + $(modal + ' .modal-form-content').scrollTop(0); + }); + + $.ajax({ + url: url, + type: 'get', + dataType: 'json', + beforeSend: function() { + $(modal).modal('show'); + }, + success: function (response) { + if (response.title) { + $(modal + ' #modal-title').html(response.title); + } + if (response.html_data) { + $(modal + ' .modal-form-content').html(response.html_data); + } + else { + alert('JSON response missing HTML data'); + $(modal).modal('hide'); + } + }, + error: function (xhr, ajaxOptions, thrownError) { + alert('Error requesting JSON data:\n' + thrownError); + $(modal).modal('hide'); + } + }); + + $(modal).on('click', '#modal-form-delete', function() { + + var form = $(modal).find('#delete-form'); + + $.ajax({ + url: url, + data: form.serialize(), + type: 'post', + dataType: 'json', + success: function (response) { + $(modal).modal('hide'); + + if (options.redirect) { + window.location.href = options.redirect; + } + }, + error: function (xhr, ajaxOptions, thrownError) { + alert('Error deleting item:\n' + thrownError); + $(modal).modal('hide'); + } + }); + }); +} + +function launchModalForm(modal, url, data, options) { $(modal).on('shown.bs.modal', function () { $(modal + ' .modal-form-content').scrollTop(0); diff --git a/InvenTree/templates/modal.html b/InvenTree/templates/modal.html index 5c270c8f61..451ee03bd0 100644 --- a/InvenTree/templates/modal.html +++ b/InvenTree/templates/modal.html @@ -5,7 +5,7 @@ - +