2013-09-25 10:01:01 +02:00
|
|
|
from django.forms.models import modelform_factory
|
|
|
|
from django.http import HttpResponseRedirect
|
2014-10-16 18:04:30 +02:00
|
|
|
from django.shortcuts import get_object_or_404
|
2012-02-15 10:46:55 +01:00
|
|
|
|
2013-09-25 12:53:44 +02:00
|
|
|
from openslides.utils.views import TemplateView, FormMixin
|
2012-03-06 12:16:03 +01:00
|
|
|
|
|
|
|
|
2013-09-25 12:53:44 +02:00
|
|
|
class PollFormView(FormMixin, TemplateView):
|
2013-02-01 12:51:01 +01:00
|
|
|
poll_class = None
|
2012-02-15 10:46:55 +01:00
|
|
|
|
2013-02-01 12:51:01 +01:00
|
|
|
def get(self, request, *args, **kwargs):
|
2014-12-22 18:09:05 +01:00
|
|
|
self.poll = self.get_object()
|
2013-02-01 12:51:01 +01:00
|
|
|
return super(PollFormView, self).get(request, *args, **kwargs)
|
2012-02-19 17:31:17 +01:00
|
|
|
|
2012-02-15 10:46:55 +01:00
|
|
|
def post(self, request, *args, **kwargs):
|
2014-12-22 18:09:05 +01:00
|
|
|
self.poll = self.get_object()
|
2012-07-13 11:16:06 +02:00
|
|
|
option_forms = self.poll.get_vote_forms(data=self.request.POST)
|
2012-02-19 17:31:17 +01:00
|
|
|
|
|
|
|
FormClass = self.get_modelform_class()
|
2012-07-10 11:48:03 +02:00
|
|
|
pollform = FormClass(data=self.request.POST, instance=self.poll,
|
2012-11-24 14:01:21 +01:00
|
|
|
prefix='pollform')
|
2012-02-19 17:31:17 +01:00
|
|
|
|
2012-02-15 10:46:55 +01:00
|
|
|
error = False
|
2012-07-13 11:16:06 +02:00
|
|
|
for form in option_forms:
|
2012-02-15 10:46:55 +01:00
|
|
|
if not form.is_valid():
|
|
|
|
error = True
|
2012-02-19 17:31:17 +01:00
|
|
|
|
|
|
|
if not pollform.is_valid():
|
|
|
|
error = True
|
|
|
|
|
2012-02-15 10:46:55 +01:00
|
|
|
if error:
|
2014-01-11 17:07:47 +01:00
|
|
|
response = self.render_to_response(self.get_context_data(
|
2012-07-13 11:16:06 +02:00
|
|
|
forms=option_forms,
|
2014-01-11 17:07:47 +01:00
|
|
|
pollform=pollform))
|
|
|
|
else:
|
|
|
|
for form in option_forms:
|
|
|
|
data = {}
|
|
|
|
for value in self.poll.get_vote_values():
|
|
|
|
data[value] = form.cleaned_data[value]
|
|
|
|
self.poll.set_vote_objects_with_values(form.option, data)
|
2012-02-19 17:31:17 +01:00
|
|
|
|
2014-01-11 17:07:47 +01:00
|
|
|
pollform.save()
|
|
|
|
response = HttpResponseRedirect(self.get_success_url())
|
|
|
|
return response
|
2013-02-01 12:51:01 +01:00
|
|
|
|
|
|
|
def get_poll_class(self):
|
|
|
|
if self.poll_class is not None:
|
|
|
|
return self.poll_class
|
|
|
|
else:
|
2013-12-02 22:29:11 +01:00
|
|
|
raise NotImplementedError(
|
|
|
|
'No poll class defined. Either provide a poll_class or define '
|
|
|
|
'a get_poll_class method.')
|
2013-02-01 12:51:01 +01:00
|
|
|
|
|
|
|
def get_object(self):
|
2014-10-16 18:04:30 +02:00
|
|
|
"""
|
|
|
|
Returns the poll object. Raises Http404 if the poll does not exist.
|
|
|
|
"""
|
2014-12-22 18:09:05 +01:00
|
|
|
try:
|
|
|
|
obj = self._object
|
|
|
|
except AttributeError:
|
|
|
|
queryset = self.get_poll_class().objects.filter(pk=self.kwargs['poll_id'])
|
|
|
|
obj = get_object_or_404(queryset)
|
|
|
|
self._object = obj
|
|
|
|
return obj
|
2013-02-01 12:51:01 +01:00
|
|
|
|
|
|
|
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)
|
2014-01-11 17:07:47 +01:00
|
|
|
return modelform_factory(type(self.poll), fields=fields)
|