fixed issue 1337, as_dict(...,field_options=True), thanks Alan
This commit is contained in:
@@ -1 +1 @@
|
|||||||
Version 2.4.1-alpha.2+timestamp.2013.02.15.17.25.19
|
Version 2.4.1-alpha.2+timestamp.2013.02.15.17.28.22
|
||||||
|
|||||||
+72
-39
@@ -7688,7 +7688,7 @@ def index():
|
|||||||
if on_define: on_define(table)
|
if on_define: on_define(table)
|
||||||
return table
|
return table
|
||||||
|
|
||||||
def as_dict(self, flat=False, sanitize=True):
|
def as_dict(self, flat=False, sanitize=True, field_options=True):
|
||||||
dbname = db_uid = uri = None
|
dbname = db_uid = uri = None
|
||||||
if not sanitize:
|
if not sanitize:
|
||||||
uri, dbname, db_uid = (self._uri, self._dbname, self._db_uid)
|
uri, dbname, db_uid = (self._uri, self._dbname, self._db_uid)
|
||||||
@@ -7707,25 +7707,29 @@ def index():
|
|||||||
tablename = str(table)
|
tablename = str(table)
|
||||||
db_as_dict["tables"].append(tablename)
|
db_as_dict["tables"].append(tablename)
|
||||||
db_as_dict["items"][tablename] = table.as_dict(flat=flat,
|
db_as_dict["items"][tablename] = table.as_dict(flat=flat,
|
||||||
sanitize=sanitize)
|
sanitize=sanitize,
|
||||||
|
field_options=field_options)
|
||||||
return db_as_dict
|
return db_as_dict
|
||||||
|
|
||||||
def as_xml(self, sanitize=True):
|
def as_xml(self, sanitize=True, field_options=True):
|
||||||
if not have_serializers:
|
if not have_serializers:
|
||||||
raise ImportError("No xml serializers available")
|
raise ImportError("No xml serializers available")
|
||||||
d = self.as_dict(flat=True, sanitize=sanitize)
|
d = self.as_dict(flat=True, sanitize=sanitize,
|
||||||
|
field_options=field_options)
|
||||||
return serializers.xml(d)
|
return serializers.xml(d)
|
||||||
|
|
||||||
def as_json(self, sanitize=True):
|
def as_json(self, sanitize=True, field_options=True):
|
||||||
if not have_serializers:
|
if not have_serializers:
|
||||||
raise ImportError("No json serializers available")
|
raise ImportError("No json serializers available")
|
||||||
d = self.as_dict(flat=True, sanitize=sanitize)
|
d = self.as_dict(flat=True, sanitize=sanitize,
|
||||||
|
field_options=field_options)
|
||||||
return serializers.json(d)
|
return serializers.json(d)
|
||||||
|
|
||||||
def as_yaml(self, sanitize=True):
|
def as_yaml(self, sanitize=True, field_options=True):
|
||||||
if not have_serializers:
|
if not have_serializers:
|
||||||
raise ImportError("No YAML serializers available")
|
raise ImportError("No YAML serializers available")
|
||||||
d = self.as_dict(flat=True, sanitize=sanitize)
|
d = self.as_dict(flat=True, sanitize=sanitize,
|
||||||
|
field_options=field_options)
|
||||||
return serializers.yaml(d)
|
return serializers.yaml(d)
|
||||||
|
|
||||||
def __contains__(self, tablename):
|
def __contains__(self, tablename):
|
||||||
@@ -8617,7 +8621,7 @@ class Table(object):
|
|||||||
if id_map and cid is not None:
|
if id_map and cid is not None:
|
||||||
id_map_self[int(line[cid])] = new_id
|
id_map_self[int(line[cid])] = new_id
|
||||||
|
|
||||||
def as_dict(self, flat=False, sanitize=True):
|
def as_dict(self, flat=False, sanitize=True, field_options=True):
|
||||||
tablename = str(self)
|
tablename = str(self)
|
||||||
table_as_dict = dict(name=tablename, items={}, fields=[],
|
table_as_dict = dict(name=tablename, items={}, fields=[],
|
||||||
sequence_name=self._sequence_name,
|
sequence_name=self._sequence_name,
|
||||||
@@ -8629,25 +8633,29 @@ class Table(object):
|
|||||||
if (field.readable or field.writable) or (not sanitize):
|
if (field.readable or field.writable) or (not sanitize):
|
||||||
table_as_dict["fields"].append(field.name)
|
table_as_dict["fields"].append(field.name)
|
||||||
table_as_dict["items"][field.name] = \
|
table_as_dict["items"][field.name] = \
|
||||||
field.as_dict(flat=flat, sanitize=sanitize)
|
field.as_dict(flat=flat, sanitize=sanitize,
|
||||||
|
options=field_options)
|
||||||
return table_as_dict
|
return table_as_dict
|
||||||
|
|
||||||
def as_xml(self, sanitize=True):
|
def as_xml(self, sanitize=True, field_options=True):
|
||||||
if not have_serializers:
|
if not have_serializers:
|
||||||
raise ImportError("No xml serializers available")
|
raise ImportError("No xml serializers available")
|
||||||
d = self.as_dict(flat=True, sanitize=sanitize)
|
d = self.as_dict(flat=True, sanitize=sanitize,
|
||||||
|
field_options=field_options)
|
||||||
return serializers.xml(d)
|
return serializers.xml(d)
|
||||||
|
|
||||||
def as_json(self, sanitize=True):
|
def as_json(self, sanitize=True, field_options=True):
|
||||||
if not have_serializers:
|
if not have_serializers:
|
||||||
raise ImportError("No json serializers available")
|
raise ImportError("No json serializers available")
|
||||||
d = self.as_dict(flat=True, sanitize=sanitize)
|
d = self.as_dict(flat=True, sanitize=sanitize,
|
||||||
|
field_options=field_options)
|
||||||
return serializers.json(d)
|
return serializers.json(d)
|
||||||
|
|
||||||
def as_yaml(self, sanitize=True):
|
def as_yaml(self, sanitize=True, field_options=True):
|
||||||
if not have_serializers:
|
if not have_serializers:
|
||||||
raise ImportError("No YAML serializers available")
|
raise ImportError("No YAML serializers available")
|
||||||
d = self.as_dict(flat=True, sanitize=sanitize)
|
d = self.as_dict(flat=True, sanitize=sanitize,
|
||||||
|
field_options=field_options)
|
||||||
return serializers.yaml(d)
|
return serializers.yaml(d)
|
||||||
|
|
||||||
def with_alias(self, alias):
|
def with_alias(self, alias):
|
||||||
@@ -9303,7 +9311,7 @@ class Field(Expression):
|
|||||||
def count(self, distinct=None):
|
def count(self, distinct=None):
|
||||||
return Expression(self.db, self.db._adapter.COUNT, self, distinct, 'integer')
|
return Expression(self.db, self.db._adapter.COUNT, self, distinct, 'integer')
|
||||||
|
|
||||||
def as_dict(self, flat=False, sanitize=True):
|
def as_dict(self, flat=False, sanitize=True, options=True):
|
||||||
|
|
||||||
attrs = ('type', 'length', 'default', 'required',
|
attrs = ('type', 'length', 'default', 'required',
|
||||||
'ondelete', 'notnull', 'unique', 'uploadfield',
|
'ondelete', 'notnull', 'unique', 'uploadfield',
|
||||||
@@ -9317,67 +9325,92 @@ class Field(Expression):
|
|||||||
|
|
||||||
SERIALIZABLE_TYPES = (int, long, basestring, dict, list,
|
SERIALIZABLE_TYPES = (int, long, basestring, dict, list,
|
||||||
float, tuple, bool, type(None))
|
float, tuple, bool, type(None))
|
||||||
|
|
||||||
def flatten(obj):
|
def flatten(obj):
|
||||||
|
newobj = None
|
||||||
if flat:
|
if flat:
|
||||||
if isinstance(obj, flatten.__class__):
|
if isinstance(obj, flatten.__class__):
|
||||||
return str(type(obj))
|
return str(type(obj))
|
||||||
elif isinstance(obj, type):
|
elif isinstance(obj, type):
|
||||||
try:
|
try:
|
||||||
obj = str(obj).split("'")[1]
|
newobj = str(obj).split("'")[1]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
obj = str(obj)
|
return str(obj)
|
||||||
elif not isinstance(obj, SERIALIZABLE_TYPES):
|
elif not isinstance(obj, SERIALIZABLE_TYPES):
|
||||||
obj = str(obj)
|
return str(obj)
|
||||||
return obj
|
elif isinstance(obj, dict):
|
||||||
|
newobj = dict()
|
||||||
|
for k, v in obj.items():
|
||||||
|
newobj[k] = flatten(v)
|
||||||
|
elif isinstance(obj, (list, tuple, set)):
|
||||||
|
newobj = [flatten(v) for v in obj]
|
||||||
|
else:
|
||||||
|
newobj = obj
|
||||||
|
return newobj
|
||||||
|
|
||||||
def filter_requires(t, r):
|
def filter_requires(t, r, options=True):
|
||||||
if sanitize and any([keyword in str(t).upper() for
|
if sanitize and any([keyword in str(t).upper() for
|
||||||
keyword in ("CRYPT", "IS_STRONG")]):
|
keyword in ("CRYPT", "IS_STRONG")]):
|
||||||
return None
|
return None
|
||||||
for k, v in r.items():
|
|
||||||
|
if not isinstance(r, dict):
|
||||||
|
if options and hasattr(r, "options"):
|
||||||
|
if callable(r.options):
|
||||||
|
r.options()
|
||||||
|
newr = r.__dict__
|
||||||
|
else:
|
||||||
|
newr = r
|
||||||
|
|
||||||
|
for k, v in newr.items():
|
||||||
if k == "other":
|
if k == "other":
|
||||||
if not isinstance(v, dict):
|
if isinstance(v, dict):
|
||||||
other = v.__dict__
|
otype, other = v.popitem()
|
||||||
else: other = v
|
else:
|
||||||
r[k] = {flatten(type(v)):
|
otype = flatten(type(v))
|
||||||
filter_requires(type(v), other)}
|
other = v
|
||||||
elif flat and (not isinstance(v, SERIALIZABLE_TYPES)):
|
newr[k] = {otype: filter_requires(otype, other,
|
||||||
r[k] = str(v)
|
options=options)}
|
||||||
return r
|
else:
|
||||||
|
newr[k] = flatten(v)
|
||||||
|
return newr
|
||||||
|
|
||||||
if isinstance(self.requires, (tuple, list, set)):
|
if isinstance(self.requires, (tuple, list, set)):
|
||||||
requires = dict([(flatten(type(r)),
|
requires = dict([(flatten(type(r)),
|
||||||
filter_requires(type(r), r.__dict__)) for
|
filter_requires(type(r), r,
|
||||||
|
options=options)) for
|
||||||
r in self.requires])
|
r in self.requires])
|
||||||
else:
|
else:
|
||||||
requires = {flatten(type(self.requires)):
|
requires = {flatten(type(self.requires)):
|
||||||
filter_requires(type(self.requires),
|
filter_requires(type(self.requires),
|
||||||
self.requires.__dict__)}
|
self.requires, options=options)}
|
||||||
|
|
||||||
d = dict(colname="%s.%s" % (self.tablename, self.name),
|
d = dict(colname="%s.%s" % (self.tablename, self.name),
|
||||||
requires=requires)
|
requires=requires)
|
||||||
d.update([(attr, flatten(getattr(self, attr))) for attr in attrs])
|
d.update([(attr, flatten(getattr(self, attr))) for attr in attrs])
|
||||||
return d
|
return d
|
||||||
|
|
||||||
def as_xml(self, sanitize=True):
|
def as_xml(self, sanitize=True, options=True):
|
||||||
if have_serializers:
|
if have_serializers:
|
||||||
xml = serializers.xml
|
xml = serializers.xml
|
||||||
else:
|
else:
|
||||||
raise ImportError("No xml serializers available")
|
raise ImportError("No xml serializers available")
|
||||||
d = self.as_dict(flat=True, sanitize=sanitize)
|
d = self.as_dict(flat=True, sanitize=sanitize,
|
||||||
|
options=options)
|
||||||
return xml(d)
|
return xml(d)
|
||||||
|
|
||||||
def as_json(self, sanitize=True):
|
def as_json(self, sanitize=True, options=True):
|
||||||
if have_serializers:
|
if have_serializers:
|
||||||
json = serializers.json
|
json = serializers.json
|
||||||
else:
|
else:
|
||||||
raise ImportError("No json serializers available")
|
raise ImportError("No json serializers available")
|
||||||
d = self.as_dict(flat=True, sanitize=sanitize)
|
d = self.as_dict(flat=True, sanitize=sanitize,
|
||||||
|
options=options)
|
||||||
return json(d)
|
return json(d)
|
||||||
|
|
||||||
def as_yaml(self, sanitize=True):
|
def as_yaml(self, sanitize=True, options=True):
|
||||||
if have_serializers:
|
if have_serializers:
|
||||||
d = self.as_dict(flat=True, sanitize=sanitize)
|
d = self.as_dict(flat=True, sanitize=sanitize,
|
||||||
|
options=options)
|
||||||
return serializers.yaml(d)
|
return serializers.yaml(d)
|
||||||
else:
|
else:
|
||||||
raise ImportError("No YAML serializers available")
|
raise ImportError("No YAML serializers available")
|
||||||
|
|||||||
Reference in New Issue
Block a user