From 448dc9fc7939e97da5b901d179b1f23e7ea27647 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 11 Dec 2024 22:06:33 -0600 Subject: [PATCH] fix: add `make_form()` and `make_grid()` methods on web handler to allow override --- src/wuttaweb/handler.py | 22 +++++++++++++++++++++- src/wuttaweb/views/base.py | 8 +++++--- tests/test_handler.py | 12 ++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/wuttaweb/handler.py b/src/wuttaweb/handler.py index f5b4d71..1ac0b78 100644 --- a/src/wuttaweb/handler.py +++ b/src/wuttaweb/handler.py @@ -26,7 +26,7 @@ Web Handler from wuttjamaican.app import GenericHandler -from wuttaweb import static +from wuttaweb import static, forms, grids class WebHandler(GenericHandler): @@ -122,3 +122,23 @@ class WebHandler(GenericHandler): default='wuttaweb.menus:MenuHandler') self.menu_handler = self.app.load_object(spec)(self.config) return self.menu_handler + + def make_form(self, request, **kwargs): + """ + Make and return a new :class:`~wuttaweb.forms.base.Form` + instance, per the given ``kwargs``. + + This is the "base" factory which merely invokes the + constructor. + """ + return forms.Form(request, **kwargs) + + def make_grid(self, request, **kwargs): + """ + Make and return a new :class:`~wuttaweb.grids.base.Grid` + instance, per the given ``kwargs``. + + This is the "base" factory which merely invokes the + constructor. + """ + return grids.Grid(request, **kwargs) diff --git a/src/wuttaweb/views/base.py b/src/wuttaweb/views/base.py index 5121f3c..c5bb0dc 100644 --- a/src/wuttaweb/views/base.py +++ b/src/wuttaweb/views/base.py @@ -30,7 +30,7 @@ from pyramid import httpexceptions from pyramid.renderers import render_to_response from pyramid.response import FileResponse -from wuttaweb import forms, grids +from wuttaweb import grids class View: @@ -75,7 +75,8 @@ class View: This is the "base" factory which merely invokes the constructor. """ - return forms.Form(self.request, **kwargs) + web = self.app.get_web_handler() + return web.make_form(self.request, **kwargs) def make_grid(self, **kwargs): """ @@ -85,7 +86,8 @@ class View: This is the "base" factory which merely invokes the constructor. """ - return grids.Grid(self.request, **kwargs) + web = self.app.get_web_handler() + return web.make_grid(self.request, **kwargs) def make_grid_action(self, key, **kwargs): """ diff --git a/tests/test_handler.py b/tests/test_handler.py index 9c4037f..effb413 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -1,6 +1,8 @@ # -*- coding: utf-8; -*- from wuttaweb import handler as mod, static +from wuttaweb.forms import Form +from wuttaweb.grids import Grid from wuttaweb.menus import MenuHandler from tests.util import WebTestCase @@ -62,3 +64,13 @@ class TestWebHandler(WebTestCase): handler = self.make_handler() menus = handler.get_menu_handler() self.assertIsInstance(menus, MenuHandler) + + def test_make_form(self): + handler = self.make_handler() + form = handler.make_form(self.request) + self.assertIsInstance(form, Form) + + def test_make_grid(self): + handler = self.make_handler() + grid = handler.make_grid(self.request) + self.assertIsInstance(grid, Grid)