3
0
Fork 0

feat: overhaul how form vue template is rendered

now a page template can add `<%def name="form_vue_fields()">` and the
form should inspect/discover and use that instead of its default
This commit is contained in:
Lance Edgar 2025-12-23 22:40:33 -06:00
parent 9edf6f298c
commit 49c001c9ad
9 changed files with 300 additions and 109 deletions

View file

@ -1,48 +1,28 @@
# -*- coding: utf-8; -*-
from unittest import TestCase
import os
from unittest.mock import MagicMock, patch
import sqlalchemy as sa
import colander
import deform
from pyramid import testing
from pyramid.renderers import get_renderer
from wuttjamaican.conf import WuttaConfig
from wuttaweb.forms import base, widgets
from wuttaweb import helpers, subscribers
from wuttaweb.forms import base as mod, widgets
from wuttaweb.grids import Grid
from wuttaweb.testing import WebTestCase
class TestForm(TestCase):
here = os.path.dirname(__file__)
def setUp(self):
self.config = WuttaConfig(
defaults={
"wutta.web.menus.handler_spec": "tests.util:NullMenuHandler",
}
)
self.app = self.config.get_app()
self.request = testing.DummyRequest(wutta_config=self.config, use_oruga=False)
self.pyramid_config = testing.setUp(
request=self.request,
settings={
"wutta_config": self.config,
"mako.directories": ["wuttaweb:templates"],
"pyramid_deform.template_search_path": "wuttaweb:templates/deform",
},
)
class TestForm(WebTestCase):
event = MagicMock(request=self.request)
subscribers.new_request(event)
def tearDown(self):
testing.tearDown()
mako_directories = ["wuttaweb:templates", here]
def make_form(self, **kwargs):
return base.Form(self.request, **kwargs)
return mod.Form(self.request, **kwargs)
def make_schema(self):
schema = colander.Schema(
@ -279,7 +259,7 @@ class TestForm(TestCase):
# but auto-generating without fields is not supported
form = self.make_form()
self.assertIsNone(form.schema)
self.assertRaises(NotImplementedError, form.get_schema)
self.assertRaises(ValueError, form.get_schema)
# schema is auto-generated if model_class provided
form = self.make_form(model_class=model.Setting)
@ -541,6 +521,40 @@ class TestForm(TestCase):
self.assertIn("<script>", html)
self.assertIn("Vue.component('wutta-form', WuttaForm)", html)
def test_get_field_vmodel(self):
model = self.app.model
form = self.make_form(model_class=model.Setting)
result = form.get_field_vmodel("name")
self.assertEqual(result, "modelData.deformField1")
def test_render_vue_fields(self):
model = self.app.model
form = self.make_form(model_class=model.Setting)
context = form.get_vue_context()
# standard behavior
html = form.render_vue_fields(context)
self.assertIn("<b-field", html)
self.assertNotIn("SOMETHING CRAZY", html)
self.assertNotIn("RANDOM TEXT", html)
# declare main template, so form will look for the fields def
# (but this template has no def)
template = get_renderer("/main_template.mako").template
with patch.dict(context, {"main_template": template}):
html = form.render_vue_fields(context)
self.assertIn("<b-field", html)
self.assertNotIn("SOMETHING CRAZY", html)
self.assertNotIn("RANDOM TEXT", html)
# now use a main template which has the fields def
template = get_renderer("/main_template_with_fields.mako").template
with patch.dict(context, {"main_template": template}):
html = form.render_vue_fields(context)
self.assertIn("<b-field", html)
self.assertIn("SOMETHING CRAZY", html)
self.assertNotIn("RANDOM TEXT", html)
def test_render_vue_field(self):
self.pyramid_config.include("pyramid_deform")
schema = self.make_schema()