json field in DAL, thanks Alan
This commit is contained in:
@@ -1 +1 @@
|
||||
Version 2.4.1-alpha.2+timestamp.2013.01.08.09.16.57
|
||||
Version 2.4.1-alpha.2+timestamp.2013.01.08.09.22.34
|
||||
|
||||
+351
-278
File diff suppressed because it is too large
Load Diff
@@ -20,6 +20,10 @@ except ImportError:
|
||||
import contrib.simplejson as json_parser # fallback to pure-Python module
|
||||
|
||||
|
||||
def loads_json(o):
|
||||
# deserialize a json string
|
||||
return json_parser.loads(o)
|
||||
|
||||
def custom_json(o):
|
||||
if hasattr(o, 'custom_json') and callable(o.custom_json):
|
||||
return o.custom_json()
|
||||
|
||||
+21
-3
@@ -30,6 +30,7 @@ from utils import md5_hash
|
||||
from validators import IS_EMPTY_OR, IS_NOT_EMPTY, IS_LIST_OF, IS_DATE, \
|
||||
IS_DATETIME, IS_INT_IN_RANGE, IS_FLOAT_IN_RANGE, IS_STRONG
|
||||
|
||||
import serializers
|
||||
import datetime
|
||||
import urllib
|
||||
import re
|
||||
@@ -39,6 +40,9 @@ import inspect
|
||||
import settings
|
||||
is_gae = settings.global_settings.web2py_runtime_gae
|
||||
|
||||
|
||||
|
||||
|
||||
table_field = re.compile('[\w_]+\.[\w_]+')
|
||||
widget_class = re.compile('^\w*')
|
||||
|
||||
@@ -164,11 +168,9 @@ class TimeWidget(StringWidget):
|
||||
class DateWidget(StringWidget):
|
||||
_class = 'date'
|
||||
|
||||
|
||||
class DatetimeWidget(StringWidget):
|
||||
_class = 'datetime'
|
||||
|
||||
|
||||
class TextWidget(FormWidget):
|
||||
_class = 'text'
|
||||
|
||||
@@ -184,6 +186,22 @@ class TextWidget(FormWidget):
|
||||
attr = cls._attributes(field, default, **attributes)
|
||||
return TEXTAREA(**attr)
|
||||
|
||||
class JSONWidget(FormWidget):
|
||||
_class = 'json'
|
||||
|
||||
@classmethod
|
||||
def widget(cls, field, value, **attributes):
|
||||
"""
|
||||
generates a TEXTAREA for JSON notation.
|
||||
|
||||
see also: :meth:`FormWidget.widget`
|
||||
"""
|
||||
if not isinstance(value, basestring):
|
||||
if value is not None:
|
||||
value = serializers.json(value)
|
||||
default = dict(value=value)
|
||||
attr = cls._attributes(field, default, **attributes)
|
||||
return TEXTAREA(**attr)
|
||||
|
||||
class BooleanWidget(FormWidget):
|
||||
_class = 'boolean'
|
||||
@@ -235,7 +253,6 @@ class OptionsWidget(FormWidget):
|
||||
raise SyntaxError(
|
||||
'widget cannot determine options of %s' % field)
|
||||
opts = [OPTION(v, _value=k) for (k, v) in options]
|
||||
|
||||
return SELECT(*opts, **attr)
|
||||
|
||||
|
||||
@@ -843,6 +860,7 @@ class SQLFORM(FORM):
|
||||
widgets = Storage(dict(
|
||||
string=StringWidget,
|
||||
text=TextWidget,
|
||||
json=JSONWidget,
|
||||
password=PasswordWidget,
|
||||
integer=IntegerWidget,
|
||||
double=DoubleWidget,
|
||||
|
||||
@@ -21,6 +21,16 @@ import unicodedata
|
||||
from cStringIO import StringIO
|
||||
from utils import simple_hash, web2py_uuid, DIGEST_ALG_BY_SIZE
|
||||
|
||||
JSONErrors = (NameError, TypeError, ValueError, AttributeError,
|
||||
KeyError)
|
||||
try:
|
||||
import json as simplejson
|
||||
except ImportError:
|
||||
from gluon.contrib import simplejson
|
||||
from gluon.contrib.simplejson.decoder import JSONDecodeError
|
||||
JSONErrors += (JSONDecodeError,)
|
||||
|
||||
|
||||
__all__ = [
|
||||
'CLEANUP',
|
||||
'CRYPT',
|
||||
@@ -53,6 +63,7 @@ __all__ = [
|
||||
'IS_UPLOAD_FILENAME',
|
||||
'IS_UPPER',
|
||||
'IS_URL',
|
||||
'IS_JSON',
|
||||
]
|
||||
|
||||
try:
|
||||
@@ -300,6 +311,30 @@ class IS_LENGTH(Validator):
|
||||
return (value, translate(self.error_message)
|
||||
% dict(min=self.minsize, max=self.maxsize))
|
||||
|
||||
class IS_JSON(Validator):
|
||||
"""
|
||||
example::
|
||||
INPUT(_type='text', _name='name',
|
||||
requires=IS_JSON(error_message="This is not a valid json input")
|
||||
|
||||
>>> IS_JSON()('{"a": 100}')
|
||||
('{"a": 100}', None)
|
||||
|
||||
>>> IS_JSON()('spam1234')
|
||||
('spam1234', 'invalid json')
|
||||
"""
|
||||
|
||||
def __init__(self, error_message='invalid json'):
|
||||
self.error_message = error_message
|
||||
|
||||
def __call__(self, value):
|
||||
try:
|
||||
simplejson.loads(value)
|
||||
return (value, None)
|
||||
except JSONErrors:
|
||||
pass
|
||||
return (value, translate(self.error_message))
|
||||
|
||||
|
||||
class IS_IN_SET(Validator):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user