Use Class-based View to support or unsupport an application. Use QuestionMixin for a confirm message

This commit is contained in:
Norman Jäckel 2012-09-19 03:10:15 +02:00
parent 5aa2bc650f
commit 3bde0a8af9
3 changed files with 37 additions and 26 deletions

View File

@ -263,6 +263,7 @@ class Application(models.Model, SlideMixin):
if not self.is_supporter(person):
ApplicationSupporter(application=self, person=person).save()
self.writelog(_("Supporter: +%s") % (person))
# TODO: Raise a precise exception for the view in else-clause
def unsupport(self, person):
"""
@ -274,6 +275,7 @@ class Application(models.Model, SlideMixin):
try:
object = self.applicationsupporter_set.get(person=person).delete()
except ApplicationSupporter.DoesNotExist:
# TODO: Don't do nothing but raise a precise exception for the view
pass
else:
self.writelog(_("Supporter: -%s") % (person))

View File

@ -13,7 +13,7 @@
from django.conf.urls.defaults import url, patterns
from openslides.application.views import (ApplicationDelete, ViewPoll,
ApplicationPDF, ApplicationPollPDF, CreateAgendaItem)
ApplicationPDF, ApplicationPollPDF, CreateAgendaItem, SupportView)
urlpatterns = patterns('openslides.application.views',
url(r'^$',
@ -99,12 +99,12 @@ urlpatterns = patterns('openslides.application.views',
),
url(r'^(?P<application_id>\d+)/support/$',
'support',
SupportView.as_view(unsupport=False, answer_url='support/'),
name='application_support',
),
url(r'^(?P<application_id>\d+)/unsupport/$',
'unsupport',
SupportView.as_view(unsupport=True, answer_url='unsupport/'),
name='application_unsupport',
),

View File

@ -41,7 +41,8 @@ from openslides.utils.pdf import stylesheet
from openslides.utils.template import Tab
from openslides.utils.utils import (template, permission_required,
del_confirm_form, gen_confirm_form)
from openslides.utils.views import PDFView, RedirectView, DeleteView, FormView
from openslides.utils.views import (PDFView, RedirectView, DeleteView,
FormView, SingleObjectMixin, QuestionMixin)
from openslides.utils.person import get_person
from openslides.config.models import config
@ -363,32 +364,40 @@ def reset(request, application_id):
return redirect(reverse('application_view', args=[application_id]))
@permission_required('application.can_support_application')
@template('application/view.html')
def support(request, application_id):
class SupportView(RedirectView, SingleObjectMixin, QuestionMixin):
"""
support an application.
Support or unsupport an application
"""
try:
Application.objects.get(pk=application_id).support(person=request.user)
messages.success(request, _("You have support the motion successfully.") )
except Application.DoesNotExist:
pass
return redirect(reverse('application_view', args=[application_id]))
permission_required = 'application.can_support_application'
model = Application
pk_url_kwarg = 'application_id' # TODO: Is this line neccessary?
unsupport = False # Must be given in SupportView.as_view()
answer_url = None # Must be given in SupportView.as_view()
def get_question(self):
if not self.unsupport:
return _('Do you really want to support this motion?')
else:
return _('Do you really want to unsupport this motion?')
@permission_required('application.can_support_application')
@template('application/view.html')
def unsupport(request, application_id):
"""
unsupport an application.
"""
try:
Application.objects.get(pk=application_id).unsupport(person=request.user)
messages.success(request, _("You have unsupport the motion successfully.") )
except Application.DoesNotExist:
pass
return redirect(reverse('application_view', args=[application_id]))
# TODO: Why do we have to overwrite this method?
def pre_redirect(self, request, *args, **kwargs):
self.confirm_form()
def pre_post_redirect(self, request, *args, **kwargs):
if self.get_answer().lower() == 'yes':
if not self.unsupport:
Application.objects.get(pk=kwargs['application_id']).support(person=request.user)
# Should the Exception Application.DoesNotExist be kept or not?
self.success_message = _("You have supported this motion successfully.")
else:
Application.objects.get(pk=kwargs['application_id']).unsupport(person=request.user)
# Should the Exception Application.DoesNotExist be kept or not?
self.success_message = _("You have unsupported this motion successfully.")
messages.success(request, self.success_message)
def get_redirect_url(self, **kwargs):
return reverse('application_view', args=[kwargs['application_id']])
@permission_required('application.can_manage_application')