Add support for "choice" widget, for report params

also add support for default value, for a param
This commit is contained in:
Lance Edgar 2020-03-26 15:24:16 -05:00
parent 2a4832f9b9
commit 89ffbd6efc

View file

@ -41,6 +41,7 @@ from rattail.threads import Thread
from rattail.util import simple_error from rattail.util import simple_error
import colander import colander
from deform import widget as dfwidget
from mako.template import Template from mako.template import Template
from pyramid.response import Response from pyramid.response import Response
from webhelpers2.html import HTML from webhelpers2.html import HTML
@ -251,9 +252,10 @@ class ReportOutputView(ExportMasterView):
if not params: if not params:
return "" return ""
# TODO: should sort these, perhaps according to Report definition?
params = [{'key': key, 'value': value} params = [{'key': key, 'value': value}
for key, value in params.items()] for key, value in params.items()]
# TODO: should sort these according to true Report definition instead?
params.sort(key=lambda param: param['key'])
route_prefix = self.get_route_prefix() route_prefix = self.get_route_prefix()
g = grids.Grid( g = grids.Grid(
@ -342,6 +344,7 @@ class GenerateReport(View):
report_params = report.make_params(Session()) report_params = report.make_params(Session())
NODE_TYPES = { NODE_TYPES = {
bool: colander.Boolean,
datetime.date: colander.Date, datetime.date: colander.Date,
} }
@ -352,10 +355,19 @@ class GenerateReport(View):
node_type = NODE_TYPES.get(param.type, colander.String) node_type = NODE_TYPES.get(param.type, colander.String)
node = colander.SchemaNode(typ=node_type(), name=param.name) node = colander.SchemaNode(typ=node_type(), name=param.name)
# maybe setup choices, if appropriate
if param.type == 'choice':
node.widget = dfwidget.SelectWidget(
values=report.get_choices(param.name, Session()))
# allow empty value if param is optional # allow empty value if param is optional
if not param.required: if not param.required:
node.missing = colander.null node.missing = colander.null
# maybe set default value
if hasattr(param, 'default'):
node.default = param.default
schema.add(node) schema.add(node)
form = forms.Form(schema=schema, request=self.request, form = forms.Form(schema=schema, request=self.request,