From 68ed5942e650423eb74e8dc8c4658952ea288ff8 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sat, 14 Jan 2023 23:23:21 -0600 Subject: [PATCH] Add basic "Review Model" step for new table wizard --- tailbone/templates/tables/create.mako | 132 +++++++++++++++++++++++++- tailbone/views/tables.py | 28 +++++- 2 files changed, 156 insertions(+), 4 deletions(-) diff --git a/tailbone/templates/tables/create.mako b/tailbone/templates/tables/create.mako index 4d46273a..9cf4a112 100644 --- a/tailbone/templates/tables/create.mako +++ b/tailbone/templates/tables/create.mako @@ -275,6 +275,12 @@ + + + Overwrite file if it exists + + +
{{ writingModelFile ? "Working, please wait..." : "Write model class to file" }} + + Skip +
@@ -299,7 +310,87 @@

Review Model

-

TODO: review model class here

+ +

+ Model code was generated to file: +

+ +

+ {{ tableModelFile }} +

+ +

+ First, review that code and adjust to your liking. +

+ +

+ Next be sure to import the new model. Typically this is done + by editing the file... +

+ +

+ ${model_dir}__init__.py +

+ +

+ ...and adding a line such as: +

+ +

+ from .{{ tableModelFileModuleName }} import {{ tableModelName }} +

+ +

+ Once you've done all that, the web app must be restarted. + This may happen automatically depending on your setup. + Test the model import status below. +

+ +
+
+

+ Model Import Status +

+
+
+
+
+
+ +
+ + import not yet attempted + + + imported okay + + + import failed: {{ modelImportStatus }} + +
+
+
+
+ + + +
+
+ + Refresh / Test Import + +
+
+
+
+
+
+
+ @click="activeStep = 'write-revision'" + :disabled="!modelImported"> Model class looks good!
@@ -515,11 +607,17 @@ } ThisPageData.tableModelFile = '${model_dir}widget.py' + ThisPageData.tableModelFileOverwrite = false ThisPageData.writingModelFile = false ThisPage.methods.writeModelFile = function() { this.writingModelFile = true + this.modelImportName = this.tableModelName + this.modelImported = false + this.modelImportStatus = "import not yet attempted" + this.modelImportProblem = false + let url = '${url('{}.write_model_file'.format(route_prefix))}' let params = { branch_name: this.tableBranch, @@ -529,8 +627,9 @@ model_title_plural: this.tableModelTitlePlural, description: this.tableDescription, versioned: this.tableVersioned, - module_file: this.tableModelFile, columns: this.tableColumns, + module_file: this.tableModelFile, + overwrite: this.tableModelFileOverwrite, } this.submitForm(url, params, response => { this.writingModelFile = false @@ -540,6 +639,33 @@ }) } + ThisPageData.modelImportName = '${rattail_app.get_class_prefix()}Widget' + ThisPageData.modelImportStatus = "import not yet attempted" + ThisPageData.modelImported = false + ThisPageData.modelImportProblem = false + + ThisPage.computed.tableModelFileModuleName = function() { + let path = this.tableModelFile + path = path.replace(/^.*\//, '') + path = path.replace(/\.py$/, '') + return path + } + + ThisPage.methods.modelImportTest = function() { + let url = '${url('{}.check_model'.format(route_prefix))}' + let params = {model_name: this.modelImportName} + this.submitForm(url, params, response => { + if (response.data.problem) { + this.modelImportProblem = true + this.modelImported = false + this.modelImportStatus = response.data.problem + } else { + this.modelImportProblem = false + this.modelImported = true + } + }) + } + diff --git a/tailbone/views/tables.py b/tailbone/views/tables.py index 196e70f5..d398733c 100644 --- a/tailbone/views/tables.py +++ b/tailbone/views/tables.py @@ -183,11 +183,28 @@ class TableView(MasterView): path = data['module_file'] if os.path.exists(path): - return {'error': "File already exists"} + if data['overwrite']: + os.remove(path) + else: + return {'error': "File already exists"} self.db_handler.write_table_model(data, path) return {'ok': True} + def check_model(self): + model = self.model + data = self.request.json_body + model_name = data['model_name'] + + if not hasattr(model, model_name): + return {'ok': True, + 'problem': "class not found in primary model contents", + 'model': self.model.__name__} + + # TODO: probably should inspect closer before assuming ok..? + + return {'ok': True} + def get_row_data(self, table): data = [] for i, column in enumerate(table['table'].columns, 1): @@ -261,6 +278,15 @@ class TableView(MasterView): renderer='json', permission='{}.create'.format(permission_prefix)) + # check model + config.add_route('{}.check_model'.format(route_prefix), + '{}/check-model'.format(url_prefix), + request_method='POST') + config.add_view(cls, attr='check_model', + route_name='{}.check_model'.format(route_prefix), + renderer='json', + permission='{}.create'.format(permission_prefix)) + class TablesView(TableView):