xmpp.chapril.org-conversejs/trans/forms.py

50 lines
1.8 KiB
Python
Raw Normal View History

2012-02-29 11:41:08 +01:00
from django import forms
2012-03-04 09:32:02 +01:00
from django.utils.translation import ugettext_lazy 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):
2012-03-02 16:29:02 +01:00
ret = [data.get(name, None)]
2012-03-01 13:49:36 +01:00
for idx in range(1, 10):
fieldname = '%s_%d' % (name, idx)
if not fieldname in data:
break
2012-03-02 16:29:02 +01:00
ret.append(data.get(fieldname, None))
ret = [smart_unicode(r.replace('\r', '')) for r in ret]
2012-03-01 13:49:36 +01:00
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-03-04 09:32:02 +01:00
fuzzy = forms.BooleanField(label = _('Fuzzy'), required = False)
2012-02-29 11:41:08 +01:00
2012-03-04 09:32:02 +01:00
class UploadForm(forms.Form):
2012-03-05 14:05:23 +01:00
file = forms.FileField(label = _('File'))
2012-03-04 09:32:02 +01:00
overwrite = forms.BooleanField(label = _('Overwrite existing translations'), required = False)