]> git.llucax.com Git - software/sercom.git/blob - sercom/json.py
Import inicial.
[software/sercom.git] / sercom / json.py
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
8
9 from turbojson.jsonify import jsonify
10
11 from turbojson.jsonify import jsonify_sqlobject
12 from sercom.model import User, Group, Permission
13
14 @jsonify.when('isinstance(obj, Group)')
15 def jsonify_group(obj):
16     result = jsonify_sqlobject( obj )
17     result["users"] = [u.user_name for u in obj.users]
18     result["permissions"] = [p.permission_name for p in obj.permissions]
19     return result
20
21 @jsonify.when('isinstance(obj, User)')
22 def jsonify_user(obj):
23     result = jsonify_sqlobject( obj )
24     del result['password']
25     result["groups"] = [g.group_name for g in obj.groups]
26     result["permissions"] = [p.permission_name for p in obj.permissions]
27     return result
28
29 @jsonify.when('isinstance(obj, Permission)')
30 def jsonify_permission(obj):
31     result = jsonify_sqlobject( obj )
32     result["groups"] = [g.group_name for g in obj.groups]
33     return result