mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Merge pull request #319 from SchrodingersGat/confirm-delete
Add a generic DeleteForm and update AjaxDeleteView
This commit is contained in:
commit
5a81372ead
@ -17,3 +17,19 @@ class HelperForm(forms.ModelForm):
|
|||||||
self.helper = FormHelper()
|
self.helper = FormHelper()
|
||||||
|
|
||||||
self.helper.form_tag = False
|
self.helper.form_tag = False
|
||||||
|
|
||||||
|
|
||||||
|
class DeleteForm(forms.Form):
|
||||||
|
""" Generic deletion form which provides simple user confirmation
|
||||||
|
"""
|
||||||
|
|
||||||
|
confirm_delete = forms.BooleanField(
|
||||||
|
required=False,
|
||||||
|
initial=False,
|
||||||
|
help_text='Confirm item deletion'
|
||||||
|
)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
fields = [
|
||||||
|
'confirm_delete'
|
||||||
|
]
|
||||||
|
@ -12,11 +12,14 @@ from django.template.loader import render_to_string
|
|||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
|
|
||||||
from django.views import View
|
from django.views import View
|
||||||
from django.views.generic import UpdateView, CreateView, DeleteView
|
from django.views.generic import UpdateView, CreateView
|
||||||
from django.views.generic.base import TemplateView
|
from django.views.generic.base import TemplateView
|
||||||
|
|
||||||
from part.models import Part
|
from part.models import Part
|
||||||
|
|
||||||
|
from .forms import DeleteForm
|
||||||
|
from .helpers import str2bool
|
||||||
|
|
||||||
from rest_framework import views
|
from rest_framework import views
|
||||||
|
|
||||||
|
|
||||||
@ -300,13 +303,28 @@ class AjaxUpdateView(AjaxMixin, UpdateView):
|
|||||||
return self.renderJsonResponse(request, form, data)
|
return self.renderJsonResponse(request, form, data)
|
||||||
|
|
||||||
|
|
||||||
class AjaxDeleteView(AjaxMixin, DeleteView):
|
class AjaxDeleteView(AjaxMixin, UpdateView):
|
||||||
|
|
||||||
""" An 'AJAXified DeleteView for removing an object from the DB
|
""" An 'AJAXified DeleteView for removing an object from the DB
|
||||||
- Returns a HTML object (not a form!) in JSON format (for delivery to a modal window)
|
- Returns a HTML object (not a form!) in JSON format (for delivery to a modal window)
|
||||||
- Handles deletion
|
- Handles deletion
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
form_class = DeleteForm
|
||||||
|
ajax_form_title = "Delete Item"
|
||||||
|
ajax_template_name = "modal_delete_form.html"
|
||||||
|
context_object_name = 'item'
|
||||||
|
|
||||||
|
def get_object(self):
|
||||||
|
try:
|
||||||
|
self.object = self.model.objects.get(pk=self.kwargs['pk'])
|
||||||
|
except:
|
||||||
|
return None
|
||||||
|
return self.object
|
||||||
|
|
||||||
|
def get_form(self):
|
||||||
|
return self.form_class(self.get_form_kwargs())
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
""" Respond to GET request
|
""" Respond to GET request
|
||||||
|
|
||||||
@ -314,19 +332,15 @@ class AjaxDeleteView(AjaxMixin, DeleteView):
|
|||||||
- Return rendered form to client
|
- Return rendered form to client
|
||||||
"""
|
"""
|
||||||
|
|
||||||
super(DeleteView, self).get(request, *args, **kwargs)
|
super(UpdateView, self).get(request, *args, **kwargs)
|
||||||
|
|
||||||
data = {
|
form = self.get_form()
|
||||||
'id': self.get_object().id,
|
|
||||||
'delete': False,
|
|
||||||
'title': self.ajax_form_title,
|
|
||||||
'html_data': render_to_string(
|
|
||||||
self.ajax_template_name,
|
|
||||||
self.get_context_data(),
|
|
||||||
request=request)
|
|
||||||
}
|
|
||||||
|
|
||||||
return JsonResponse(data)
|
context = self.get_context_data()
|
||||||
|
|
||||||
|
context[self.context_object_name] = self.get_object()
|
||||||
|
|
||||||
|
return self.renderJsonResponse(request, form, context=context)
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
""" Respond to POST request
|
""" Respond to POST request
|
||||||
@ -337,14 +351,24 @@ class AjaxDeleteView(AjaxMixin, DeleteView):
|
|||||||
|
|
||||||
obj = self.get_object()
|
obj = self.get_object()
|
||||||
pk = obj.id
|
pk = obj.id
|
||||||
|
|
||||||
|
form = self.get_form()
|
||||||
|
|
||||||
|
confirmed = str2bool(request.POST.get('confirm_delete', False))
|
||||||
|
context = self.get_context_data()
|
||||||
|
|
||||||
|
if confirmed:
|
||||||
obj.delete()
|
obj.delete()
|
||||||
|
else:
|
||||||
|
form.errors['confirm_delete'] = ['Check box to confirm item deletion']
|
||||||
|
context[self.context_object_name] = self.get_object()
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
'id': pk,
|
'id': pk,
|
||||||
'delete': True
|
'form_valid': confirmed
|
||||||
}
|
}
|
||||||
|
|
||||||
return self.renderJsonResponse(request, data=data)
|
return self.renderJsonResponse(request, form, data=data, context=context)
|
||||||
|
|
||||||
|
|
||||||
class IndexView(TemplateView):
|
class IndexView(TemplateView):
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
|
{% extends "modal_delete_form.html" %}
|
||||||
|
|
||||||
|
{% block pre_form_content %}
|
||||||
Are you sure you want to unallocate these parts?
|
Are you sure you want to unallocate these parts?
|
||||||
<br>
|
<br>
|
||||||
This will remove {{ item.quantity }} parts from build '{{ item.build.title }}'.
|
This will remove {{ item.quantity }} parts from build '{{ item.build.title }}'.
|
||||||
|
{% endblock %}
|
@ -1,3 +1,7 @@
|
|||||||
|
{% extends "modal_delete_form.html" %}
|
||||||
|
|
||||||
|
{% block pre_form_content %}
|
||||||
|
|
||||||
Are you sure you want to delete company '{{ company.name }}'?
|
Are you sure you want to delete company '{{ company.name }}'?
|
||||||
|
|
||||||
<br>
|
<br>
|
||||||
@ -11,3 +15,5 @@ If this supplier is deleted, these supplier part entries will also be deleted.</
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
{% endblock %}
|
@ -54,7 +54,7 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
$('#delete-company').click(function() {
|
$('#delete-company').click(function() {
|
||||||
launchDeleteForm(
|
launchModalForm(
|
||||||
"{% url 'company-delete' company.id %}",
|
"{% url 'company-delete' company.id %}",
|
||||||
{
|
{
|
||||||
redirect: "{% url 'company-index' %}"
|
redirect: "{% url 'company-index' %}"
|
||||||
|
@ -1 +1,5 @@
|
|||||||
|
{% extends "modal_delete_form.html" %}
|
||||||
|
|
||||||
|
{% block pre_form_content %}
|
||||||
Are you sure you want to delete this supplier part?
|
Are you sure you want to delete this supplier part?
|
||||||
|
{% endblock %}
|
@ -109,7 +109,7 @@ InvenTree | {{ company.name }} - Parts
|
|||||||
});
|
});
|
||||||
|
|
||||||
$('#delete-part').click(function() {
|
$('#delete-part').click(function() {
|
||||||
launchDeleteForm(
|
launchModalForm(
|
||||||
"{% url 'supplier-part-delete' part.id %}",
|
"{% url 'supplier-part-delete' part.id %}",
|
||||||
{
|
{
|
||||||
redirect: "{% url 'company-index' %}"
|
redirect: "{% url 'company-index' %}"
|
||||||
|
@ -93,10 +93,12 @@ class CompanyCreate(AjaxCreateView):
|
|||||||
|
|
||||||
class CompanyDelete(AjaxDeleteView):
|
class CompanyDelete(AjaxDeleteView):
|
||||||
""" View for deleting a Company object """
|
""" View for deleting a Company object """
|
||||||
|
|
||||||
model = Company
|
model = Company
|
||||||
success_url = '/company/'
|
success_url = '/company/'
|
||||||
ajax_template_name = 'company/delete.html'
|
ajax_template_name = 'company/delete.html'
|
||||||
ajax_form_title = 'Delete Company'
|
ajax_form_title = 'Delete Company'
|
||||||
|
context_object_name = 'company'
|
||||||
|
|
||||||
def get_data(self):
|
def get_data(self):
|
||||||
return {
|
return {
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
|
{% extends "modal_delete_form.html" %}
|
||||||
|
|
||||||
|
{% block pre_form_content %}
|
||||||
Are you sure you wish to delete this attachment?
|
Are you sure you wish to delete this attachment?
|
||||||
<br>
|
<br>
|
||||||
This will remove the file '{{ attachment.basename }}'.
|
This will remove the file '{{ attachment.basename }}'.
|
||||||
|
{% endblock %}
|
@ -62,7 +62,7 @@
|
|||||||
$("#attachment-table").on('click', '.attachment-delete-button', function() {
|
$("#attachment-table").on('click', '.attachment-delete-button', function() {
|
||||||
var button = $(this);
|
var button = $(this);
|
||||||
|
|
||||||
launchDeleteForm(button.attr('url'), {
|
launchModalForm(button.attr('url'), {
|
||||||
success: function() {
|
success: function() {
|
||||||
location.reload();
|
location.reload();
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
|
{% extends "modal_delete_form.html" %}
|
||||||
|
|
||||||
|
{% block pre_form_content %}
|
||||||
|
|
||||||
Are you sure you want to delete this BOM item?
|
Are you sure you want to delete this BOM item?
|
||||||
<br>
|
<br>
|
||||||
Deleting this entry will remove the BOM row from the following part:
|
Deleting this entry will remove the BOM row from the following part:
|
||||||
@ -7,3 +11,5 @@ Deleting this entry will remove the BOM row from the following part:
|
|||||||
<b>{{ item.part.full_name }}</b> - <i>{{ item.part.description }}</i>
|
<b>{{ item.part.full_name }}</b> - <i>{{ item.part.description }}</i>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
{% endblock %}
|
@ -111,7 +111,7 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
$('#cat-delete').click(function() {
|
$('#cat-delete').click(function() {
|
||||||
launchDeleteForm("{% url 'category-delete' category.id %}",
|
launchModalForm("{% url 'category-delete' category.id %}",
|
||||||
{
|
{
|
||||||
redirect: redirect
|
redirect: redirect
|
||||||
});
|
});
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
{% extends "modal_delete_form.html" %}
|
||||||
|
|
||||||
|
{% block pre_form_content %}
|
||||||
Are you sure you want to delete category '{{ category.name }}'?
|
Are you sure you want to delete category '{{ category.name }}'?
|
||||||
|
|
||||||
{% if category.children.all|length > 0 %}
|
{% if category.children.all|length > 0 %}
|
||||||
@ -31,3 +34,5 @@ the top level 'Parts' category.
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
{% endblock %}
|
@ -193,7 +193,7 @@
|
|||||||
|
|
||||||
|
|
||||||
$('#delete-part').click(function() {
|
$('#delete-part').click(function() {
|
||||||
launchDeleteForm(
|
launchModalForm(
|
||||||
"{% url 'part-delete' part.id %}",
|
"{% url 'part-delete' part.id %}",
|
||||||
{
|
{
|
||||||
redirect: {% if part.category %}"{% url 'category-detail' part.category.id %}"{% else %}"{% url 'part-index' %}"{% endif %}
|
redirect: {% if part.category %}"{% url 'category-detail' part.category.id %}"{% else %}"{% url 'part-index' %}"{% endif %}
|
||||||
|
@ -804,3 +804,4 @@ class SupplierPartDelete(AjaxDeleteView):
|
|||||||
success_url = '/supplier/'
|
success_url = '/supplier/'
|
||||||
ajax_template_name = 'company/partdelete.html'
|
ajax_template_name = 'company/partdelete.html'
|
||||||
ajax_form_title = 'Delete Supplier Part'
|
ajax_form_title = 'Delete Supplier Part'
|
||||||
|
context_object_name = 'supplier_part'
|
||||||
|
@ -184,7 +184,7 @@ function loadBomTable(table, options) {
|
|||||||
table.on('click', '.bom-delete-button', function() {
|
table.on('click', '.bom-delete-button', function() {
|
||||||
var button = $(this);
|
var button = $(this);
|
||||||
|
|
||||||
launchDeleteForm(button.attr('url'), {
|
launchModalForm(button.attr('url'), {
|
||||||
success: function() {
|
success: function() {
|
||||||
reloadBomTable(table);
|
reloadBomTable(table);
|
||||||
}
|
}
|
||||||
|
@ -87,7 +87,7 @@ function loadAllocationTable(table, part_id, part, url, required, button) {
|
|||||||
table.on('click', '.item-del-button', function() {
|
table.on('click', '.item-del-button', function() {
|
||||||
var button = $(this);
|
var button = $(this);
|
||||||
|
|
||||||
launchDeleteForm(button.attr('url'), {
|
launchModalForm(button.attr('url'), {
|
||||||
success: function() {
|
success: function() {
|
||||||
table.bootstrapTable('refresh');
|
table.bootstrapTable('refresh');
|
||||||
}
|
}
|
||||||
|
@ -345,68 +345,6 @@ function openModal(options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function launchDeleteForm(url, options = {}) {
|
|
||||||
/* Launch a modal form to delete an object
|
|
||||||
*/
|
|
||||||
|
|
||||||
var modal = options.modal || '#modal-delete';
|
|
||||||
|
|
||||||
$(modal).on('shown.bs.modal', function() {
|
|
||||||
$(modal + ' .modal-form-content').scrollTop(0);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Un-bind any attached click listeners
|
|
||||||
$(modal).off('click', '#modal-form-delete');
|
|
||||||
|
|
||||||
// Request delete form data
|
|
||||||
$.ajax({
|
|
||||||
url: url,
|
|
||||||
type: 'get',
|
|
||||||
dataType: 'json',
|
|
||||||
beforeSend: function() {
|
|
||||||
openModal({modal: modal});
|
|
||||||
},
|
|
||||||
success: function (response) {
|
|
||||||
if (response.title) {
|
|
||||||
modalSetTitle(modal, response.title);
|
|
||||||
}
|
|
||||||
if (response.html_data) {
|
|
||||||
modalSetContent(modal, response.html_data);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
|
|
||||||
$(modal).modal('hide');
|
|
||||||
showAlertDialog('Invalid form response', 'JSON response missing HTML data');
|
|
||||||
}
|
|
||||||
},
|
|
||||||
error: function (xhr, ajaxOptions, thrownError) {
|
|
||||||
$(modal).modal('hide');
|
|
||||||
showAlertDialog('Error requesting form data', renderErrorMessage(xhr));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$(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');
|
|
||||||
afterForm(response, options);
|
|
||||||
},
|
|
||||||
error: function (xhr, ajaxOptions, thrownError) {
|
|
||||||
$(modal).modal('hide');
|
|
||||||
showAlertDialog('Error deleting item', renderErrorMessage(xhr));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function injectModalForm(modal, form_html) {
|
function injectModalForm(modal, form_html) {
|
||||||
/* Inject form content into the modal.
|
/* Inject form content into the modal.
|
||||||
* Updates the HTML of the form content, and then applies some other updates
|
* Updates the HTML of the form content, and then applies some other updates
|
||||||
|
@ -194,7 +194,7 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
$("#stock-delete").click(function () {
|
$("#stock-delete").click(function () {
|
||||||
launchDeleteForm(
|
launchModalForm(
|
||||||
"{% url 'stock-item-delete' item.id %}",
|
"{% url 'stock-item-delete' item.id %}",
|
||||||
{
|
{
|
||||||
redirect: "{% url 'part-stock' item.part.id %}"
|
redirect: "{% url 'part-stock' item.part.id %}"
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
|
{% extends "modal_delete_form.html" %}
|
||||||
|
|
||||||
|
{% block pre_form_content %}
|
||||||
|
|
||||||
|
<div class='alert alert-danger alert-block'>
|
||||||
Are you sure you want to delete this stock item?
|
Are you sure you want to delete this stock item?
|
||||||
|
|
||||||
<br>
|
<br>
|
||||||
|
|
||||||
This will remove <b>{{ item.quantity }}</b> units of <b>{{ item.part.full_name }}</b> from stock.
|
This will remove <b>{{ item.quantity }}</b> units of <b>{{ item.part.full_name }}</b> from stock.
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{% endblock %}
|
@ -97,7 +97,7 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
$('#location-delete').click(function() {
|
$('#location-delete').click(function() {
|
||||||
launchDeleteForm("{% url 'stock-location-delete' location.id %}",
|
launchModalForm("{% url 'stock-location-delete' location.id %}",
|
||||||
{
|
{
|
||||||
redirect: "{% url 'stock-index' %}"
|
redirect: "{% url 'stock-index' %}"
|
||||||
});
|
});
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
{% extends "modal_delete_form.html" %}
|
||||||
|
|
||||||
|
{% block pre_form_content %}
|
||||||
Are you sure you want to delete stock location '{{ location.name }}'?
|
Are you sure you want to delete stock location '{{ location.name }}'?
|
||||||
|
|
||||||
<br>
|
<br>
|
||||||
@ -34,3 +37,4 @@ If this location is deleted, these items will be moved to the top level 'Stock'
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
{% endblock %}
|
1
InvenTree/templates/modal_delete_form.html
Normal file
1
InvenTree/templates/modal_delete_form.html
Normal file
@ -0,0 +1 @@
|
|||||||
|
{% extends "modal_form.html" %}
|
Loading…
Reference in New Issue
Block a user