2012-02-29 11:41:08 +01:00
|
|
|
from django import forms
|
|
|
|
from django.utils.translation import ugettext_lazy, ugettext as _
|
2012-03-01 13:35:31 +01:00
|
|
|
from django.utils.safestring import mark_safe
|
2012-03-01 13:49:36 +01:00
|
|
|
from django.utils.encoding import smart_unicode
|
2012-02-29 11:41:08 +01:00
|
|
|
|
2012-03-01 13:31:23 +01:00
|
|
|
class PluralTextarea(forms.Textarea):
|
|
|
|
'''
|
|
|
|
Text area extension which possibly handles plurals.
|
|
|
|
'''
|
|
|
|
def render(self, name, value, attrs=None):
|
2012-03-01 13:35:31 +01:00
|
|
|
if type(value) != list:
|
|
|
|
return super(PluralTextarea, self).render(name, value, attrs)
|
|
|
|
ret = []
|
|
|
|
for idx, val in enumerate(value):
|
|
|
|
if idx > 0:
|
|
|
|
fieldname = '%s_%d' % (name, idx)
|
|
|
|
else:
|
|
|
|
fieldname = name
|
|
|
|
ret.append(super(PluralTextarea, self).render(fieldname, val, attrs))
|
|
|
|
return mark_safe('<br />'.join(ret))
|
2012-03-01 13:31:23 +01:00
|
|
|
|
2012-03-01 13:49:36 +01:00
|
|
|
def value_from_datadict(self, data, files, name):
|
|
|
|
ret = [smart_unicode(data.get(name, None))]
|
|
|
|
for idx in range(1, 10):
|
|
|
|
fieldname = '%s_%d' % (name, idx)
|
|
|
|
if not fieldname in data:
|
|
|
|
break
|
|
|
|
ret.append(smart_unicode(data.get(fieldname, None)))
|
|
|
|
if len(ret) == 0:
|
|
|
|
return ret[0]
|
|
|
|
return ret
|
|
|
|
|
2012-03-01 13:38:10 +01:00
|
|
|
class PluralField(forms.CharField):
|
|
|
|
def __init__(self, max_length=None, min_length=None, *args, **kwargs):
|
|
|
|
super(PluralField, self).__init__(*args, widget = PluralTextarea, **kwargs)
|
|
|
|
|
2012-03-01 13:49:36 +01:00
|
|
|
def to_python(self, value):
|
|
|
|
# We can get list from PluralTextarea
|
|
|
|
return value
|
|
|
|
|
2012-02-29 11:41:08 +01:00
|
|
|
class TranslationForm(forms.Form):
|
|
|
|
checksum = forms.CharField(widget = forms.HiddenInput)
|
2012-03-01 13:38:10 +01:00
|
|
|
target = PluralField(required = False)
|
2012-02-29 11:41:08 +01:00
|
|
|
fuzzy = forms.BooleanField(label = ugettext_lazy('Fuzzy'), required = False)
|
|
|
|
|