mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-01-26 16:16:31 -06:00
New Param: Added Param.get_label() method for a way to retrieve translated message at request time
This commit is contained in:
parent
026860bd56
commit
c121d0064b
@ -23,6 +23,7 @@ Parameter system for command plugins.
|
||||
|
||||
from types import NoneType
|
||||
from util import make_repr
|
||||
from request import ugettext
|
||||
from plugable import ReadOnly, lock, check_name
|
||||
from constants import NULLS, TYPE_ERROR, CALLABLE_ERROR
|
||||
|
||||
@ -199,6 +200,7 @@ class Param(ReadOnly):
|
||||
|
||||
kwargs = (
|
||||
('cli_name', str, None),
|
||||
('label', callable, None),
|
||||
('doc', str, ''),
|
||||
('required', bool, True),
|
||||
('multivalue', bool, False),
|
||||
@ -301,6 +303,14 @@ class Param(ReadOnly):
|
||||
**self.__kw
|
||||
)
|
||||
|
||||
def get_label(self):
|
||||
"""
|
||||
Return translated label using `request.ugettext`.
|
||||
"""
|
||||
if self.label is None:
|
||||
return self.cli_name.decode('UTF-8')
|
||||
return self.label(ugettext)
|
||||
|
||||
def normalize(self, value):
|
||||
"""
|
||||
Normalize ``value`` using normalizer callback.
|
||||
|
@ -23,7 +23,7 @@ Test the `ipalib.error2` module.
|
||||
|
||||
import re
|
||||
import inspect
|
||||
from tests.util import assert_equal, raises, DummyUGettext
|
||||
from tests.util import assert_equal, raises, dummy_ugettext
|
||||
from ipalib import errors2
|
||||
from ipalib.constants import TYPE_ERROR
|
||||
|
||||
|
@ -17,14 +17,14 @@
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
|
||||
"""
|
||||
Test the `ipalib.parameter` module.
|
||||
"""
|
||||
|
||||
from tests.util import raises, ClassChecker, read_only
|
||||
from tests.util import dummy_ugettext, assert_equal
|
||||
from tests.data import binary_bytes, utf8_bytes, unicode_str
|
||||
from ipalib import parameter
|
||||
from ipalib import parameter, request
|
||||
from ipalib.constants import TYPE_ERROR, CALLABLE_ERROR, NULLS
|
||||
|
||||
|
||||
@ -137,6 +137,7 @@ class test_Param(ClassChecker):
|
||||
|
||||
# Test default kwarg values:
|
||||
assert o.cli_name is name
|
||||
assert o.label is None
|
||||
assert o.doc == ''
|
||||
assert o.required is True
|
||||
assert o.multivalue is False
|
||||
@ -200,6 +201,50 @@ class test_Param(ClassChecker):
|
||||
o = self.cls('name', multivalue=True)
|
||||
assert repr(o) == "Param('name', multivalue=True)"
|
||||
|
||||
def test_get_label(self):
|
||||
"""
|
||||
Test the `ipalib.parameter.get_label` method.
|
||||
"""
|
||||
context = request.context
|
||||
cli_name = 'the_cli_name'
|
||||
message = 'The Label'
|
||||
label = lambda _: _(message)
|
||||
o = self.cls('name', cli_name=cli_name, label=label)
|
||||
assert o.label is label
|
||||
|
||||
## Scenario 1: label=callable (a lambda form)
|
||||
|
||||
# Test with no context.ugettext:
|
||||
assert not hasattr(context, 'ugettext')
|
||||
assert_equal(o.get_label(), u'The Label')
|
||||
|
||||
# Test with dummy context.ugettext:
|
||||
assert not hasattr(context, 'ugettext')
|
||||
dummy = dummy_ugettext()
|
||||
context.ugettext = dummy
|
||||
assert o.get_label() is dummy.translation
|
||||
assert dummy.message is message
|
||||
del context.ugettext
|
||||
|
||||
## Scenario 2: label=None
|
||||
o = self.cls('name', cli_name=cli_name)
|
||||
assert o.label is None
|
||||
|
||||
# Test with no context.ugettext:
|
||||
assert not hasattr(context, 'ugettext')
|
||||
assert_equal(o.get_label(), u'the_cli_name')
|
||||
|
||||
# Test with dummy context.ugettext:
|
||||
assert not hasattr(context, 'ugettext')
|
||||
dummy = dummy_ugettext()
|
||||
context.ugettext = dummy
|
||||
assert_equal(o.get_label(), u'the_cli_name')
|
||||
assert not hasattr(dummy, 'message')
|
||||
|
||||
# Cleanup
|
||||
del context.ugettext
|
||||
assert not hasattr(context, 'ugettext')
|
||||
|
||||
def test_convert(self):
|
||||
"""
|
||||
Test the `ipalib.parameter.Param.convert` method.
|
||||
|
@ -24,7 +24,7 @@ Test the `ipalib.request` module.
|
||||
import threading
|
||||
import locale
|
||||
from tests.util import raises, assert_equal
|
||||
from tests.util import TempDir, DummyUGettext, DummyUNGettext
|
||||
from tests.util import TempDir, dummy_ugettext, dummy_ungettext
|
||||
from ipalib.constants import OVERRIDE_ERROR
|
||||
from ipalib import request
|
||||
|
||||
@ -43,7 +43,7 @@ def test_ugettext():
|
||||
|
||||
# Test with dummy context.ugettext:
|
||||
assert not hasattr(context, 'ugettext')
|
||||
dummy = DummyUGettext()
|
||||
dummy = dummy_ugettext()
|
||||
context.ugettext = dummy
|
||||
assert f(message) is dummy.translation
|
||||
assert dummy.message is message
|
||||
@ -69,7 +69,7 @@ def test_ungettext():
|
||||
|
||||
# Test singular with dummy context.ungettext
|
||||
assert not hasattr(context, 'ungettext')
|
||||
dummy = DummyUNGettext()
|
||||
dummy = dummy_ungettext()
|
||||
context.ungettext = dummy
|
||||
assert f(singular, plural, 1) is dummy.translation_singular
|
||||
assert dummy.singular is singular
|
||||
@ -80,7 +80,7 @@ def test_ungettext():
|
||||
|
||||
# Test plural with dummy context.ungettext
|
||||
assert not hasattr(context, 'ungettext')
|
||||
dummy = DummyUNGettext()
|
||||
dummy = dummy_ungettext()
|
||||
context.ungettext = dummy
|
||||
assert f(singular, plural, 2) is dummy.translation_plural
|
||||
assert dummy.singular is singular
|
||||
|
@ -287,7 +287,7 @@ class PluginTester(object):
|
||||
return (o, api, home)
|
||||
|
||||
|
||||
class DummyUGettext(object):
|
||||
class dummy_ugettext(object):
|
||||
__called = False
|
||||
|
||||
def __init__(self):
|
||||
@ -301,7 +301,7 @@ class DummyUGettext(object):
|
||||
return self.translation
|
||||
|
||||
|
||||
class DummyUNGettext(object):
|
||||
class dummy_ungettext(object):
|
||||
__called = False
|
||||
|
||||
def __init__(self):
|
||||
|
Loading…
Reference in New Issue
Block a user