Clean up SupportView. Raise error message at GET request instead of POST.

This commit is contained in:
Norman Jäckel 2012-10-28 02:09:47 +02:00
parent 1002ecc6e6
commit 9d3d095162

View File

@ -366,7 +366,8 @@ def reset(request, motion_id):
class SupportView(SingleObjectMixin, QuestionMixin, RedirectView): class SupportView(SingleObjectMixin, QuestionMixin, RedirectView):
""" """
Support or unsupport an motion. Classed based view to support or unsupport a motion. Use
support=True or support=False in urls.py
""" """
permission_required = 'motion.can_support_motion' permission_required = 'motion.can_support_motion'
model = Motion model = Motion
@ -379,32 +380,25 @@ class SupportView(SingleObjectMixin, QuestionMixin, RedirectView):
else: else:
return _('Do you really want to unsupport this motion?') return _('Do you really want to unsupport this motion?')
def pre_redirect(self, request, *args, **kwargs):
allowed_actions = self.get_object().get_allowed_actions(request.user)
if self.support and not 'support' in allowed_actions:
messages.error(request, _('You can not support this motion.'))
elif not self.support and not 'unsupport' in allowed_actions:
messages.error(request, _('You can not unsupport this motion.'))
else:
super(SupportView, self).pre_redirect(request, *args, **kwargs)
def pre_post_redirect(self, request, *args, **kwargs): def pre_post_redirect(self, request, *args, **kwargs):
motion = self.get_object() motion = self.get_object()
allowed_actions = motion.get_allowed_actions(request.user) if self.get_answer().lower() == 'yes':
if not self.get_answer().lower() == 'yes': if self.support:
return
if self.support:
if 'support' in allowed_actions:
motion.support(person=request.user) motion.support(person=request.user)
messages.success( self.success_message = _("You have supported this motion successfully.")
request,
_("You have supported this motion successfully."))
else: else:
messages.error( motion.unsupport(person=request.user)
request, self.success_message = _("You have unsupported this motion successfully.")
_('You can not support this motion.')) messages.success(request, self.success_message)
else:
if 'unsupport' in allowed_actions:
self.get_object().unsupport(person=request.user)
messages.success(
request,
_("You have unsupported this motion successfully."))
else:
messages.error(
request,
_('You can not unsupport this motion.'))
def get_redirect_url(self, **kwargs): def get_redirect_url(self, **kwargs):
return reverse('motion_view', args=[kwargs[self.pk_url_kwarg]]) return reverse('motion_view', args=[kwargs[self.pk_url_kwarg]])