redesign the DeleteView
Append a QuestionMixin to send a question via the django message api
This commit is contained in:
parent
1f2f7cc73a
commit
87af568eeb
@ -167,69 +167,24 @@ class ItemDelete(DeleteView):
|
|||||||
model = Item
|
model = Item
|
||||||
url = 'item_overview'
|
url = 'item_overview'
|
||||||
|
|
||||||
def pre_post_redirect(self, request, *args, **kwargs):
|
def get_answer_options(self):
|
||||||
self.object = self.get_object()
|
if self.object.children.exists():
|
||||||
|
return [('all', _("Yes, with all child items."))] + self.answer_options
|
||||||
|
else:
|
||||||
|
return self.answer_options
|
||||||
|
|
||||||
if 'all' in request.POST:
|
def pre_post_redirect(self, request, *args, **kwargs):
|
||||||
|
if self.get_answer() == 'all':
|
||||||
self.object.delete(with_children=True)
|
self.object.delete(with_children=True)
|
||||||
messages.success(request,
|
messages.success(request,
|
||||||
_("Item %s and his children were successfully deleted.") \
|
_("Item %s and his children were successfully deleted.") \
|
||||||
% html_strong(self.object))
|
% html_strong(self.object))
|
||||||
else:
|
elif self.get_answer() == 'yes':
|
||||||
self.object.delete(with_children=False)
|
self.object.delete(with_children=False)
|
||||||
messages.success(request,
|
messages.success(request,
|
||||||
_("Item %s was successfully deleted.") \
|
_("Item %s was successfully deleted.") \
|
||||||
% html_strong(self.object))
|
% html_strong(self.object))
|
||||||
|
|
||||||
def gen_confirm_form(self, request, message, url, singleitem=False):
|
|
||||||
if singleitem:
|
|
||||||
messages.warning(
|
|
||||||
request,
|
|
||||||
"""
|
|
||||||
%s
|
|
||||||
<form action="%s" method="post">
|
|
||||||
<input type="hidden" value="%s" name="csrfmiddlewaretoken">
|
|
||||||
<input type="submit" value="%s">
|
|
||||||
<input type="button" value="%s">
|
|
||||||
</form>
|
|
||||||
"""
|
|
||||||
% (message, url, csrf(request)['csrf_token'], _("Yes"),
|
|
||||||
_("No"))
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
messages.warning(
|
|
||||||
request,
|
|
||||||
"""
|
|
||||||
%s
|
|
||||||
<form action="%s" method="post">
|
|
||||||
<input type="hidden" value="%s" name="csrfmiddlewaretoken">
|
|
||||||
<input type="submit" value="%s">
|
|
||||||
<input type="submit" name="all" value="%s">
|
|
||||||
<input type="button" value="%s">
|
|
||||||
</form>
|
|
||||||
"""
|
|
||||||
% (message, url, csrf(request)['csrf_token'], _("Yes"),
|
|
||||||
_("Yes, with all child items."), _("No"))
|
|
||||||
)
|
|
||||||
|
|
||||||
def confirm_form(self, request, object, item=None):
|
|
||||||
if item is None:
|
|
||||||
item = object
|
|
||||||
if item.get_children():
|
|
||||||
self.gen_confirm_form(
|
|
||||||
request,
|
|
||||||
_('Do you really want to delete %s?') % html_strong(item),
|
|
||||||
item.get_absolute_url('delete'),
|
|
||||||
False,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
self.gen_confirm_form(
|
|
||||||
request,
|
|
||||||
_('Do you really want to delete %s?') % html_strong(item),
|
|
||||||
item.get_absolute_url('delete'),
|
|
||||||
True,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class AgendaPDF(PDFView):
|
class AgendaPDF(PDFView):
|
||||||
"""
|
"""
|
||||||
|
@ -34,7 +34,7 @@ from django.conf import settings
|
|||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
from django.http import HttpResponseServerError, HttpResponse, HttpResponseRedirect
|
from django.http import HttpResponseServerError, HttpResponse, HttpResponseRedirect
|
||||||
from django.utils.decorators import method_decorator
|
from django.utils.decorators import method_decorator
|
||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _, ugettext_noop, ugettext_lazy
|
||||||
from django.utils.importlib import import_module
|
from django.utils.importlib import import_module
|
||||||
from django.template import loader, RequestContext
|
from django.template import loader, RequestContext
|
||||||
from django.template.loader import render_to_string
|
from django.template.loader import render_to_string
|
||||||
@ -105,6 +105,55 @@ class AjaxMixin(object):
|
|||||||
return HttpResponse(json.dumps(self.get_ajax_context(**kwargs)))
|
return HttpResponse(json.dumps(self.get_ajax_context(**kwargs)))
|
||||||
|
|
||||||
|
|
||||||
|
class QuestionMixin(object):
|
||||||
|
question = ugettext_lazy('Are you sure?')
|
||||||
|
answer_options = [('yes', ugettext_lazy("Yes")), ('no', ugettext_lazy("No"))]
|
||||||
|
|
||||||
|
def get_answer_options(self):
|
||||||
|
return self.answer_options
|
||||||
|
|
||||||
|
def get_confirm_question(self):
|
||||||
|
return self.questions
|
||||||
|
|
||||||
|
def get_success_message(self, option=None):
|
||||||
|
if option is None:
|
||||||
|
return _('Invalid answer')
|
||||||
|
return _('You choose %s') % option
|
||||||
|
|
||||||
|
def get_answer(self):
|
||||||
|
for option in self.get_answer_options():
|
||||||
|
if option[0] in self.request.POST:
|
||||||
|
return option[0]
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get_answer_url(self):
|
||||||
|
return self.answer_url
|
||||||
|
|
||||||
|
def confirm_form(self):
|
||||||
|
option_fields = "\n".join([
|
||||||
|
'<input type="submit" name="%s" value="%s">' % (option[0], unicode(option[1]))
|
||||||
|
for option in self.get_answer_options()])
|
||||||
|
messages.warning(self.request,
|
||||||
|
"""
|
||||||
|
%(message)s
|
||||||
|
<form action="%(url)s" method="post">
|
||||||
|
<input type="hidden" value="%(csrf)s" name="csrfmiddlewaretoken">
|
||||||
|
%(option_fields)s
|
||||||
|
</form>
|
||||||
|
""" % {
|
||||||
|
'message': self.get_confirm_question(),
|
||||||
|
'url': self.get_answer_url(),
|
||||||
|
'csrf': csrf(self.request)['csrf_token'],
|
||||||
|
'option_fields': option_fields})
|
||||||
|
|
||||||
|
def pre_redirect(self, request, *args, **kwargs):
|
||||||
|
self.confirm_form(request, self.object)
|
||||||
|
|
||||||
|
def pre_post_redirect(self, request, *args, **kwargs):
|
||||||
|
option = self.get_answer()
|
||||||
|
messages.success(request, self.get_success_message(option))
|
||||||
|
|
||||||
|
|
||||||
class TemplateView(PermissionMixin, _TemplateView):
|
class TemplateView(PermissionMixin, _TemplateView):
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
context = super(TemplateView, self).get_context_data(**kwargs)
|
context = super(TemplateView, self).get_context_data(**kwargs)
|
||||||
@ -221,7 +270,7 @@ class CreateView(PermissionMixin, _CreateView):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class DeleteView(RedirectView, SingleObjectMixin):
|
class DeleteView(RedirectView, SingleObjectMixin, QuestionMixin):
|
||||||
def get_confirm_question(self):
|
def get_confirm_question(self):
|
||||||
return _('Do you really want to delete %s?') % html_strong(self.object)
|
return _('Do you really want to delete %s?') % html_strong(self.object)
|
||||||
|
|
||||||
@ -229,30 +278,19 @@ class DeleteView(RedirectView, SingleObjectMixin):
|
|||||||
return _('%s was successfully deleted.') % html_strong(self.object)
|
return _('%s was successfully deleted.') % html_strong(self.object)
|
||||||
|
|
||||||
def pre_redirect(self, request, *args, **kwargs):
|
def pre_redirect(self, request, *args, **kwargs):
|
||||||
self.confirm_form(request, self.object)
|
self.confirm_form()
|
||||||
|
|
||||||
def pre_post_redirect(self, request, *args, **kwargs):
|
def pre_post_redirect(self, request, *args, **kwargs):
|
||||||
self.object.delete()
|
if self.get_answer().lower() == 'yes':
|
||||||
messages.success(request, self.get_success_message())
|
self.object.delete()
|
||||||
|
messages.success(request, self.get_success_message())
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
self.object = self.get_object()
|
self.object = self.get_object()
|
||||||
return super(DeleteView, self).get(request, *args, **kwargs)
|
return super(DeleteView, self).get(request, *args, **kwargs)
|
||||||
|
|
||||||
def confirm_form(self, request, object):
|
def get_answer_url(self):
|
||||||
self.gen_confirm_form(request, self.get_confirm_question(),
|
return self.object.get_absolute_url('delete')
|
||||||
object.get_absolute_url('delete'))
|
|
||||||
|
|
||||||
def gen_confirm_form(self, request, message, url):
|
|
||||||
messages.warning(request,
|
|
||||||
"""
|
|
||||||
%s
|
|
||||||
<form action="%s" method="post">
|
|
||||||
<input type="hidden" value="%s" name="csrfmiddlewaretoken">
|
|
||||||
<input type="submit" value="%s">
|
|
||||||
<input type="button" value="%s">
|
|
||||||
</form>
|
|
||||||
""" % (message, url, csrf(request)['csrf_token'], _("Yes"), _("No")))
|
|
||||||
|
|
||||||
|
|
||||||
class DetailView(TemplateView, SingleObjectMixin):
|
class DetailView(TemplateView, SingleObjectMixin):
|
||||||
|
Loading…
Reference in New Issue
Block a user