From d911e311d6d6c036956f59d3735ed770546a29ae Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Sat, 26 Jan 2013 18:18:02 +0100 Subject: [PATCH] Let the user support and unsupport the motion --- openslides/motion/models.py | 27 ++++++++++++++++ openslides/motion/urls.py | 10 ++++++ openslides/motion/views.py | 61 ++++++++++++++++++++++++++++++++++++- 3 files changed, 97 insertions(+), 1 deletion(-) diff --git a/openslides/motion/models.py b/openslides/motion/models.py index 279276f8d..510d481ac 100644 --- a/openslides/motion/models.py +++ b/openslides/motion/models.py @@ -240,6 +240,33 @@ class Motion(SlideMixin, models.Model): except IndexError: return self.new_version + def is_supporter(self, person): + try: + return self.supporter.filter(person=person).exists() + except AttributeError: + return False + + def support(self, person): + """ + Add a Supporter to the list of supporters of the motion. + """ + if not self.is_supporter(person): + MotionSupporter(motion=self, person=person).save() + #self.writelog(_("Supporter: +%s") % (person)) + # TODO: Raise a precise exception for the view in else-clause + + def unsupport(self, person): + """ + remove a supporter from the list of supporters of the motion + """ + try: + self.supporter.filter(person=person).delete() + except MotionSupporter.DoesNotExist: + # TODO: Don't do nothing but raise a precise exception for the view + pass + #else: + #self.writelog(_("Supporter: -%s") % (person)) + class MotionVersion(models.Model): title = models.CharField(max_length=255, verbose_name=ugettext_lazy("Title")) diff --git a/openslides/motion/urls.py b/openslides/motion/urls.py index 59b9b7b2d..ebc1c2155 100644 --- a/openslides/motion/urls.py +++ b/openslides/motion/urls.py @@ -37,4 +37,14 @@ urlpatterns = patterns('openslides.motion.views', 'motion_detail', name='motion_version_detail', ), + + url(r'^(?P\d+)/support/$', + 'motion_support', + name='motion_support', + ), + + url(r'^(?P\d+)/unsupport/$', + 'motion_unsupport', + name='motion_unsupport', + ), ) diff --git a/openslides/motion/views.py b/openslides/motion/views.py index d02aac3c3..4875dae36 100644 --- a/openslides/motion/views.py +++ b/openslides/motion/views.py @@ -22,7 +22,7 @@ from django.http import Http404 from openslides.utils.pdf import stylesheet from openslides.utils.views import ( TemplateView, RedirectView, UpdateView, CreateView, DeleteView, PDFView, - DetailView, ListView, FormView) + DetailView, ListView, FormView, QuestionMixin) from openslides.utils.template import Tab from openslides.utils.utils import html_strong from openslides.projector.api import get_active_slide @@ -121,6 +121,65 @@ class MotionUpdateView(MotionMixin, UpdateView): motion_edit = MotionUpdateView.as_view() +class SupportView(SingleObjectMixin, QuestionMixin, RedirectView): + """ + Classed based view to support or unsupport a motion. Use + support=True or support=False in urls.py + """ + permission_required = 'motion.can_support_motion' + model = Motion + support = True + + 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. + """ + return True #TODO + 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 + elif not self.support and not 'unsupport' in allowed_actions: + messages.error(request, _('You can not unsupport this motion.')) + return False + else: + return True + + def pre_redirect(self, request, *args, **kwargs): + if self.check_allowed_actions(request): + super(SupportView, self).pre_redirect(request, *args, **kwargs) + + 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: + self.object.support(person=self.request.user) + else: + 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 self.object.get_absolute_url() + +motion_support = SupportView.as_view(support=True) +motion_unsupport = SupportView.as_view(support=False) + + class Config(FormView): permission_required = 'config.can_manage_config' form_class = ConfigForm