make sure we really render a Translation (bug 624053)

This commit is contained in:
Jeff Balogh 2011-01-10 16:31:31 -07:00
Родитель 012bebc341
Коммит a89099b108
2 изменённых файлов: 24 добавлений и 1 удалений

Просмотреть файл

@ -0,0 +1,18 @@
from pyquery import PyQuery as pq
import test_utils
from nose.tools import eq_
from translations import models, widgets
class TestWidget(test_utils.TestCase):
def test_avoid_purified_translation(self):
# Even if we pass in a LinkifiedTranslation the widget switches to a
# normal Translation before rendering.
w = widgets.TransTextarea.widget()
link = models.LinkifiedTranslation(localized_string='<b>yum yum</b>',
locale='fr', id=10)
link.clean()
widget = w.render('name', link)
eq_(pq(widget).html(), '<b>yum yum</b>')

Просмотреть файл

@ -59,7 +59,7 @@ class TransMulti(forms.widgets.MultiWidget):
def decompress(self, value):
if not value:
return []
elif isinstance(value, long):
elif isinstance(value, (long, int)):
# We got a foreign key to the translation table.
qs = Translation.objects.filter(id=value)
return list(qs.filter(localized_string__isnull=False))
@ -103,11 +103,16 @@ class _TransWidget(object):
"""
def render(self, name, value, attrs=None):
from .fields import switch
attrs = self.build_attrs(attrs)
lang = to_language(value.locale)
attrs.update(lang=lang)
# Use rsplit to drop django's name_idx numbering. (name_0 => name)
name = '%s_%s' % (name.rsplit('_', 1)[0], lang)
# Make sure we don't get a Linkified/Purified Translation. We don't
# want people editing a bleached value.
if value.__class__ != Translation:
value = switch(value, Translation)
return super(_TransWidget, self).render(name, value, attrs)