From e375460932f87e73bd2156796c837cc9a96a3a4a Mon Sep 17 00:00:00 2001 From: tux091 <renaud.legac@free.fr> Date: Thu, 1 Dec 2011 12:06:51 +0100 Subject: [PATCH] Add a mechanism to transport database validator on the client side. --- modules/plugin_dbui/cfgsvc.py | 68 +++++++++++++++++++++++++++++++---- static/plugin_dbui/CHANGELOG | 4 ++- 2 files changed, 65 insertions(+), 7 deletions(-) diff --git a/modules/plugin_dbui/cfgsvc.py b/modules/plugin_dbui/cfgsvc.py index 791c3e3c..df6de8a1 100755 --- a/modules/plugin_dbui/cfgsvc.py +++ b/modules/plugin_dbui/cfgsvc.py @@ -1,13 +1,22 @@ -""" $Id$ +""" Service to determine ExtJS widget configuration parameters + from the web2py model. + Author: R. Le Gac + """ -from reportlab.lib.yaml import BaseParser __version__ = "$Revision$" from basesvc import BaseSvc from foreignfield import ForeignField +from gluon.validators import (IS_DATE_IN_RANGE, + IS_DATETIME_IN_RANGE, + IS_DECIMAL_IN_RANGE, + IS_FLOAT_IN_RANGE, + IS_INT_IN_RANGE, + IS_MATCH, + IS_LENGTH) from helper import encode_field from mapper import map_default from modifier import Widget @@ -20,7 +29,8 @@ FIELD_IN_DBFIELD = "Field %s already in the dbFields list." FTYPE_TO_XTYPE = {'boolean': {'xtype': 'checkbox'},\ 'date': {'xtype': 'datefield', 'format': 'Y-m-d'},\ 'datetime': {'xtype': 'datefield', 'format': 'Y-m-d H:i:s'},\ - 'double': {'xtype': 'numberfield'},\ + 'double': {'xtype': 'numberfield', 'decimalPrecision':2, + 'decimalSeparator': '.'},\ 'id': {'xtype': 'textfield'},\ 'integer': {'xtype': 'numberfield'},\ 'password': {'xtype': 'textfield', 'inputType': 'password'},\ @@ -38,8 +48,6 @@ SUCCESS= 'success' TOTAL = 'count' - - class CfgSvc(BaseSvc): """The configuration service which build the configuration options for the ExtJS widget. @@ -248,7 +256,10 @@ class CfgSvc(BaseSvc): # tool tip if field.comment: cfg["tipText"] = field.comment - + + # validators + cfg.update(self._get_field_validators(field)) + # hide primary key # hide field which are not readable and not writable # hide field request by the form modifier @@ -372,6 +383,51 @@ class CfgSvc(BaseSvc): return di + def _get_field_validators(self, field): + """ Determine configuration parameters to handle database validators + on the client side. + + It mainly set value for minValue, maxValue, regular expression, .... + + Return a dictionary + + """ + cfg = {} + + validators = field.requires + if not isinstance(validators, (list, tuple)): + validators = [validators] + + for vdt in validators: + + # date and time validators + if isinstance(vdt, IS_DATE_IN_RANGE): + format = '%Y-%m-%d' + cfg['minValue'] = vdt.minimum.strftime(format) + cfg['maxValue'] = vdt.maximum.strftime(format) + + elif isinstance(vdt, IS_DATETIME_IN_RANGE): + format = '%Y-%m-%d %H:%M:%S' + cfg['minValue'] = vdt.minimum.strftime(format) + cfg['maxValue'] = vdt.maximum.strftime(format) + + # number validators + elif isinstance(vdt, (IS_DECIMAL_IN_RANGE, IS_FLOAT_IN_RANGE, IS_INT_IN_RANGE)): + cfg['minValue'] = vdt.minimum + cfg['maxValue'] = vdt.maximum + + # string validator + elif isinstance(vdt, IS_MATCH): + cfg["regex"] = vdt.regex.pattern + + # others validator + elif isinstance(vdt, IS_LENGTH): + cfg["maxLength"] = vdt.maxsize + cfg["minLength"] = vdt.minsize + + return cfg + + def _is_foreign_field(self, tablename, fieldname): """ Wrapper method diff --git a/static/plugin_dbui/CHANGELOG b/static/plugin_dbui/CHANGELOG index 0cbefb00..71a5f73f 100644 --- a/static/plugin_dbui/CHANGELOG +++ b/static/plugin_dbui/CHANGELOG @@ -1,7 +1,9 @@ --------------------------------- CHANGE LOG ---------------------------------- -0.4.x (Rev) +HEAD + - Add mechanism to transport database validators on the client side. - Improve the GridModifier to modify column parameters. + - Bugs fixed 0.4.2 (Oct 11) - Migrate to git and to ExtJS 3.4 -- GitLab