3
0
Fork 0

fix: add scale kwarg for WuttaMoney schema type, widget

This commit is contained in:
Lance Edgar 2025-01-07 13:40:44 -06:00
parent b73127e350
commit 7f09ca5ede
3 changed files with 21 additions and 1 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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):