translate poll values

This commit is contained in:
Oskar Hahn 2012-04-17 17:35:50 +02:00
parent 0a8d40e2ac
commit 189cfa1309
7 changed files with 342 additions and 294 deletions

View File

@ -18,7 +18,6 @@ from utils.forms import CssClassMixin
from application.models import Application
class UserModelChoiceField(ModelChoiceField):
"""
Extend ModelChoiceField for users so that the choices are
@ -58,6 +57,7 @@ class ApplicationImportForm(Form, CssClassMixin):
csvfile = FileField(widget=FileInput(attrs={'size':'50'}), label=_("CSV File"))
import_permitted = BooleanField(required=False, label=_("Import applications with status \"permitted\""), help_text=_("Set the initial status for each application to \"permitted\""))
class ConfigForm(Form, CssClassMixin):
application_min_supporters = IntegerField(
widget=TextInput(attrs={'class':'small-input'}),

View File

@ -504,7 +504,7 @@ register_slidemodel(Application)
class ApplicationOption(BaseOption):
def __getattr__(self, name):
if name in ['yes', 'no', 'contained']:
if name in ['Yes', 'No', 'Abstain']:
try:
return self.get_votes().get(value=name)
except Vote.DoesNotExist:
@ -514,7 +514,7 @@ class ApplicationOption(BaseOption):
class ApplicationPoll(BasePoll, CountInvalid, CountVotesCast):
option_class = ApplicationOption
vote_values = ['yes', 'no', 'contained'] #todo: Translate the names without changing the db-key
vote_values = [_('Yes', fixstr=True), _('No', fixstr=True), _('Abstain', fixstr=True)]
application = models.ForeignKey(Application)

View File

@ -88,9 +88,9 @@
<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 }}<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-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.Abstain }}<br>
<img src="{% static 'images/icons/voting-invalid.png' %}" title="{% trans 'Invalid' %}"> {{ poll.print_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.print_votescast }}

File diff suppressed because it is too large Load Diff

View File

@ -2,6 +2,7 @@ from django import forms
from django.utils.translation import ugettext as _
from utils.forms import CssClassMixin
from models import Vote
class OptionForm(forms.Form, CssClassMixin):
@ -11,8 +12,16 @@ class OptionForm(forms.Form, CssClassMixin):
kwargs['prefix'] = "option-%s" % formid
super(OptionForm, self).__init__(*args, **kwargs)
for key, value in extra:
for vote in extra:
if type(vote) is Vote:
key = vote.value
value = vote.get_value()
weight = vote.get_weight()
else:
key = vote
value = _(vote)
weight = None
self.fields[key] = forms.IntegerField(
label=_(key),
initial=value,
label=value,
initial=weight,
)

View File

@ -36,9 +36,15 @@ class Vote(models.Model):
weight = models.IntegerField(default=1)
value = models.CharField(max_length=255, null=True)
def __unicode__(self):
def get_weight(self):
return print_value(self.weight)
def get_value(self):
return _(self.value)
def __unicode__(self):
return self.get_weight()
class CountVotesCast(models.Model):
votescast = models.IntegerField(null=True, blank=True, verbose_name=_("Votes cast"))
@ -85,29 +91,50 @@ class BasePoll(models.Model, SlideMixin):
description = models.TextField(null=True, blank=True, verbose_name=_("Description")) #TODO: Use this field or delete it.
option_class = TextOption
vote_values = [_('votes')]
vote_values = [_('votes', fixstr=True)]
def has_votes(self):
"""
Return True, the there are votes in the poll.
"""
if self.get_options().filter(vote__isnull=False):
return True
return False
def set_options(self, options_data=[]):
"""
Add new Option pbjects to the poll.
option_data: A List of arguments for the Option.
"""
for option_data in options_data:
option = self.option_class(**option_data)
option = self.get_option_class()(**option_data)
option.poll = self
option.save()
def get_options(self):
"""
Return the option objects for the poll.
"""
return self.get_option_class().objects.filter(poll=self)
def get_option_class(self):
"""
Return the option class for the poll. Default is self.option_class.
"""
return self.option_class
def get_vote_values(self):
"""
Return the possible values for the poll as list.
"""
return self.vote_values
def set_form_values(self, option, data):
# TODO: recall this function. It has nothing to do with a form
"""
Create or update the vote objects for the poll.
"""
for value in self.get_vote_values():
try:
vote = Vote.objects.filter(option=option).get(value=value)
@ -117,21 +144,30 @@ class BasePoll(models.Model, SlideMixin):
vote.save()
def get_form_values(self, option_id):
# TODO: recall this function. It has nothing to do with a form
"""
Return a the values and the weight of the values as a list with two elements.
"""
values = []
for value in self.get_vote_values():
try:
vote = Vote.objects.filter(option=option_id).get(value=value)
weight = vote.weight
values.append(vote)
except Vote.DoesNotExist:
weight = None
values.append((value, weight))
values.append(value)
return values
def get_vote_form(self, **kwargs):
"""
Return the form for one option of the poll.
"""
from poll.forms import OptionForm
return OptionForm(extra=self.get_form_values(kwargs['formid']), **kwargs)
def get_vote_forms(self, **kwargs):
"""
Return a list of forms for the poll
"""
forms = []
for option in self.get_options():
form = self.get_vote_form(formid=option.id, **kwargs)
@ -140,6 +176,9 @@ class BasePoll(models.Model, SlideMixin):
return forms
def slide(self):
"""
show a Slide for the Poll.
"""
data = super(BasePoll, self).slide()
# data['template'] = 'projector/TODO.html'
return data