Changed utils.- and poll.views

This commit is contained in:
Oskar Hahn 2013-02-01 12:51:01 +01:00
parent d911e311d6
commit adeaf248ef
2 changed files with 48 additions and 35 deletions

View File

@ -12,42 +12,20 @@
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.forms.models import modelform_factory 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): class PollFormView(UrlMixin, TemplateView):
template_name = 'poll/poll.html' poll_class = None
poll_argument = 'poll_id'
def set_poll(self, poll_id): def get(self, request, *args, **kwargs):
poll_id = poll_id self.poll = self.object = self.get_object()
self.poll = self.poll_class.objects.get(pk=poll_id) return super(PollFormView, self).get(request, *args, **kwargs)
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 post(self, 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) option_forms = self.poll.get_vote_forms(data=self.request.POST)
FormClass = self.get_modelform_class() FormClass = self.get_modelform_class()
@ -76,3 +54,32 @@ class PollFormView(TemplateView):
pollform.save() pollform.save()
return HttpResponseRedirect(self.get_success_url()) 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)

View File

@ -116,7 +116,7 @@ class UrlMixin(object):
def get_apply_url(self): def get_apply_url(self):
if self.apply_url_name: 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: elif self.apply_url:
return self.apply_url return self.apply_url
else: else:
@ -131,17 +131,19 @@ class UrlMixin(object):
return self.get_apply_url() return self.get_apply_url()
if self.success_url_name: 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: elif self.success_url:
return self.success_url return self.success_url
else: else:
try: try:
url = self.object.get_absolute_url() return self.object.get_absolute_url()
except AttributeError: except AttributeError:
raise ImproperlyConfigured( raise ImproperlyConfigured(
"No URL to redirect to. Either provide a url or define" "No URL to redirect to. Either provide a url or define"
" a get_absolute_url method on the Model.") " a get_absolute_url method on the Model.")
return url
def get_url_name_args(self):
return []
class QuestionMixin(object): class QuestionMixin(object):
@ -218,6 +220,7 @@ class AjaxView(PermissionMixin, AjaxMixin, View):
class RedirectView(PermissionMixin, AjaxMixin, _RedirectView): class RedirectView(PermissionMixin, AjaxMixin, _RedirectView):
permanent = False permanent = False
allow_ajax = False allow_ajax = False
url_name = None
def pre_redirect(self, request, *args, **kwargs): def pre_redirect(self, request, *args, **kwargs):
pass pass
@ -236,7 +239,10 @@ class RedirectView(PermissionMixin, AjaxMixin, _RedirectView):
return super(RedirectView, self).get(request, *args, **kwargs) return super(RedirectView, self).get(request, *args, **kwargs)
def get_redirect_url(self, **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): class FormView(PermissionMixin, ExtraContextMixin, UrlMixin, _FormView):