Add basic "Review Model" step for new table wizard

This commit is contained in:
Lance Edgar 2023-01-14 23:23:21 -06:00
parent 9d2bcff96b
commit 68ed5942e6
2 changed files with 156 additions and 4 deletions

View file

@ -275,6 +275,12 @@
<b-input v-model="tableModelFile"></b-input>
</b-field>
<b-field horizontal>
<b-checkbox v-model="tableModelFileOverwrite">
Overwrite file if it exists
</b-checkbox>
</b-field>
<div class="form">
<div class="buttons">
<b-button icon-pack="fas"
@ -289,6 +295,11 @@
:disabled="writingModelFile">
{{ writingModelFile ? "Working, please wait..." : "Write model class to file" }}
</b-button>
<b-button icon-pack="fas"
icon-left="arrow-right"
@click="activeStep = 'review-model'">
Skip
</b-button>
</div>
</div>
</b-step-item>
@ -299,7 +310,87 @@
<h3 class="is-size-3 block">
Review Model
</h3>
<p class="block">TODO: review model class here</p>
<p class="block">
Model code was generated to file:
</p>
<p class="block is-family-code" style="padding-left: 3rem;">
{{ tableModelFile }}
</p>
<p class="block">
First, review that code and adjust to your liking.
</p>
<p class="block">
Next be sure to import the new model. Typically this is done
by editing the file...
</p>
<p class="block is-family-code" style="padding-left: 3rem;">
${model_dir}__init__.py
</p>
<p class="block">
...and adding a line such as:
</p>
<p class="block is-family-code" style="padding-left: 3rem;">
from .{{ tableModelFileModuleName }} import {{ tableModelName }}
</p>
<p class="block">
Once you&apos;ve done all that, the web app must be restarted.
This may happen automatically depending on your setup.
Test the model import status below.
</p>
<div class="card block">
<header class="card-header">
<p class="card-header-title">
Model Import Status
</p>
</header>
<div class="card-content">
<div class="content">
<div class="level">
<div class="level-left">
<div class="level-item">
<span v-if="!modelImported && !modelImportProblem">
import not yet attempted
</span>
<span v-if="modelImported"
class="has-text-success has-text-weight-bold">
imported okay
</span>
<span v-if="modelImportProblem"
class="has-text-danger">
import failed: {{ modelImportStatus }}
</span>
</div>
</div>
<div class="level-right">
<div class="level-item">
<b-field horizontal label="Model Class">
<b-input v-model="modelImportName"></b-input>
</b-field>
</div>
<div class="level-item">
<b-button type="is-primary"
icon-pack="fas"
icon-left="redo"
@click="modelImportTest()">
Refresh / Test Import
</b-button>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="buttons">
<b-button icon-pack="fas"
icon-left="arrow-left"
@ -309,7 +400,8 @@
<b-button type="is-primary"
icon-pack="fas"
icon-left="check"
@click="activeStep = 'write-revision'">
@click="activeStep = 'write-revision'"
:disabled="!modelImported">
Model class looks good!
</b-button>
</div>
@ -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
}
})
}
</script>
</%def>

View file

@ -183,11 +183,28 @@ class TableView(MasterView):
path = data['module_file']
if os.path.exists(path):
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):