From cac005f993529c0f5422d48ec6d39e449a73e7df Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Fri, 13 Jan 2023 03:51:12 -0600 Subject: [PATCH] Semi-finish logic for writing new table model class to file definitely needs more polish and features, but the gist.. --- tailbone/templates/tables/create.mako | 363 ++++++++++++++++++++++++-- tailbone/views/tables.py | 86 +++--- 2 files changed, 387 insertions(+), 62 deletions(-) diff --git a/tailbone/templates/tables/create.mako b/tailbone/templates/tables/create.mako index 90d9d26f..4d46273a 100644 --- a/tailbone/templates/tables/create.mako +++ b/tailbone/templates/tables/create.mako @@ -10,7 +10,7 @@ -<%def name="render_buefy_form()"> +<%def name="render_this_page()"> Enter Details - ${parent.render_buefy_form()} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Record version data for this table + + + +
+ +
+
+

Columns

+
+
+ + New + +
+
+ + + % if buefy_0_8: + + % endif + + + + + + +
+ +
+ + Details are complete + +
+ + label="Write Model">

Write Model

+ + + {{ tableBranch }} + + + + {{ tableName }} + + + + {{ tableModelName }} + + + + + +
- - TODO: poser_widget - - - TODO: PoserWidget - - - TODO: ~/src/poser/poser/db/model/widgets.py -
- Write model class to file + @click="writeModelFile()" + :disabled="writingModelFile"> + {{ writingModelFile ? "Working, please wait..." : "Write model class to file" }}
@@ -206,6 +438,107 @@ diff --git a/tailbone/views/tables.py b/tailbone/views/tables.py index db045a73..196e70f5 100644 --- a/tailbone/views/tables.py +++ b/tailbone/views/tables.py @@ -26,6 +26,7 @@ Views with info about the underlying Rattail tables from __future__ import unicode_literals, absolute_import +import os import sys import warnings @@ -160,65 +161,36 @@ class TableView(MasterView): def make_form_schema(self): return TableSchema() - def configure_form(self, f): - super(TableView, self).configure_form(f) + def template_kwargs_create(self, **kwargs): + kwargs = super(TableView, self).template_kwargs_create(**kwargs) app = self.get_rattail_app() + model = self.model - # exclude some fields when creating - if self.creating: - f.remove('row_count', - 'module_name', - 'module_file') + kwargs['branch_name_options'] = self.db_handler.get_alembic_branch_names() - # branch_name - if self.creating: + branch_name = app.get_table_prefix() + if branch_name not in kwargs['branch_name_options']: + branch_name = None + kwargs['branch_name'] = branch_name - # move this field to top of form, as it's more fundamental - # when creating new table - f.remove('branch_name') - f.insert(0, 'branch_name') + kwargs['model_dir'] = (os.path.dirname(model.__file__) + + os.sep) - # define options for dropdown - branches = self.db_handler.get_alembic_branch_names() - values = [(branch, branch) for branch in branches] - f.set_widget('branch_name', dfwidget.SelectWidget(values=values)) + return kwargs - # default to custom app branch, if applicable - table_prefix = app.get_table_prefix() - if table_prefix in branches: - f.set_default('branch_name', table_prefix) - f.set_helptext('branch_name', "Leave this set to your custom app branch, unless you know what you're doing.") + def write_model_file(self): + data = self.request.json_body + path = data['module_file'] - # table_name - if self.creating: - f.set_default('table_name', '{}_widget'.format(app.get_table_prefix())) - f.set_helptext('table_name', "Should be singular in nature, i.e. 'widget' not 'widgets'") + if os.path.exists(path): + return {'error': "File already exists"} - # model_name - if self.creating: - f.set_default('model_name', '{}Widget'.format(app.get_class_prefix())) - f.set_helptext('model_name', "Should be singular in nature, i.e. 'Widget' not 'Widgets'") - - # model_title* - if self.creating: - f.set_default('model_title', 'Widget') - f.set_helptext('model_title', "Human-friendly singular model title.") - f.set_default('model_title_plural', 'Widgets') - f.set_helptext('model_title_plural', "Human-friendly plural model title.") - - # description - if self.creating: - f.set_default('description', "Represents a cool widget.") - f.set_helptext('description', "Brief description of what a record in this table represents.") - - # TODO: not sure yet how to handle "save" action - # def save_create_form(self, form): - # return form.validated + self.db_handler.write_table_model(data, path) + return {'ok': True} def get_row_data(self, table): data = [] for i, column in enumerate(table['table'].columns, 1): - data.append({ 'column': column, 'sequence': i, @@ -269,8 +241,26 @@ class TableView(MasterView): if not rattail_config.production(): cls.creatable = True + cls._table_defaults(config) cls._defaults(config) + @classmethod + def _table_defaults(cls, config): + route_prefix = cls.get_route_prefix() + url_prefix = cls.get_url_prefix() + permission_prefix = cls.get_permission_prefix() + + if cls.creatable: + + # write model class to file + config.add_route('{}.write_model_file'.format(route_prefix), + '{}/write-model-file'.format(url_prefix), + request_method='POST') + config.add_view(cls, attr='write_model_file', + route_name='{}.write_model_file'.format(route_prefix), + renderer='json', + permission='{}.create'.format(permission_prefix)) + class TablesView(TableView): @@ -303,6 +293,8 @@ class TableSchema(colander.Schema): module_file = colander.SchemaNode(colander.String(), missing=colander.null) + versioned = colander.SchemaNode(colander.Bool()) + def defaults(config, **kwargs): base = globals()