fix: add schema node type, widget for "money" (currency) fields
This commit is contained in:
parent
c4fe90834e
commit
171e9f7488
|
@ -155,6 +155,28 @@ class WuttaEnum(colander.Enum):
|
||||||
return widgets.SelectWidget(**kwargs)
|
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):
|
class WuttaSet(colander.Set):
|
||||||
"""
|
"""
|
||||||
Custom schema type for :class:`python:set` fields.
|
Custom schema type for :class:`python:set` fields.
|
||||||
|
|
|
@ -41,6 +41,7 @@ in the namespace:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
|
import decimal
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import colander
|
import colander
|
||||||
|
@ -194,6 +195,42 @@ class WuttaDateTimeWidget(DateTimeInputWidget):
|
||||||
return super().serialize(field, cstruct, **kw)
|
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):
|
class FileDownloadWidget(Widget):
|
||||||
"""
|
"""
|
||||||
Widget for use with :class:`~wuttaweb.forms.schema.FileDownload`
|
Widget for use with :class:`~wuttaweb.forms.schema.FileDownload`
|
||||||
|
|
|
@ -80,6 +80,15 @@ class TestWuttaEnum(WebTestCase):
|
||||||
self.assertIsInstance(widget, widgets.SelectWidget)
|
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):
|
class TestObjectRef(DataTestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
# -*- coding: utf-8; -*-
|
# -*- coding: utf-8; -*-
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
|
import decimal
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
import colander
|
import colander
|
||||||
|
@ -107,6 +108,36 @@ class TestWuttaDateTimeWidget(WebTestCase):
|
||||||
self.assertEqual(result, '2024-12-12 13:49+0000')
|
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('<b-input', result)
|
||||||
|
|
||||||
|
# readonly is rendered per app convention
|
||||||
|
result = widget.serialize(field, str(amount), readonly=True)
|
||||||
|
self.assertEqual(result, '$12.34')
|
||||||
|
|
||||||
|
# readonly w/ null value
|
||||||
|
result = widget.serialize(field, None, readonly=True)
|
||||||
|
self.assertEqual(result, '')
|
||||||
|
|
||||||
|
|
||||||
class TestFileDownloadWidget(WebTestCase):
|
class TestFileDownloadWidget(WebTestCase):
|
||||||
|
|
||||||
def make_field(self, node, **kwargs):
|
def make_field(self, node, **kwargs):
|
||||||
|
|
Loading…
Reference in a new issue