Add specific data type options for new table entry form

including basic FK / relationship support
This commit is contained in:
Lance Edgar 2023-01-16 22:50:51 -06:00
parent 98fa6eea05
commit e4c2336659
2 changed files with 166 additions and 21 deletions

View file

@ -31,6 +31,7 @@ import sys
import warnings
import six
from sqlalchemy_utils import get_mapper
from rattail.util import simple_error
@ -122,8 +123,6 @@ class TableView(MasterView):
f.remove('versioned')
def get_instance(self):
from sqlalchemy_utils import get_mapper
model = self.model
table_name = self.request.matchdict['table_name']
@ -204,6 +203,9 @@ class TableView(MasterView):
branch_name = None
kwargs['branch_name'] = branch_name
kwargs['existing_tables'] = [{'name': table.name}
for table in model.Base.metadata.sorted_tables]
kwargs['model_dir'] = (os.path.dirname(model.__file__)
+ os.sep)
@ -212,6 +214,7 @@ class TableView(MasterView):
def write_model_file(self):
data = self.request.json_body
path = data['module_file']
model = self.model
if os.path.exists(path):
if data['overwrite']:
@ -219,6 +222,23 @@ class TableView(MasterView):
else:
return {'error': "File already exists"}
for column in data['columns']:
if column['data_type']['type'] == '_fk_uuid_' and column['relationship']:
name = column['relationship']
table = model.Base.metadata.tables[column['data_type']['reference']]
try:
mapper = get_mapper(table)
except ValueError:
reference_model = table.name.capitalize()
else:
reference_model = mapper.class_.__name__
column['relationship'] = {
'name': name,
'reference_model': reference_model,
}
self.db_handler.write_table_model(data, path)
return {'ok': True}