From f7e371d21d2b83da9a38c930e8ee6c89952b0d14 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Mon, 19 Aug 2024 13:40:23 -0500 Subject: [PATCH] fix: add `render_vue_finalize()` methods for grids, forms to make the templates just that much cleaner --- src/wuttaweb/forms/base.py | 35 +++++++++++++++---- src/wuttaweb/grids/base.py | 24 +++++++++++++ src/wuttaweb/templates/form.mako | 9 +++-- .../templates/grids/vue_template.mako | 2 +- src/wuttaweb/templates/master/index.mako | 5 +-- tests/forms/test_base.py | 6 ++++ tests/grids/test_base.py | 6 ++++ 7 files changed, 71 insertions(+), 16 deletions(-) diff --git a/src/wuttaweb/forms/base.py b/src/wuttaweb/forms/base.py index 6b16bdd..5acca59 100644 --- a/src/wuttaweb/forms/base.py +++ b/src/wuttaweb/forms/base.py @@ -918,19 +918,42 @@ class Form: return HTML.tag('b-field', c=[html], **attrs) + def render_vue_finalize(self): + """ + Render the Vue "finalize" script for the form. + + By default this simply returns: + + .. code-block:: html + + + + The actual output may depend on various form attributes, in + particular :attr:`vue_tagname`. + """ + set_data = f"{self.vue_component}.data = function() {{ return {self.vue_component}Data }}" + make_component = f"Vue.component('{self.vue_tagname}', {self.vue_component})" + return HTML.tag('script', c=['\n', + HTML.literal(set_data), + '\n', + HTML.literal(make_component), + '\n']) + def get_vue_model_data(self): """ Returns a dict with form model data. Values may be nested depending on the types of fields contained in the form. - Note that the values need not be "converted" (to be - JSON-compatible) at this stage, for instance ``colander.null`` - is not a problem here. The point is to collect the raw data. + This collects the ``cstruct`` values for all fields which are + present both in :attr:`fields` as well as the Deform schema. - The dict should have a key/value for each field in the form. + It also converts each as needed, to ensure it is + JSON-serializable. - This method is called by :meth:`render_vue_model_data()` which - is responsible for ensuring JSON compatibility. + :returns: Dict of field/value items. """ dform = self.get_deform() model_data = {} diff --git a/src/wuttaweb/grids/base.py b/src/wuttaweb/grids/base.py index 23033a5..3f22aad 100644 --- a/src/wuttaweb/grids/base.py +++ b/src/wuttaweb/grids/base.py @@ -1278,6 +1278,30 @@ class Grid: output = render(template, context) return HTML.literal(output) + def render_vue_finalize(self): + """ + Render the Vue "finalize" script for the grid. + + By default this simply returns: + + .. code-block:: html + + + + The actual output may depend on various grid attributes, in + particular :attr:`vue_tagname`. + """ + set_data = f"{self.vue_component}.data = function() {{ return {self.vue_component}Data }}" + make_component = f"Vue.component('{self.vue_tagname}', {self.vue_component})" + return HTML.tag('script', c=['\n', + HTML.literal(set_data), + '\n', + HTML.literal(make_component), + '\n']) + def get_vue_columns(self): """ Returns a list of Vue-compatible column definitions. diff --git a/src/wuttaweb/templates/form.mako b/src/wuttaweb/templates/form.mako index fc6d3c0..9a0d21e 100644 --- a/src/wuttaweb/templates/form.mako +++ b/src/wuttaweb/templates/form.mako @@ -3,7 +3,9 @@ <%def name="page_content()">
- ${form.render_vue_tag()} + % if form is not Undefined: + ${form.render_vue_tag()} + % endif
@@ -17,10 +19,7 @@ <%def name="finalize_this_page_vars()"> ${parent.finalize_this_page_vars()} % if form is not Undefined: - + ${form.render_vue_finalize()} % endif diff --git a/src/wuttaweb/templates/grids/vue_template.mako b/src/wuttaweb/templates/grids/vue_template.mako index 568cfc7..b161d9f 100644 --- a/src/wuttaweb/templates/grids/vue_template.mako +++ b/src/wuttaweb/templates/grids/vue_template.mako @@ -37,7 +37,7 @@ ## paging % if grid.paginated: paginated - pagination-size="is-small" + pagination-size="${'small' if request.use_oruga else 'is-small'}" :per-page="perPage" :current-page="currentPage" @page-change="onPageChange" diff --git a/src/wuttaweb/templates/master/index.mako b/src/wuttaweb/templates/master/index.mako index 2b6f14b..6731f65 100644 --- a/src/wuttaweb/templates/master/index.mako +++ b/src/wuttaweb/templates/master/index.mako @@ -22,10 +22,7 @@ <%def name="finalize_this_page_vars()"> ${parent.finalize_this_page_vars()} % if grid is not Undefined: - + ${grid.render_vue_finalize()} % endif diff --git a/tests/forms/test_base.py b/tests/forms/test_base.py index 399ecaf..70ee55f 100644 --- a/tests/forms/test_base.py +++ b/tests/forms/test_base.py @@ -405,6 +405,12 @@ class TestForm(TestCase): self.assertIn('