diff --git a/src/wuttaweb/forms/schema.py b/src/wuttaweb/forms/schema.py index b5e7667..7591a7a 100644 --- a/src/wuttaweb/forms/schema.py +++ b/src/wuttaweb/forms/schema.py @@ -155,6 +155,28 @@ class WuttaEnum(colander.Enum): return widgets.SelectWidget(**kwargs) +class WuttaMoney(colander.Money): + """ + Custom schema type for "money" fields. + + This is a subclass of :class:`colander:colander.Money`, but uses + the custom :class:`~wuttaweb.forms.widgets.WuttaMoneyInputWidget` + by default. + + :param request: Current :term:`request` object. + """ + + def __init__(self, request, *args, **kwargs): + super().__init__(*args, **kwargs) + self.request = request + self.config = self.request.wutta_config + self.app = self.config.get_app() + + def widget_maker(self, **kwargs): + """ """ + return widgets.WuttaMoneyInputWidget(self.request, **kwargs) + + class WuttaSet(colander.Set): """ Custom schema type for :class:`python:set` fields. diff --git a/src/wuttaweb/forms/widgets.py b/src/wuttaweb/forms/widgets.py index 0fa8773..2541195 100644 --- a/src/wuttaweb/forms/widgets.py +++ b/src/wuttaweb/forms/widgets.py @@ -41,6 +41,7 @@ in the namespace: """ import datetime +import decimal import os import colander @@ -194,6 +195,42 @@ class WuttaDateTimeWidget(DateTimeInputWidget): return super().serialize(field, cstruct, **kw) +class WuttaMoneyInputWidget(MoneyInputWidget): + """ + Custom widget for "money" fields. This is used by default for + :class:`~wuttaweb.forms.schema.WuttaMoney` type nodes. + + The main purpose of this widget is to leverage + :meth:`~wuttjamaican:wuttjamaican.app.AppHandler.render_currency()` + for the readonly display. + + This is a subclass of + :class:`deform:deform.widget.MoneyInputWidget` and uses these + Deform templates: + + * ``moneyinput`` + + :param request: Current :term:`request` object. + """ + + def __init__(self, request, *args, **kwargs): + super().__init__(*args, **kwargs) + self.request = request + self.config = self.request.wutta_config + self.app = self.config.get_app() + + def serialize(self, field, cstruct, **kw): + """ """ + readonly = kw.get('readonly', self.readonly) + if readonly: + if cstruct in (colander.null, None): + return "" + cstruct = decimal.Decimal(cstruct) + return self.app.render_currency(cstruct) + + return super().serialize(field, cstruct, **kw) + + class FileDownloadWidget(Widget): """ Widget for use with :class:`~wuttaweb.forms.schema.FileDownload` diff --git a/tests/forms/test_schema.py b/tests/forms/test_schema.py index 7b15660..564e8c2 100644 --- a/tests/forms/test_schema.py +++ b/tests/forms/test_schema.py @@ -80,6 +80,15 @@ class TestWuttaEnum(WebTestCase): self.assertIsInstance(widget, widgets.SelectWidget) +class TestWuttaMoney(WebTestCase): + + def test_widget_maker(self): + enum = self.app.enum + typ = mod.WuttaMoney(self.request) + widget = typ.widget_maker() + self.assertIsInstance(widget, widgets.WuttaMoneyInputWidget) + + class TestObjectRef(DataTestCase): def setUp(self): diff --git a/tests/forms/test_widgets.py b/tests/forms/test_widgets.py index e324458..71f0dc0 100644 --- a/tests/forms/test_widgets.py +++ b/tests/forms/test_widgets.py @@ -1,6 +1,7 @@ # -*- coding: utf-8; -*- import datetime +import decimal from unittest.mock import patch import colander @@ -107,6 +108,36 @@ class TestWuttaDateTimeWidget(WebTestCase): self.assertEqual(result, '2024-12-12 13:49+0000') +class TestWuttaMoneyInputWidget(WebTestCase): + + def make_field(self, node, **kwargs): + # TODO: not sure why default renderer is in use even though + # pyramid_deform was included in setup? but this works.. + kwargs.setdefault('renderer', deform.Form.default_renderer) + return deform.Field(node, **kwargs) + + def make_widget(self, **kwargs): + return mod.WuttaMoneyInputWidget(self.request, **kwargs) + + def test_serialize(self): + node = colander.SchemaNode(WuttaDateTime()) + field = self.make_field(node) + widget = self.make_widget() + amount = decimal.Decimal('12.34') + + # editable widget has normal text input + result = widget.serialize(field, str(amount)) + self.assertIn('