3
0
Fork 0

Compare commits

...

6 commits

Author SHA1 Message Date
Lance Edgar ae9ca8eee3 bump: version 0.19.1 → 0.19.2 2025-01-07 15:21:30 -06:00
Lance Edgar ee8ca11f6a fix: always use prop key for default grid filters
previous logic was using underlying column name, which breaks when the
prop key does not match
2025-01-07 15:07:21 -06:00
Lance Edgar e5f7fe43c2 fix: avoid request.current_route_url() for user menu
not sure why, at least on chromium browser sometimes that will throw
an error!?  but this works around fine
2025-01-07 14:48:51 -06:00
Lance Edgar 7f09ca5ede fix: add scale kwarg for WuttaMoney schema type, widget 2025-01-07 13:40:44 -06:00
Lance Edgar b73127e350 fix: make WuttaQuantity serialize w/ app handler, remove custom widget
turns out we need to always serialize the value via render_quantity()
and the widget becomes redundant
2025-01-07 13:34:42 -06:00
Lance Edgar b5b88e2a7b fix: bugfix for bool simple settings with default value 2025-01-06 19:29:58 -06:00
11 changed files with 122 additions and 91 deletions

View file

@ -5,6 +5,16 @@ All notable changes to wuttaweb will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
## v0.19.2 (2025-01-07)
### Fix
- always use prop key for default grid filters
- avoid `request.current_route_url()` for user menu
- add `scale` kwarg for `WuttaMoney` schema type, widget
- make WuttaQuantity serialize w/ app handler, remove custom widget
- bugfix for bool simple settings with default value
## v0.19.1 (2025-01-06) ## v0.19.1 (2025-01-06)
### Fix ### Fix

View file

@ -6,7 +6,7 @@ build-backend = "hatchling.build"
[project] [project]
name = "WuttaWeb" name = "WuttaWeb"
version = "0.19.1" version = "0.19.2"
description = "Web App for Wutta Framework" description = "Web App for Wutta Framework"
readme = "README.md" readme = "README.md"
authors = [{name = "Lance Edgar", email = "lance@wuttaproject.org"}] authors = [{name = "Lance Edgar", email = "lance@wuttaproject.org"}]
@ -42,7 +42,6 @@ dependencies = [
"pyramid_fanstatic", "pyramid_fanstatic",
"pyramid_mako", "pyramid_mako",
"pyramid_tm", "pyramid_tm",
"SQLAlchemy-Utils",
"waitress", "waitress",
"WebHelpers2", "WebHelpers2",
"WuttJamaican[db]>=0.19.2", "WuttJamaican[db]>=0.19.2",

View file

@ -164,9 +164,13 @@ class WuttaMoney(colander.Money):
by default. by default.
:param request: Current :term:`request` object. :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): def __init__(self, request, *args, **kwargs):
self.scale = kwargs.pop('scale', None)
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.request = request self.request = request
self.config = self.request.wutta_config self.config = self.request.wutta_config
@ -174,6 +178,8 @@ class WuttaMoney(colander.Money):
def widget_maker(self, **kwargs): def widget_maker(self, **kwargs):
""" """ """ """
if self.scale:
kwargs.setdefault('scale', self.scale)
return widgets.WuttaMoneyInputWidget(self.request, **kwargs) return widgets.WuttaMoneyInputWidget(self.request, **kwargs)
@ -181,8 +187,9 @@ class WuttaQuantity(colander.Decimal):
""" """
Custom schema type for "quantity" fields. Custom schema type for "quantity" fields.
This is a subclass of :class:`colander:colander.Decimal` but uses This is a subclass of :class:`colander:colander.Decimal` but will
:class:`~wuttaweb.forms.widgets.WuttaQuantityWidget` by default. serialize values via
:meth:`~wuttjamaican:wuttjamaican.app.AppHandler.render_quantity()`.
:param request: Current :term:`request` object. :param request: Current :term:`request` object.
""" """
@ -193,9 +200,14 @@ class WuttaQuantity(colander.Decimal):
self.config = self.request.wutta_config self.config = self.request.wutta_config
self.app = self.config.get_app() self.app = self.config.get_app()
def widget_maker(self, **kwargs): def serialize(self, node, appstruct):
""" """ """ """
return widgets.WuttaQuantityWidget(self.request, **kwargs) if appstruct in (colander.null, None):
return colander.null
# nb. we render as quantity here to avoid values like 12.0000,
# so we just show value like 12 instead
return self.app.render_quantity(appstruct)
class WuttaSet(colander.Set): class WuttaSet(colander.Set):

View file

@ -206,9 +206,13 @@ class WuttaMoneyInputWidget(MoneyInputWidget):
* ``moneyinput`` * ``moneyinput``
:param request: Current :term:`request` object. :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): def __init__(self, request, *args, **kwargs):
self.scale = kwargs.pop('scale', 2)
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.request = request self.request = request
self.config = self.request.wutta_config self.config = self.request.wutta_config
@ -221,43 +225,8 @@ class WuttaMoneyInputWidget(MoneyInputWidget):
if cstruct in (colander.null, None): if cstruct in (colander.null, None):
return HTML.tag('span') return HTML.tag('span')
cstruct = decimal.Decimal(cstruct) 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)
class WuttaQuantityWidget(TextInputWidget):
"""
Custom widget for "quantity" fields. This is used by default for
:class:`~wuttaweb.forms.schema.WuttaQuantity` type nodes.
The main purpose of this widget is to leverage
:meth:`~wuttjamaican:wuttjamaican.app.AppHandler.render_quantity()`
for the readonly display.
This is a subclass of
:class:`deform:deform.widget.TextInputWidget` and uses these
Deform templates:
* ``textinput``
: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 HTML.tag('span')
cstruct = decimal.Decimal(cstruct)
return HTML.tag('span', c=[self.app.render_quantity(cstruct)])
return super().serialize(field, cstruct, **kw) return super().serialize(field, cstruct, **kw)

View file

@ -32,7 +32,6 @@ from collections import namedtuple, OrderedDict
import sqlalchemy as sa import sqlalchemy as sa
from sqlalchemy import orm from sqlalchemy import orm
from sqlalchemy_utils import get_columns
import paginate import paginate
from paginate_sqlalchemy import SqlalchemyOrmPage from paginate_sqlalchemy import SqlalchemyOrmPage
@ -1147,16 +1146,29 @@ class Grid:
filters = filters or {} filters = filters or {}
if self.model_class: if self.model_class:
# nb. i first tried self.get_model_columns() but my notes
# say that was too aggressive in many cases. then i tried # nb. i have found this confusing for some reason. some
# using the *subset* of self.columns, just the ones which # things i've tried so far include:
# corresponded to a property on the model class. and now #
# i am using sa-utils to give the "true" column list.. # i first tried self.get_model_columns() but my notes say
for col in get_columns(self.model_class): # that was too aggressive in many cases.
if col.key in filters: #
continue # then i tried using the *subset* of self.columns, just
prop = getattr(self.model_class, col.key) # the ones which correspond to a property on the model
filters[prop.key] = self.make_filter(prop) # class. but sometimes that skips filters we need.
#
# then i tried get_columns() from sa-utils to give the
# "true" column list, but that fails when the underlying
# column has different name than the prop/attr key.
#
# so now, we are looking directly at the sa mapper, for
# all column attrs and then using the prop key.
inspector = sa.inspect(self.model_class)
for prop in inspector.column_attrs:
if prop.key not in filters:
attr = getattr(self.model_class, prop.key)
filters[prop.key] = self.make_filter(attr)
return filters return filters

View file

@ -659,7 +659,7 @@
% if request.is_root: % if request.is_root:
${h.form(url('stop_root'), ref='stopBeingRootForm')} ${h.form(url('stop_root'), ref='stopBeingRootForm')}
${h.csrf_token(request)} ${h.csrf_token(request)}
<input type="hidden" name="referrer" value="${request.current_route_url()}" /> <input type="hidden" name="referrer" value="${request.url}" />
<a @click="stopBeingRoot()" <a @click="stopBeingRoot()"
class="navbar-item has-background-danger has-text-white"> class="navbar-item has-background-danger has-text-white">
Stop being root Stop being root

View file

@ -1329,6 +1329,9 @@ class MasterView(View):
if name in data: if name in data:
value = data[name] value = data[name]
elif simple.get('type') is bool:
# nb. bool false will be *missing* from data
value = False
else: else:
value = simple.get('default') value = simple.get('default')

View file

@ -1,6 +1,7 @@
# -*- coding: utf-8; -*- # -*- coding: utf-8; -*-
import datetime import datetime
import decimal
from unittest import TestCase from unittest import TestCase
from unittest.mock import patch from unittest.mock import patch
@ -15,7 +16,7 @@ from wuttaweb.forms import widgets
from wuttaweb.testing import DataTestCase, WebTestCase from wuttaweb.testing import DataTestCase, WebTestCase
class TestWutaDateTime(TestCase): class TestWuttaDateTime(TestCase):
def test_deserialize(self): def test_deserialize(self):
typ = mod.WuttaDateTime() typ = mod.WuttaDateTime()
@ -84,18 +85,41 @@ class TestWuttaMoney(WebTestCase):
def test_widget_maker(self): def test_widget_maker(self):
enum = self.app.enum enum = self.app.enum
# default scale
typ = mod.WuttaMoney(self.request) typ = mod.WuttaMoney(self.request)
widget = typ.widget_maker() widget = typ.widget_maker()
self.assertIsInstance(widget, widgets.WuttaMoneyInputWidget) 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): class TestWuttaQuantity(WebTestCase):
def test_widget_maker(self): def test_serialize(self):
enum = self.app.enum node = colander.SchemaNode(mod.WuttaQuantity(self.request))
typ = mod.WuttaQuantity(self.request) typ = node.typ
widget = typ.widget_maker()
self.assertIsInstance(widget, widgets.WuttaQuantityWidget) # null
result = typ.serialize(node, colander.null)
self.assertIs(result, colander.null)
result = typ.serialize(node, None)
self.assertIs(result, colander.null)
# quantity
result = typ.serialize(node, 42)
self.assertEqual(result, '42')
result = typ.serialize(node, 42.00)
self.assertEqual(result, '42')
result = typ.serialize(node, decimal.Decimal('42.00'))
self.assertEqual(result, '42')
result = typ.serialize(node, 42.13)
self.assertEqual(result, '42.13')
class TestObjectRef(DataTestCase): class TestObjectRef(DataTestCase):

View file

@ -143,36 +143,6 @@ class TestWuttaMoneyInputWidget(WebTestCase):
self.assertEqual(result, '<span></span>') self.assertEqual(result, '<span></span>')
class TestWuttaQuantityWidget(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.WuttaQuantityWidget(self.request, **kwargs)
def test_serialize(self):
node = colander.SchemaNode(schema.WuttaQuantity(self.request))
field = self.make_field(node)
widget = self.make_widget()
amount = decimal.Decimal('42.00')
# 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, '<span>42</span>')
# readonly w/ null value
result = widget.serialize(field, None, readonly=True)
self.assertEqual(result, '<span></span>')
class TestFileDownloadWidget(WebTestCase): class TestFileDownloadWidget(WebTestCase):
def make_field(self, node, **kwargs): def make_field(self, node, **kwargs):

View file

@ -1565,6 +1565,38 @@ class TestMasterView(WebTestCase):
count = self.session.query(model.Setting).count() count = self.session.query(model.Setting).count()
self.assertEqual(count, 0) self.assertEqual(count, 0)
def test_configure_gather_settings(self):
view = self.make_view()
simple_settings = [
{'name': 'wutta.app_title'},
{'name': 'wutta.foo'},
{'name': 'wutta.flag', 'type': bool, 'default': True},
{'name': 'wutta.number', 'type': int, 'default': 42},
{'name': 'wutta.value1', 'save_if_empty': True},
{'name': 'wutta.value2', 'save_if_empty': False},
{'name': 'wutta.value3', 'save_if_empty': False, 'default': 'baz'},
]
data = {
'wutta.app_title': 'Poser',
'wutta.foo': 'bar',
'wutta.number': 44,
'wutta.value1': None,
}
with patch.object(view, 'configure_get_simple_settings', return_value=simple_settings):
settings = view.configure_gather_settings(data)
self.assertEqual(len(settings), 6)
self.assertEqual(settings, [
{'name': 'wutta.app_title', 'value': 'Poser'},
{'name': 'wutta.foo', 'value': 'bar'},
{'name': 'wutta.flag', 'value': 'false'},
{'name': 'wutta.number', 'value': '44'},
{'name': 'wutta.value1', 'value': ''},
{'name': 'wutta.value3', 'value': 'baz'},
])
############################## ##############################
# row methods # row methods
############################## ##############################