addet votes cast and votes invalid to new poll-api

This commit is contained in:
Oskar Hahn 2012-02-19 17:31:17 +01:00
parent 7174e93988
commit 5aaa1a53fa
7 changed files with 91 additions and 32 deletions

View File

@ -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)

View File

@ -33,8 +33,15 @@
<td>{{ value.errors }}{{ value }}</td>
</tr>
{% endfor %}
<tr class="total">
<td>{% trans "Invalid votes" %}</td>
<td>{{ pollform.votesinvalid.errors }}{{ pollform.votesinvalid }}</td>
</tr>
<tr class="total">
<td>{% trans "Votes cast" %}</td>
<td>{{ pollform.votescast.errors }}{{ pollform.votescast }}</td>
</tr>
</table>
<p>
<button class="button" type="submit">
<span class="icon ok">{%trans 'Save' %}</span>
</button>

View File

@ -76,12 +76,12 @@
<br>
{% if poll.has_votes %}
{% with poll.get_options.0 as option %}
<img src="/static/images/icons/voting-yes.png" title="{% trans 'Yes' %}"> {{ option.yes.weight }}<br>
<img src="/static/images/icons/voting-no.png" title="{% trans 'No' %}"> {{ option.no.weight }}<br>
<img src="/static/images/icons/voting-abstention.png" title="{% trans 'Abstention' %}"> {{ option.contained.weight }}<br>
<img src="/static/images/icons/voting-invalid.png" title="{% trans 'Invalid' %}"> {{ poll.votesinvalidf }}<br>
<img src="/static/images/icons/voting-yes.png" title="{% trans 'Yes' %}"> {{ option.yes }}<br>
<img src="/static/images/icons/voting-no.png" title="{% trans 'No' %}"> {{ option.no }}<br>
<img src="/static/images/icons/voting-abstention.png" title="{% trans 'Abstention' %}"> {{ option.contained }}<br>
<img src="/static/images/icons/voting-invalid.png" title="{% trans 'Invalid' %}"> {{ poll.votesinvalid }}<br>
<div style="border-top: 1px solid; padding-top: 5px; margin: 5px 0; width: 10em;">
<img src="/static/images/icons/voting-total.png" title="{% trans 'Votes cast' %}"> {{ poll.votescastf }}
<img src="/static/images/icons/voting-total.png" title="{% trans 'Votes cast' %}"> {{ poll.votescast }}
</div>
{% endwith %}
{% if perms.application.can_manage_application %}

View File

@ -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 ''

View File

@ -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')

View File

@ -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):

View File

@ -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())