Merge pull request #1400 from ostcar/fix_localized_thing

Fix LocalizedModelMultipleChoiceField
This commit is contained in:
Oskar Hahn 2015-01-16 21:05:41 +01:00
commit 4257ecefb3
2 changed files with 22 additions and 4 deletions

View File

@ -52,19 +52,25 @@ class LocalizedModelChoiceField(forms.ModelChoiceField):
class LocalizedModelMultipleChoiceField(forms.ModelMultipleChoiceField):
"""
FormField to translate models in many to many fields in forms.
"""
def __init__(self, *args, **kwargs):
self.to_field_name = kwargs.get('to_field_name', None)
super(LocalizedModelMultipleChoiceField, self).__init__(*args, **kwargs)
def _localized_get_choices(self):
"""
Generator which calles super() and yields the values.
"""
if hasattr(self, '_choices'):
return self._choices
c = []
for (id, text) in super(LocalizedModelMultipleChoiceField, self)._get_choices():
for (id, text) in super()._get_choices():
# This line is only required to translate permission objects
text = text.split(' | ')[-1]
c.append((id, ugettext_lazy(text)))
return c
yield (id, ugettext_lazy(text))
choices = property(_localized_get_choices, forms.ChoiceField._set_choices)

12
tests/utils/test_forms.py Normal file
View File

@ -0,0 +1,12 @@
from types import GeneratorType
from unittest.mock import MagicMock
from openslides.utils.forms import LocalizedModelMultipleChoiceField
from openslides.utils.test import TestCase
class TestLocalizedModelMultipleChoiceField(TestCase):
def test_localized_get_choices(self):
test_field = LocalizedModelMultipleChoiceField(queryset=MagicMock())
self.assertEqual(type(test_field.choices), GeneratorType)