1 # A JSON-based API(view) for your app.
2 # Most rules would look like:
3 # @jsonify.when("isinstance(obj, YourClass)")
4 # def jsonify_yourclass(obj):
5 # return [obj.val1, obj.val2]
6 # @jsonify can convert your objects to following types:
7 # lists, dicts, numbers and strings
9 from turbojson.jsonify import jsonify #, jsonify_sqlobject
10 from sercom.model import SQLObject, Rol, Usuario, Permiso
12 #@jsonify.when('isinstance(obj, SQLObject')
13 def jsonify_sqlobject(obj):
16 cls = obj.sqlmeta.soClass
17 for name in cls.sqlmeta.columns.keys():
18 if name != 'childName':
19 result[name] = getattr(obj, name)
20 while cls.sqlmeta.parentClass:
21 cls = cls.sqlmeta.parentClass
22 for name in cls.sqlmeta.columns.keys():
23 if name != 'childName':
24 result[name] = getattr(obj, name)
27 @jsonify.when('isinstance(obj, Rol)')
29 result = jsonify_sqlobject(obj)
30 result["usuarios"] = [u.usuario for u in obj.usuarios]
31 result["permisos"] = [p.nombre for p in obj.permisos]
34 @jsonify.when('isinstance(obj, Usuario)')
35 def jsonify_user(obj):
36 result = jsonify_sqlobject(obj)
37 del result['contrasenia']
38 result["roles"] = [r.nombre for r in obj.roles]
39 result["permisos"] = [p.nombre for p in obj.permisos]
42 @jsonify.when('isinstance(obj, Permiso)')
43 def jsonify_permission(obj):
44 return dict(nombre=obj.nombre, descripcion=obj.descripcion)