mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Add a generic DeleteForm and update AjaxDeleteView
This commit is contained in:
parent
4860b88927
commit
e35a9284eb
@ -17,3 +17,19 @@ class HelperForm(forms.ModelForm):
|
||||
self.helper = FormHelper()
|
||||
|
||||
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'
|
||||
]
|
||||
|
@ -17,6 +17,9 @@ from django.views.generic.base import TemplateView
|
||||
|
||||
from part.models import Part
|
||||
|
||||
from .forms import DeleteForm
|
||||
from .helpers import str2bool
|
||||
|
||||
from rest_framework import views
|
||||
|
||||
|
||||
@ -300,13 +303,28 @@ class AjaxUpdateView(AjaxMixin, UpdateView):
|
||||
return self.renderJsonResponse(request, form, data)
|
||||
|
||||
|
||||
class AjaxDeleteView(AjaxMixin, DeleteView):
|
||||
class AjaxDeleteView(AjaxMixin, UpdateView):
|
||||
|
||||
""" 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)
|
||||
- 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):
|
||||
""" Respond to GET request
|
||||
|
||||
@ -314,19 +332,15 @@ class AjaxDeleteView(AjaxMixin, DeleteView):
|
||||
- Return rendered form to client
|
||||
"""
|
||||
|
||||
super(DeleteView, self).get(request, *args, **kwargs)
|
||||
super(UpdateView, self).get(request, *args, **kwargs)
|
||||
|
||||
data = {
|
||||
'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)
|
||||
}
|
||||
form = self.get_form()
|
||||
|
||||
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):
|
||||
""" Respond to POST request
|
||||
@ -337,14 +351,24 @@ class AjaxDeleteView(AjaxMixin, DeleteView):
|
||||
|
||||
obj = self.get_object()
|
||||
pk = obj.id
|
||||
obj.delete()
|
||||
|
||||
form = self.get_form()
|
||||
|
||||
confirmed = str2bool(request.POST.get('confirm_delete', False))
|
||||
context = self.get_context_data()
|
||||
|
||||
if confirmed:
|
||||
obj.delete()
|
||||
else:
|
||||
form.errors['confirm_delete'] = ['Check box to confirm item deletion']
|
||||
context[self.context_object_name] = self.get_object()
|
||||
|
||||
data = {
|
||||
'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):
|
||||
|
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