Skip to content
Snippets Groups Projects
Commit e3754609 authored by tux091's avatar tux091
Browse files

Add a mechanism to transport database validator on the client side.

parent bb97c65f
No related branches found
No related tags found
No related merge requests found
""" $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
......
--------------------------------- 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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment