Merge pull request #1400 from ostcar/fix_localized_thing
Fix LocalizedModelMultipleChoiceField
This commit is contained in:
commit
4257ecefb3
@ -52,19 +52,25 @@ class LocalizedModelChoiceField(forms.ModelChoiceField):
|
|||||||
|
|
||||||
|
|
||||||
class LocalizedModelMultipleChoiceField(forms.ModelMultipleChoiceField):
|
class LocalizedModelMultipleChoiceField(forms.ModelMultipleChoiceField):
|
||||||
|
"""
|
||||||
|
FormField to translate models in many to many fields in forms.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
self.to_field_name = kwargs.get('to_field_name', None)
|
self.to_field_name = kwargs.get('to_field_name', None)
|
||||||
super(LocalizedModelMultipleChoiceField, self).__init__(*args, **kwargs)
|
super(LocalizedModelMultipleChoiceField, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
def _localized_get_choices(self):
|
def _localized_get_choices(self):
|
||||||
|
"""
|
||||||
|
Generator which calles super() and yields the values.
|
||||||
|
"""
|
||||||
if hasattr(self, '_choices'):
|
if hasattr(self, '_choices'):
|
||||||
return self._choices
|
return self._choices
|
||||||
|
|
||||||
c = []
|
for (id, text) in super()._get_choices():
|
||||||
for (id, text) in super(LocalizedModelMultipleChoiceField, self)._get_choices():
|
# This line is only required to translate permission objects
|
||||||
text = text.split(' | ')[-1]
|
text = text.split(' | ')[-1]
|
||||||
c.append((id, ugettext_lazy(text)))
|
yield (id, ugettext_lazy(text))
|
||||||
return c
|
|
||||||
|
|
||||||
choices = property(_localized_get_choices, forms.ChoiceField._set_choices)
|
choices = property(_localized_get_choices, forms.ChoiceField._set_choices)
|
||||||
|
|
||||||
|
12
tests/utils/test_forms.py
Normal file
12
tests/utils/test_forms.py
Normal 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)
|
Loading…
Reference in New Issue
Block a user