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 from application.models import Application
class UserModelChoiceField(ModelChoiceField): class UserModelChoiceField(ModelChoiceField):
""" """
Extend ModelChoiceField for users so that the choices are 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")) 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\"")) 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): class ConfigForm(Form, CssClassMixin):
application_min_supporters = IntegerField( application_min_supporters = IntegerField(
widget=TextInput(attrs={'class':'small-input'}), widget=TextInput(attrs={'class':'small-input'}),

View File

@ -504,7 +504,7 @@ 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', 'Abstain']:
try: try:
return self.get_votes().get(value=name) return self.get_votes().get(value=name)
except Vote.DoesNotExist: except Vote.DoesNotExist:
@ -514,7 +514,7 @@ class ApplicationOption(BaseOption):
class ApplicationPoll(BasePoll, CountInvalid, CountVotesCast): 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 vote_values = [_('Yes', fixstr=True), _('No', fixstr=True), _('Abstain', fixstr=True)]
application = models.ForeignKey(Application) application = models.ForeignKey(Application)

View File

@ -88,9 +88,9 @@
<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 }}<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-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-abstention.png' %}" title="{% trans 'Abstention' %}"> {{ option.Abstain }}<br>
<img src="{% static 'images/icons/voting-invalid.png' %}" title="{% trans 'Invalid' %}"> {{ poll.print_votesinvalid }}<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;"> <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 }} <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 django.utils.translation import ugettext as _
from utils.forms import CssClassMixin from utils.forms import CssClassMixin
from models import Vote
class OptionForm(forms.Form, CssClassMixin): class OptionForm(forms.Form, CssClassMixin):
@ -11,8 +12,16 @@ class OptionForm(forms.Form, CssClassMixin):
kwargs['prefix'] = "option-%s" % formid kwargs['prefix'] = "option-%s" % formid
super(OptionForm, self).__init__(*args, **kwargs) 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( self.fields[key] = forms.IntegerField(
label=_(key), label=value,
initial=value, initial=weight,
) )

View File

@ -36,9 +36,15 @@ class Vote(models.Model):
weight = models.IntegerField(default=1) weight = models.IntegerField(default=1)
value = models.CharField(max_length=255, null=True) value = models.CharField(max_length=255, null=True)
def __unicode__(self): def get_weight(self):
return print_value(self.weight) return print_value(self.weight)
def get_value(self):
return _(self.value)
def __unicode__(self):
return self.get_weight()
class CountVotesCast(models.Model): class CountVotesCast(models.Model):
votescast = models.IntegerField(null=True, blank=True, verbose_name=_("Votes cast")) 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. description = models.TextField(null=True, blank=True, verbose_name=_("Description")) #TODO: Use this field or delete it.
option_class = TextOption option_class = TextOption
vote_values = [_('votes')] vote_values = [_('votes', fixstr=True)]
def has_votes(self): def has_votes(self):
"""
Return True, the there are votes in the poll.
"""
if self.get_options().filter(vote__isnull=False): if self.get_options().filter(vote__isnull=False):
return True return True
return False return False
def set_options(self, options_data=[]): 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: for option_data in options_data:
option = self.option_class(**option_data) option = self.get_option_class()(**option_data)
option.poll = self option.poll = self
option.save() option.save()
def get_options(self): def get_options(self):
"""
Return the option objects for the poll.
"""
return self.get_option_class().objects.filter(poll=self) return self.get_option_class().objects.filter(poll=self)
def get_option_class(self): def get_option_class(self):
"""
Return the option class for the poll. Default is self.option_class.
"""
return self.option_class return self.option_class
def get_vote_values(self): def get_vote_values(self):
"""
Return the possible values for the poll as list.
"""
return self.vote_values return self.vote_values
def set_form_values(self, option, data): 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(): for value in self.get_vote_values():
try: try:
vote = Vote.objects.filter(option=option).get(value=value) vote = Vote.objects.filter(option=option).get(value=value)
@ -117,21 +144,30 @@ class BasePoll(models.Model, SlideMixin):
vote.save() vote.save()
def get_form_values(self, option_id): 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 = [] values = []
for value in self.get_vote_values(): for value in self.get_vote_values():
try: try:
vote = Vote.objects.filter(option=option_id).get(value=value) vote = Vote.objects.filter(option=option_id).get(value=value)
weight = vote.weight values.append(vote)
except Vote.DoesNotExist: except Vote.DoesNotExist:
weight = None values.append(value)
values.append((value, weight))
return values return values
def get_vote_form(self, **kwargs): def get_vote_form(self, **kwargs):
"""
Return the form for one option of the poll.
"""
from poll.forms import OptionForm 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):
"""
Return a list of forms for the poll
"""
forms = [] forms = []
for option in self.get_options(): for option in self.get_options():
form = self.get_vote_form(formid=option.id, **kwargs) form = self.get_vote_form(formid=option.id, **kwargs)
@ -140,6 +176,9 @@ class BasePoll(models.Model, SlideMixin):
return forms return forms
def slide(self): def slide(self):
"""
show a Slide for the Poll.
"""
data = super(BasePoll, self).slide() data = super(BasePoll, self).slide()
# data['template'] = 'projector/TODO.html' # data['template'] = 'projector/TODO.html'
return data return data