diff --git a/openslides/application/models.py b/openslides/application/models.py index 496c3dff1..3eb00d636 100644 --- a/openslides/application/models.py +++ b/openslides/application/models.py @@ -24,7 +24,7 @@ from participant.models import Profile from system import config from utils.utils import _propper_unicode from poll import ChoicePoll -from poll.models import BaseOption, BasePoll +from poll.models import BaseOption, BasePoll, CountVotesCast, CountInvalid, Vote class Application(models.Model, Slide): @@ -494,17 +494,22 @@ register_slidemodel(Application) class ApplicationOption(BaseOption): def __getattr__(self, name): - if name in [_('yes'), _('no'), _('contained')]: - print self.poll.get_vote_values() + if name in ['yes', 'no', 'contained']: try: - return self.get_votes().get(value=name) + value = self.get_votes().get(value=name).weight + if value == -1: + return _('majority') + if value == -2: + return _('undocumented') + return value except Vote.DoesNotExist: pass raise AttributeError(name) -class ApplicationPoll(BasePoll): +class ApplicationPoll(BasePoll, CountInvalid, CountVotesCast): option_class = ApplicationOption + vote_values = ['yes', 'no', 'contained'] #todo: Translate the names without changing the db-key application = models.ForeignKey(Application) @@ -513,3 +518,7 @@ class ApplicationPoll(BasePoll): def set_options(self): self.get_option_class()(poll=self).save() + + def append_pollform_fields(self, fields): + CountInvalid.append_pollform_fields(self, fields) + CountVotesCast.append_pollform_fields(self, fields) diff --git a/openslides/application/templates/application/poll_view.html b/openslides/application/templates/application/poll_view.html index a2ba6ac2b..1dd88711f 100644 --- a/openslides/application/templates/application/poll_view.html +++ b/openslides/application/templates/application/poll_view.html @@ -23,18 +23,25 @@ -1 := {% trans 'majority' %}, -2 := {% trans 'undocumented' %}
{% csrf_token %} - - - - - {% for value in forms.0 %} - - + + + + {% for value in forms.0 %} + + + + + {% endfor %} + + + + + + + - {% endfor %}
{% trans "Option" %}{% trans "Votes" %}
{{ value.label }}{{ value.errors }}{{ value }}{% trans "Option" %}{% trans "Votes" %}
{{ value.label }}{{ value.errors }}{{ value }}
{% trans "Invalid votes" %}{{ pollform.votesinvalid.errors }}{{ pollform.votesinvalid }}
{% trans "Votes cast" %}{{ pollform.votescast.errors }}{{ pollform.votescast }}
-

diff --git a/openslides/application/templates/application/view.html b/openslides/application/templates/application/view.html index eb121c571..10cc82ce2 100644 --- a/openslides/application/templates/application/view.html +++ b/openslides/application/templates/application/view.html @@ -76,12 +76,12 @@
{% if poll.has_votes %} {% with poll.get_options.0 as option %} - {{ option.yes.weight }}
- {{ option.no.weight }}
- {{ option.contained.weight }}
- {{ poll.votesinvalidf }}
+ {{ option.yes }}
+ {{ option.no }}
+ {{ option.contained }}
+ {{ poll.votesinvalid }}

- {{ poll.votescastf }} + {{ poll.votescast }}
{% endwith %} {% if perms.application.can_manage_application %} diff --git a/openslides/application/views.py b/openslides/application/views.py index f0c3dbf35..df58fce0d 100644 --- a/openslides/application/views.py +++ b/openslides/application/views.py @@ -420,18 +420,17 @@ def view_poll(request, poll_id): class ViewPoll(PollFormView): poll_class = ApplicationPoll - vote_values = [_('yes'), _('no'), _('contained')] template_name = 'application/poll_view.html' def get_context_data(self, **kwargs): - context = super(ViewPoll, self).get_context_data() + context = super(ViewPoll, self).get_context_data(**kwargs) self.application = self.poll.get_application() context['application'] = self.application return context def get_success_url(self): if not 'apply' in self.request.POST: - return reverse('application_view', args=[self.application.id]) + return reverse('application_view', args=[self.poll.application.id]) return '' diff --git a/openslides/poll/forms.py b/openslides/poll/forms.py index 40fb1cee3..f6a1f1a89 100644 --- a/openslides/poll/forms.py +++ b/openslides/poll/forms.py @@ -1,6 +1,7 @@ from django import forms from django.utils.translation import ugettext as _ + class OptionForm(forms.Form): def __init__(self, *args, **kwargs): extra = kwargs.pop('extra') diff --git a/openslides/poll/models.py b/openslides/poll/models.py index 113e367d3..b1eff8938 100644 --- a/openslides/poll/models.py +++ b/openslides/poll/models.py @@ -16,7 +16,7 @@ from django.utils.translation import ugettext as _ from projector.api import register_slidemodel from projector.models import Slide -from poll.forms import OptionForm + class BaseOption(models.Model): @@ -40,12 +40,32 @@ class Vote(models.Model): value = models.CharField(max_length=255, null=True) +class CountVotesCast(models.Model): + votescast = models.IntegerField(null=True, blank=True, verbose_name=_("Votes cast")) + + def append_pollform_fields(self, fields): + fields.append('votescast') + + class Meta: + abstract = True + + +class CountInvalid(models.Model): + votesinvalid = models.IntegerField(null=True, blank=True, verbose_name=_("Votes invalid")) + + def append_pollform_fields(self, fields): + fields.append('votesinvalid') + + class Meta: + abstract = True + + class BasePoll(models.Model, Slide): + #TODO: It would be nice if this class wouldn't be a subclass from models.Model. But it is needet aslong + # BaseOption has a foreignKey on BasePoll prefix = 'BasePoll' - description = models.TextField(null=True, blank=True, verbose_name = _("Description")) - votescast = models.IntegerField(null=True, blank=True, verbose_name = _("Votes cast")) - votesinvalid = models.IntegerField(null=True, blank=True, verbose_name = _("Votes invalid")) + description = models.TextField(null=True, blank=True, verbose_name=_("Description")) #TODO: Use this field or delete it. option_class = TextOption vote_values = [_('votes')] @@ -91,6 +111,7 @@ class BasePoll(models.Model, Slide): return values def get_vote_form(self, **kwargs): + from poll.forms import OptionForm return OptionForm(extra=self.get_form_values(kwargs['formid']), **kwargs) def get_vote_forms(self, **kwargs): diff --git a/openslides/poll/views.py b/openslides/poll/views.py index ac6d8352b..3059e4595 100644 --- a/openslides/poll/views.py +++ b/openslides/poll/views.py @@ -1,5 +1,6 @@ from django.views.generic import TemplateView from django.http import HttpResponseRedirect +from django.forms.models import modelform_factory class PollFormView(TemplateView): template_name = 'poll/poll.html' @@ -8,33 +9,54 @@ class PollFormView(TemplateView): def set_poll(self, poll_id): poll_id = poll_id self.poll = self.poll_class.objects.get(pk=poll_id) - self.poll.vote_values = self.vote_values 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 not 'forms' in context: - context['forms'] = context['poll'].get_vote_forms() + 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): - context = self.get_context_data(**kwargs) + self.set_poll(self.kwargs['poll_id']) forms = self.poll.get_vote_forms(data=self.request.POST) + + FormClass = self.get_modelform_class() + pollform = FormClass(data=self.request.POST, instance=self.poll, prefix='pollform') + error = False for form in forms: if not form.is_valid(): error = True + + if not pollform.is_valid(): + error = True + if error: - return self.render_to_response(self.get_context_data(forms=forms)) + return self.render_to_response(self.get_context_data( + forms=forms, + pollform=pollform, + )) for form in forms: data = {} for value in self.poll.vote_values: data[value] = form.cleaned_data[value] - print data self.poll.set_form_values(form.option, data) + + pollform.save() return HttpResponseRedirect(self.get_success_url())