diff --git a/src/wuttaweb/forms/schema.py b/src/wuttaweb/forms/schema.py index 07502e6..e4c3703 100644 --- a/src/wuttaweb/forms/schema.py +++ b/src/wuttaweb/forms/schema.py @@ -164,9 +164,13 @@ class WuttaMoney(colander.Money): by default. :param request: Current :term:`request` object. + + :param scale: If this kwarg is specified, it will be passed along + to the widget constructor. """ def __init__(self, request, *args, **kwargs): + self.scale = kwargs.pop('scale', None) super().__init__(*args, **kwargs) self.request = request self.config = self.request.wutta_config @@ -174,6 +178,8 @@ class WuttaMoney(colander.Money): def widget_maker(self, **kwargs): """ """ + if self.scale: + kwargs.setdefault('scale', self.scale) return widgets.WuttaMoneyInputWidget(self.request, **kwargs) diff --git a/src/wuttaweb/forms/widgets.py b/src/wuttaweb/forms/widgets.py index 7b3e4be..1d3035c 100644 --- a/src/wuttaweb/forms/widgets.py +++ b/src/wuttaweb/forms/widgets.py @@ -206,9 +206,13 @@ class WuttaMoneyInputWidget(MoneyInputWidget): * ``moneyinput`` :param request: Current :term:`request` object. + + :param scale: If this kwarg is specified, it will be passed along + to ``render_currency()`` call. """ def __init__(self, request, *args, **kwargs): + self.scale = kwargs.pop('scale', 2) super().__init__(*args, **kwargs) self.request = request self.config = self.request.wutta_config @@ -221,7 +225,8 @@ class WuttaMoneyInputWidget(MoneyInputWidget): if cstruct in (colander.null, None): return HTML.tag('span') cstruct = decimal.Decimal(cstruct) - return HTML.tag('span', c=[self.app.render_currency(cstruct)]) + text = self.app.render_currency(cstruct, scale=self.scale) + return HTML.tag('span', c=[text]) return super().serialize(field, cstruct, **kw) diff --git a/tests/forms/test_schema.py b/tests/forms/test_schema.py index b7b68c4..2a37aa5 100644 --- a/tests/forms/test_schema.py +++ b/tests/forms/test_schema.py @@ -85,9 +85,18 @@ class TestWuttaMoney(WebTestCase): def test_widget_maker(self): enum = self.app.enum + + # default scale typ = mod.WuttaMoney(self.request) widget = typ.widget_maker() self.assertIsInstance(widget, widgets.WuttaMoneyInputWidget) + self.assertEqual(widget.scale, 2) + + # custom scale + typ = mod.WuttaMoney(self.request, scale=4) + widget = typ.widget_maker() + self.assertIsInstance(widget, widgets.WuttaMoneyInputWidget) + self.assertEqual(widget.scale, 4) class TestWuttaQuantity(WebTestCase):