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 system import config
from utils.utils import _propper_unicode from utils.utils import _propper_unicode
from poll import ChoicePoll from poll import ChoicePoll
from poll.models import BaseOption, BasePoll from poll.models import BaseOption, BasePoll, CountVotesCast, CountInvalid, Vote
class Application(models.Model, Slide): class Application(models.Model, Slide):
@ -494,17 +494,22 @@ register_slidemodel(Application)
class ApplicationOption(BaseOption): class ApplicationOption(BaseOption):
def __getattr__(self, name): def __getattr__(self, name):
if name in [_('yes'), _('no'), _('contained')]: if name in ['yes', 'no', 'contained']:
print self.poll.get_vote_values()
try: 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: except Vote.DoesNotExist:
pass pass
raise AttributeError(name) raise AttributeError(name)
class ApplicationPoll(BasePoll): class ApplicationPoll(BasePoll, CountInvalid, CountVotesCast):
option_class = ApplicationOption option_class = ApplicationOption
vote_values = ['yes', 'no', 'contained'] #todo: Translate the names without changing the db-key
application = models.ForeignKey(Application) application = models.ForeignKey(Application)
@ -513,3 +518,7 @@ class ApplicationPoll(BasePoll):
def set_options(self): def set_options(self):
self.get_option_class()(poll=self).save() 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

@ -23,18 +23,25 @@
<i>-1 := {% trans 'majority' %}, -2 := {% trans 'undocumented' %}</i> <i>-1 := {% trans 'majority' %}, -2 := {% trans 'undocumented' %}</i>
<form action="" method="post">{% csrf_token %} <form action="" method="post">{% csrf_token %}
<table class="table" style="width: auto;"> <table class="table" style="width: auto;">
<tr>
<th>{% trans "Option" %}</th>
<th>{% trans "Votes" %}</th>
</tr>
{% for value in forms.0 %}
<tr> <tr>
<td>{{ value.label }}</td> <th>{% trans "Option" %}</th>
<td>{{ value.errors }}{{ value }}</td> <th>{% trans "Votes" %}</th>
</tr>
{% for value in forms.0 %}
<tr>
<td>{{ value.label }}</td>
<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> </tr>
{% endfor %}
</table> </table>
<p>
<button class="button" type="submit"> <button class="button" type="submit">
<span class="icon ok">{%trans 'Save' %}</span> <span class="icon ok">{%trans 'Save' %}</span>
</button> </button>

View File

@ -76,12 +76,12 @@
<br> <br>
{% if poll.has_votes %} {% if poll.has_votes %}
{% with poll.get_options.0 as option %} {% 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-yes.png" title="{% trans 'Yes' %}"> {{ option.yes }}<br>
<img src="/static/images/icons/voting-no.png" title="{% trans 'No' %}"> {{ option.no.weight }}<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.weight }}<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.votesinvalidf }}<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;"> <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> </div>
{% endwith %} {% endwith %}
{% if perms.application.can_manage_application %} {% if perms.application.can_manage_application %}

View File

@ -420,18 +420,17 @@ def view_poll(request, poll_id):
class ViewPoll(PollFormView): class ViewPoll(PollFormView):
poll_class = ApplicationPoll poll_class = ApplicationPoll
vote_values = [_('yes'), _('no'), _('contained')]
template_name = 'application/poll_view.html' template_name = 'application/poll_view.html'
def get_context_data(self, **kwargs): 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() self.application = self.poll.get_application()
context['application'] = self.application context['application'] = self.application
return context return context
def get_success_url(self): def get_success_url(self):
if not 'apply' in self.request.POST: 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 '' return ''

View File

@ -1,6 +1,7 @@
from django import forms from django import forms
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
class OptionForm(forms.Form): class OptionForm(forms.Form):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
extra = kwargs.pop('extra') 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.api import register_slidemodel
from projector.models import Slide from projector.models import Slide
from poll.forms import OptionForm
class BaseOption(models.Model): class BaseOption(models.Model):
@ -40,12 +40,32 @@ class Vote(models.Model):
value = models.CharField(max_length=255, null=True) 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): 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' prefix = 'BasePoll'
description = models.TextField(null=True, blank=True, verbose_name = _("Description")) description = models.TextField(null=True, blank=True, verbose_name=_("Description")) #TODO: Use this field or delete it.
votescast = models.IntegerField(null=True, blank=True, verbose_name = _("Votes cast"))
votesinvalid = models.IntegerField(null=True, blank=True, verbose_name = _("Votes invalid"))
option_class = TextOption option_class = TextOption
vote_values = [_('votes')] vote_values = [_('votes')]
@ -91,6 +111,7 @@ class BasePoll(models.Model, Slide):
return values return values
def get_vote_form(self, **kwargs): def get_vote_form(self, **kwargs):
from poll.forms import OptionForm
return OptionForm(extra=self.get_form_values(kwargs['formid']), **kwargs) return OptionForm(extra=self.get_form_values(kwargs['formid']), **kwargs)
def get_vote_forms(self, **kwargs): def get_vote_forms(self, **kwargs):

View File

@ -1,5 +1,6 @@
from django.views.generic import TemplateView from django.views.generic import TemplateView
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.forms.models import modelform_factory
class PollFormView(TemplateView): class PollFormView(TemplateView):
template_name = 'poll/poll.html' template_name = 'poll/poll.html'
@ -8,33 +9,54 @@ class PollFormView(TemplateView):
def set_poll(self, poll_id): def set_poll(self, poll_id):
poll_id = poll_id poll_id = poll_id
self.poll = self.poll_class.objects.get(pk=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): def get_context_data(self, **kwargs):
context = super(PollFormView, self).get_context_data(**kwargs) context = super(PollFormView, self).get_context_data(**kwargs)
self.set_poll(self.kwargs['poll_id']) self.set_poll(self.kwargs['poll_id'])
context['poll'] = self.poll context['poll'] = self.poll
if not 'forms' in context: if 'forms' in kwargs:
context['forms'] = context['poll'].get_vote_forms() 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 return context
def get_success_url(self): def get_success_url(self):
return self.success_url 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):
context = self.get_context_data(**kwargs) self.set_poll(self.kwargs['poll_id'])
forms = self.poll.get_vote_forms(data=self.request.POST) 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 error = False
for form in forms: for form in forms:
if not form.is_valid(): if not form.is_valid():
error = True error = True
if not pollform.is_valid():
error = True
if error: 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: for form in forms:
data = {} data = {}
for value in self.poll.vote_values: for value in self.poll.vote_values:
data[value] = form.cleaned_data[value] data[value] = form.cleaned_data[value]
print data
self.poll.set_form_values(form.option, data) self.poll.set_form_values(form.option, data)
pollform.save()
return HttpResponseRedirect(self.get_success_url()) return HttpResponseRedirect(self.get_success_url())