From ecb6419f8cc153056c06a4890faa06c7d87b975f Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Sat, 26 Jan 2013 16:19:53 +0100 Subject: [PATCH] fixed apply and success url in utils.views --- openslides/motion/views.py | 1 - openslides/utils/views.py | 44 +++++++++++++++++++++++--------------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/openslides/motion/views.py b/openslides/motion/views.py index 23815b9d2..631290e2a 100644 --- a/openslides/motion/views.py +++ b/openslides/motion/views.py @@ -107,7 +107,6 @@ class MotionUpdateView(MotionMixin, UpdateView): """ # TODO: set permissions model = Motion - apply_url = '' motion_edit = MotionUpdateView.as_view() diff --git a/openslides/utils/views.py b/openslides/utils/views.py index 412ddac88..106c9d9f2 100644 --- a/openslides/utils/views.py +++ b/openslides/utils/views.py @@ -24,6 +24,7 @@ from reportlab.lib.units import cm from django.contrib import messages from django.contrib.auth.decorators import login_required from django.core.context_processors import csrf +from django.core.exceptions import ImproperlyConfigured from django.core.urlresolvers import reverse from django.conf import settings from django.dispatch import receiver @@ -107,13 +108,32 @@ class ExtraContextMixin(object): return context -class SuccessUrlMixin(object): +class UrlMixin(object): + apply_url_name = None + success_url_name = None + success_url = None + apply_url = None + + def get_apply_url(self): + if self.apply_url_name: + return reverse(self.apply_url_name) + elif self.apply_url: + return self.apply_url + else: + try: + return self.object.get_absolute_url('edit') + except AttributeError: + raise ImproperlyConfigured( + "No URL to redirect to. Provide an apply_url_name.") + def get_success_url(self): messages.success(self.request, self.get_success_message()) if 'apply' in self.request.POST: - return reverse(self.get_apply_url(), args=[self.object.id]) - if self.success_url: - url = reverse(self.success_url) + return self.get_apply_url() + if self.success_url_name: + return reverse(self.success_url) + elif self.success_url: + return self.success_url else: try: url = self.object.get_absolute_url() @@ -219,7 +239,7 @@ class RedirectView(PermissionMixin, AjaxMixin, _RedirectView): return reverse(super(RedirectView, self).get_redirect_url(**kwargs)) -class FormView(PermissionMixin, ExtraContextMixin, SuccessUrlMixin, _FormView): +class FormView(PermissionMixin, ExtraContextMixin, UrlMixin, _FormView): def form_invalid(self, form): messages.error(self.request, _('Please check the form for errors.')) return super(FormView, self).form_invalid(form) @@ -240,7 +260,7 @@ class ModelFormMixin(object): form.save_m2m() -class UpdateView(PermissionMixin, SuccessUrlMixin, ExtraContextMixin, +class UpdateView(PermissionMixin, UrlMixin, ExtraContextMixin, ModelFormMixin, _UpdateView): def form_invalid(self, form): messages.error(self.request, _('Please check the form for errors.')) @@ -250,18 +270,8 @@ class UpdateView(PermissionMixin, SuccessUrlMixin, ExtraContextMixin, return _('%s was successfully modified.') % html_strong(self.object) -class CreateView(PermissionMixin, SuccessUrlMixin, ExtraContextMixin, +class CreateView(PermissionMixin, UrlMixin, ExtraContextMixin, ModelFormMixin, _CreateView): - apply_url = None - success_url = None - - def get_apply_url(self): - if self.apply_url: - return self.apply_url - else: - raise ImproperlyConfigured( - "No URL to redirect to. Provide a apply_url.") - def form_invalid(self, form): messages.error(self.request, _('Please check the form for errors.')) return super(CreateView, self).form_invalid(form)