From 87af568eeb38368e9115ff01cad08dd4ae095103 Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Fri, 10 Aug 2012 21:00:13 +0200 Subject: [PATCH] redesign the DeleteView Append a QuestionMixin to send a question via the django message api --- openslides/agenda/views.py | 61 ++++-------------------------- openslides/utils/views.py | 76 ++++++++++++++++++++++++++++---------- 2 files changed, 65 insertions(+), 72 deletions(-) diff --git a/openslides/agenda/views.py b/openslides/agenda/views.py index 9e5f69ac3..24a21f1c8 100644 --- a/openslides/agenda/views.py +++ b/openslides/agenda/views.py @@ -167,69 +167,24 @@ class ItemDelete(DeleteView): model = Item url = 'item_overview' - def pre_post_redirect(self, request, *args, **kwargs): - self.object = self.get_object() + def get_answer_options(self): + 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) messages.success(request, _("Item %s and his children were successfully deleted.") \ % html_strong(self.object)) - else: + elif self.get_answer() == 'yes': self.object.delete(with_children=False) messages.success(request, _("Item %s was successfully deleted.") \ % html_strong(self.object)) - def gen_confirm_form(self, request, message, url, singleitem=False): - if singleitem: - messages.warning( - request, - """ - %s -
- - - -
- """ - % (message, url, csrf(request)['csrf_token'], _("Yes"), - _("No")) - ) - else: - messages.warning( - request, - """ - %s -
- - - - -
- """ - % (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): """ diff --git a/openslides/utils/views.py b/openslides/utils/views.py index 5dab27f3c..c7e398481 100644 --- a/openslides/utils/views.py +++ b/openslides/utils/views.py @@ -34,7 +34,7 @@ from django.conf import settings from django.dispatch import receiver from django.http import HttpResponseServerError, HttpResponse, HttpResponseRedirect 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.template import loader, RequestContext from django.template.loader import render_to_string @@ -105,6 +105,55 @@ class AjaxMixin(object): 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([ + '' % (option[0], unicode(option[1])) + for option in self.get_answer_options()]) + messages.warning(self.request, + """ + %(message)s +
+ + %(option_fields)s +
+ """ % { + '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): def get_context_data(self, **kwargs): context = super(TemplateView, self).get_context_data(**kwargs) @@ -221,7 +270,7 @@ class CreateView(PermissionMixin, _CreateView): pass -class DeleteView(RedirectView, SingleObjectMixin): +class DeleteView(RedirectView, SingleObjectMixin, QuestionMixin): def get_confirm_question(self): 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) def pre_redirect(self, request, *args, **kwargs): - self.confirm_form(request, self.object) + self.confirm_form() def pre_post_redirect(self, request, *args, **kwargs): - self.object.delete() - messages.success(request, self.get_success_message()) + if self.get_answer().lower() == 'yes': + self.object.delete() + messages.success(request, self.get_success_message()) def get(self, request, *args, **kwargs): self.object = self.get_object() return super(DeleteView, self).get(request, *args, **kwargs) - def confirm_form(self, request, object): - self.gen_confirm_form(request, self.get_confirm_question(), - object.get_absolute_url('delete')) - - def gen_confirm_form(self, request, message, url): - messages.warning(request, - """ - %s -
- - - -
- """ % (message, url, csrf(request)['csrf_token'], _("Yes"), _("No"))) + def get_answer_url(self): + return self.object.get_absolute_url('delete') class DetailView(TemplateView, SingleObjectMixin):