Semi-finish logic for writing new table model class to file

definitely needs more polish and features, but the gist..
This commit is contained in:
Lance Edgar 2023-01-13 03:51:12 -06:00
parent fb7368993c
commit cac005f993
2 changed files with 387 additions and 62 deletions

View file

@ -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()