From 79c5e9342256b1a9a0837d9e6aadad8611763807 Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Sun, 28 Oct 2012 19:59:41 +0100 Subject: [PATCH] Redesigned the QuestioMixin --- openslides/agenda/views.py | 4 +- openslides/motion/views.py | 41 ++++++++++----------- openslides/participant/views.py | 16 +++----- openslides/utils/views.py | 65 ++++++++++++++++++--------------- 4 files changed, 63 insertions(+), 63 deletions(-) diff --git a/openslides/agenda/views.py b/openslides/agenda/views.py index deb3e69a6..57629e1e4 100644 --- a/openslides/agenda/views.py +++ b/openslides/agenda/views.py @@ -177,12 +177,12 @@ class ItemDelete(DeleteView): if self.get_answer() == 'all': self.object.delete(with_children=True) messages.success(request, - _("Item %s and his children were successfully deleted.") \ + _("Item %s and his children were successfully deleted.") % html_strong(self.object)) elif self.get_answer() == 'yes': self.object.delete(with_children=False) messages.success(request, - _("Item %s was successfully deleted.") \ + _("Item %s was successfully deleted.") % html_strong(self.object)) diff --git a/openslides/motion/views.py b/openslides/motion/views.py index 66a7f3100..78873ba92 100644 --- a/openslides/motion/views.py +++ b/openslides/motion/views.py @@ -374,18 +374,16 @@ class SupportView(SingleObjectMixin, QuestionMixin, RedirectView): pk_url_kwarg = 'motion_id' support = True - def get_question(self): - if self.support: - return _('Do you really want to support this motion?') - else: - return _('Do you really want to unsupport this motion?') + def get(self, request, *args, **kwargs): + self.object = self.get_object() + return super(SupportView, self).get(request, *args, **kwargs) def check_allowed_actions(self, request): """ Checks whether request.user can support or unsupport the motion. Returns True or False. """ - allowed_actions = self.get_object().get_allowed_actions(request.user) + allowed_actions = self.object.get_allowed_actions(request.user) if self.support and not 'support' in allowed_actions: messages.error(request, _('You can not support this motion.')) return False @@ -399,27 +397,28 @@ class SupportView(SingleObjectMixin, QuestionMixin, RedirectView): if self.check_allowed_actions(request): super(SupportView, self).pre_redirect(request, *args, **kwargs) - def pre_post_redirect(self, request, *args, **kwargs): - motion = self.get_object() - if self.get_answer().lower() == 'yes' and self.check_allowed_actions(request): + def get_question(self): + if self.support: + return _('Do you really want to support this motion?') + else: + return _('Do you really want to unsupport this motion?') + + def case_yes(self): + if self.check_allowed_actions(self.request): if self.support: - motion.support(person=request.user) - self.success_message = _("You have supported this motion successfully.") + self.object.support(person=self.request.user) else: - motion.unsupport(person=request.user) - self.success_message = _("You have unsupported this motion successfully.") - messages.success(request, self.success_message) + self.object.unsupport(person=self.request.user) + + def get_success_message(self): + if self.support: + return _("You have supported this motion successfully.") + else: + return _("You have unsupported this motion successfully.") def get_redirect_url(self, **kwargs): return reverse('motion_view', args=[kwargs[self.pk_url_kwarg]]) - def get_answer_url(self): - if self.support: - answer_url = 'motion_support' - else: - answer_url = 'motion_unsupport' - return reverse(answer_url, args=[self.kwargs[self.pk_url_kwarg]]) - @permission_required('motion.can_manage_motion') @template('motion/view.html') diff --git a/openslides/participant/views.py b/openslides/participant/views.py index 8c49d6eee..00599c327 100644 --- a/openslides/participant/views.py +++ b/openslides/participant/views.py @@ -320,7 +320,7 @@ class UserImportView(FormView): return super(UserImportView, self).form_valid(form) -class ResetPasswordView(RedirectView, SingleObjectMixin, QuestionMixin): +class ResetPasswordView(SingleObjectMixin, QuestionMixin, RedirectView): """ Set the Passwort for a user to his default password. """ @@ -336,17 +336,11 @@ class ResetPasswordView(RedirectView, SingleObjectMixin, QuestionMixin): def get_redirect_url(self, **kwargs): return reverse('user_edit', args=[self.object.id]) - def pre_redirect(self, request, *args, **kwargs): - self.confirm_form() + def case_yes(self): + self.object.reset_password() - def pre_post_redirect(self, request, *args, **kwargs): - if self.get_answer().lower() == 'yes': - self.object.reset_password() - messages.success(request, - _('The Password for %s was successfully reset.') % html_strong(self.object)) - - def get_answer_url(self): - return reverse('user_reset_password', args=[self.object.id]) + def get_success_message(self): + return _('The Password for %s was successfully reset.') % html_strong(self.object) class GroupOverviewView(ListView): diff --git a/openslides/utils/views.py b/openslides/utils/views.py index 3ecfb64de..d3fd10d07 100644 --- a/openslides/utils/views.py +++ b/openslides/utils/views.py @@ -110,20 +110,21 @@ class QuestionMixin(object): success_message = ugettext_lazy('Thank you for your answer') answer_options = [('yes', ugettext_lazy("Yes")), ('no', ugettext_lazy("No"))] - def get_answer_options(self): - return self.answer_options + def pre_redirect(self, request, *args, **kwargs): + # Prints the question in a GET request + self.confirm_form() def get_question(self): return unicode(self.question) - 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_options(self): + return self.answer_options def get_answer_url(self): - return self.answer_url + try: + return self.answer_url + except AttributeError: + return self.request.path def confirm_form(self): option_fields = "\n".join([ @@ -142,11 +143,25 @@ class QuestionMixin(object): 'csrf': csrf(self.request)['csrf_token'], 'option_fields': option_fields}) - def pre_redirect(self, request, *args, **kwargs): - self.confirm_form() - def pre_post_redirect(self, request, *args, **kwargs): - messages.success(request) + # Reacts on the response of the user in a POST-request. + # TODO: call the methodes for all possible answers. + if self.get_answer() == 'yes': + self.case_yes() + messages.success(request, self.get_success_message()) + + def get_answer(self): + for option in self.get_answer_options(): + if option[0] in self.request.POST: + return option[0] + return None + + def case_yes(self): + # TODO: raise a warning + pass + + def get_success_message(self): + return self.success_message class TemplateView(PermissionMixin, _TemplateView): @@ -266,27 +281,19 @@ class CreateView(PermissionMixin, _CreateView): pass -class DeleteView(RedirectView, SingleObjectMixin, QuestionMixin): - def get_question(self): - return _('Do you really want to delete %s?') % html_strong(self.object) - - def get_success_message(self): - return _('%s was successfully deleted.') % html_strong(self.object) - - def pre_redirect(self, request, *args, **kwargs): - self.confirm_form() - - def pre_post_redirect(self, request, *args, **kwargs): - if self.get_answer().lower() == 'yes': - self.object.delete() - messages.success(request, self.get_success_message()) - +class DeleteView(SingleObjectMixin, QuestionMixin, RedirectView): def get(self, request, *args, **kwargs): self.object = self.get_object() return super(DeleteView, self).get(request, *args, **kwargs) - def get_answer_url(self): - return self.object.get_absolute_url('delete') + def get_question(self): + return _('Do you really want to delete %s?') % html_strong(self.object) + + def case_yes(self): + self.object.delete() + + def get_success_message(self): + return _('%s was successfully deleted.') % html_strong(self.object) class DetailView(TemplateView, SingleObjectMixin):