Show basic column info as row grid when viewing Table

This commit is contained in:
Lance Edgar 2023-01-12 22:56:12 -06:00
parent 38f88407ff
commit fb7368993c

View file

@ -2,7 +2,7 @@
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2022 Lance Edgar
# Copyright © 2010-2023 Lance Edgar
#
# This file is part of Rattail.
#
@ -29,8 +29,11 @@ from __future__ import unicode_literals, absolute_import
import sys
import warnings
import six
import colander
from deform import widget as dfwidget
from webhelpers2.html import HTML
from tailbone.views import MasterView
@ -60,6 +63,19 @@ class TableView(MasterView):
'row_count',
]
has_rows = True
rows_pageable = False
rows_filterable = False
rows_viewable = False
row_grid_columns = [
'sequence',
'column_name',
'data_type',
'nullable',
'description',
]
def __init__(self, request):
super(TableView, self).__init__(request)
app = self.get_rattail_app()
@ -117,6 +133,7 @@ class TableView(MasterView):
}
table = model.Base.metadata.tables.get(table_name)
data['table'] = table
if table is not None:
try:
mapper = get_mapper(table)
@ -198,6 +215,52 @@ class TableView(MasterView):
# def save_create_form(self, form):
# return form.validated
def get_row_data(self, table):
data = []
for i, column in enumerate(table['table'].columns, 1):
data.append({
'column': column,
'sequence': i,
'column_name': column.name,
'data_type': six.text_type(repr(column.type)),
'nullable': column.nullable,
'description': column.doc,
})
return data
def configure_row_grid(self, g):
super(TableView, self).configure_row_grid(g)
g.sorters['sequence'] = g.make_simple_sorter('sequence')
g.set_sort_defaults('sequence')
g.set_label('sequence', "Seq.")
g.sorters['column_name'] = g.make_simple_sorter('column_name',
foldcase=True)
g.set_searchable('column_name')
g.sorters['data_type'] = g.make_simple_sorter('data_type',
foldcase=True)
g.set_searchable('data_type')
g.set_type('nullable', 'boolean')
g.sorters['nullable'] = g.make_simple_sorter('nullable')
g.set_renderer('description', self.render_column_description)
def render_column_description(self, column, field):
text = column[field]
if not text:
return
max_length = 80
if len(text) < max_length:
return text
return HTML.tag('span', title=text, c="{} ...".format(text[:max_length]))
@classmethod
def defaults(cls, config):
rattail_config = config.registry.settings.get('rattail_config')