Added AjaxDeleteView

- AjaxDeleteView handles ajaxified delete GET and POST events
- Added modal deletion window
- Added launchDeleteForm jQuery function
This commit is contained in:
Oliver 2018-04-27 00:06:44 +10:00
parent 9fd275ed3e
commit 784b0dec02
8 changed files with 161 additions and 36 deletions

View File

@ -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

View File

@ -7,37 +7,6 @@
{% block del_body %}
{% if part.used_in_count %}
<p>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:
<ul class="list-group">
{% for child in part.used_in.all %}
<li class='list-group-item'>{{ child.part.name }} - {{ child.part.description }}</li>
{% endfor %}
</p>
{% endif %}
{% if part.locations.all|length > 0 %}
<p>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:
<ul class='list-group'>
{% for stock in part.locations.all %}
<li class='list-group-item'>{{ stock.location.name }} - {{ stock.quantity }} items</li>
{% endfor %}
</ul>
</p>
{% endif %}
{% if part.supplier_parts.all|length > 0 %}
<p>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.
<ul class='list-group'>
{% for spart in part.supplier_parts.all %}
<li class='list-group-item'>{{ spart.supplier.name }} - {{ spart.SKU }}</li>
{% endfor %}
</ul>
</p>
{% endif %}
{% if part.serials.all|length > 0 %}
<p>There are {{ part.serials.all|length }} unique parts tracked for '{{ part.name }}'. Deleting this part will permanently remove this tracking information.</p>
{% endif %}
{$ include 'part/partial_delete' with part=part %}
{% endblock %}

View File

@ -88,6 +88,7 @@
{% endblock %}
{% block javascript %}
<script type='text/javascript' src="{% static 'script/modal_form.js' %}"></script>
@ -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' %}"}
);
});
});
</script>

View File

@ -0,0 +1,32 @@
{% if part.used_in_count %}
<p>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:
<ul class="list-group">
{% for child in part.used_in.all %}
<li class='list-group-item'>{{ child.part.name }} - {{ child.part.description }}</li>
{% endfor %}
</p>
{% endif %}
{% if part.locations.all|length > 0 %}
<p>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:
<ul class='list-group'>
{% for stock in part.locations.all %}
<li class='list-group-item'>{{ stock.location.name }} - {{ stock.quantity }} items</li>
{% endfor %}
</ul>
</p>
{% endif %}
{% if part.supplier_parts.all|length > 0 %}
<p>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.
<ul class='list-group'>
{% for spart in part.supplier_parts.all %}
<li class='list-group-item'>{{ spart.supplier.name }} - {{ spart.SKU }}</li>
{% endfor %}
</ul>
</p>
{% endif %}
{% if part.serials.all|length > 0 %}
<p>There are {{ part.serials.all|length }} unique parts tracked for '{{ part.name }}'. Deleting this part will permanently remove this tracking information.</p>
{% endif %}

View File

@ -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):

View File

@ -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);

View File

@ -5,7 +5,7 @@
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<h3 id='modal-title'></h3>
<h3 id='modal-title'>Form Title Here</h3>
</div>
<div class='modal-form-content'>
</div>

View File

@ -0,0 +1,21 @@
<div class='modal fade modal-fixed-footer' tabindex='-1' role='dialog' id='modal-delete'>
<div class='modal-dialog'>
<div class='modal-content'>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<h3 id='modal-title'>Confirm Item Deletion</h3>
</div>
<form action='' method='post' id='delete-form'>
{% csrf_token %}
</form>
<div class='modal-form-content'>
</div>
<div class='modal-footer'>
<button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
<button type='button' class='btn btn-danger' id='modal-form-delete'>Delete</button>
</div>
</div>
</div>
</div>