diff --git a/openslides/poll/views.py b/openslides/poll/views.py index 70c48e7cc..2293e8c54 100644 --- a/openslides/poll/views.py +++ b/openslides/poll/views.py @@ -12,42 +12,20 @@ from django.http import HttpResponseRedirect from django.forms.models import modelform_factory +from django.core.exceptions import ImproperlyConfigured -from openslides.utils.views import TemplateView +from openslides.utils.views import TemplateView, UrlMixin -class PollFormView(TemplateView): - template_name = 'poll/poll.html' - poll_argument = 'poll_id' +class PollFormView(UrlMixin, TemplateView): + poll_class = None - def set_poll(self, poll_id): - poll_id = poll_id - self.poll = self.poll_class.objects.get(pk=poll_id) - - def get_context_data(self, **kwargs): - context = super(PollFormView, self).get_context_data(**kwargs) - self.set_poll(self.kwargs['poll_id']) - context['poll'] = self.poll - if 'forms' in kwargs: - context['forms'] = kwargs['forms'] - context['pollform'] = kwargs['pollform'] - else: - context['forms'] = self.poll.get_vote_forms() - FormClass = self.get_modelform_class() - context['pollform'] = FormClass(instance=self.poll, - prefix='pollform') - return context - - def get_success_url(self): - return self.success_url - - def get_modelform_class(self): - fields = [] - self.poll.append_pollform_fields(fields) - return modelform_factory(self.poll.__class__, fields=fields) + def get(self, request, *args, **kwargs): + self.poll = self.object = self.get_object() + return super(PollFormView, self).get(request, *args, **kwargs) def post(self, request, *args, **kwargs): - self.set_poll(self.kwargs['poll_id']) + self.poll = self.object = self.get_object() option_forms = self.poll.get_vote_forms(data=self.request.POST) FormClass = self.get_modelform_class() @@ -76,3 +54,32 @@ class PollFormView(TemplateView): pollform.save() return HttpResponseRedirect(self.get_success_url()) + + def get_poll_class(self): + if self.poll_class is not None: + return self.poll_class + else: + raise ImproperlyConfigured( + "No poll class defined. Either provide a poll_class or define" + " a get_poll_class method.") + + def get_object(self): + return self.get_poll_class().objects.get(pk=self.kwargs['poll_id']) + + def get_context_data(self, **kwargs): + context = super(PollFormView, self).get_context_data(**kwargs) + context['poll'] = self.poll + if 'forms' in kwargs: + context['forms'] = kwargs['forms'] + context['pollform'] = kwargs['pollform'] + else: + context['forms'] = self.poll.get_vote_forms() + FormClass = self.get_modelform_class() + context['pollform'] = FormClass(instance=self.poll, + prefix='pollform') + return context + + def get_modelform_class(self): + fields = [] + self.poll.append_pollform_fields(fields) + return modelform_factory(self.poll.__class__, fields=fields) diff --git a/openslides/utils/views.py b/openslides/utils/views.py index 0958421d0..4051f93e2 100644 --- a/openslides/utils/views.py +++ b/openslides/utils/views.py @@ -116,7 +116,7 @@ class UrlMixin(object): def get_apply_url(self): if self.apply_url_name: - return reverse(self.apply_url_name) + return reverse(self.apply_url_name, args=self.get_url_name_args()) elif self.apply_url: return self.apply_url else: @@ -131,17 +131,19 @@ class UrlMixin(object): return self.get_apply_url() if self.success_url_name: - return reverse(self.success_url_name) + return reverse(self.success_url_name, args=self.get_url_name_args()) elif self.success_url: return self.success_url else: try: - url = self.object.get_absolute_url() + return self.object.get_absolute_url() except AttributeError: raise ImproperlyConfigured( "No URL to redirect to. Either provide a url or define" " a get_absolute_url method on the Model.") - return url + + def get_url_name_args(self): + return [] class QuestionMixin(object): @@ -218,6 +220,7 @@ class AjaxView(PermissionMixin, AjaxMixin, View): class RedirectView(PermissionMixin, AjaxMixin, _RedirectView): permanent = False allow_ajax = False + url_name = None def pre_redirect(self, request, *args, **kwargs): pass @@ -236,7 +239,10 @@ class RedirectView(PermissionMixin, AjaxMixin, _RedirectView): return super(RedirectView, self).get(request, *args, **kwargs) def get_redirect_url(self, **kwargs): - return reverse(super(RedirectView, self).get_redirect_url(**kwargs)) + if self.url_name is not None: + return reverse(self.url_name) + else: + return super(RedirectView, self).get_redirect_url(**kwargs) class FormView(PermissionMixin, ExtraContextMixin, UrlMixin, _FormView):