From: Leandro Lucarella Date: Thu, 22 Feb 2007 18:51:21 +0000 (+0000) Subject: Arreglar jsonify. X-Git-Tag: pre-merge~126 X-Git-Url: https://git.llucax.com/z.facultad/75.52/sercom.git/commitdiff_plain/9a61fcfae96620b6b54c1823a4a2df8ef08c8d50 Arreglar jsonify. Arreglar tanto jsonificación de clases propias (las pocas que crea automáticamente TG) y el jsonify_sqlobject() que no soporta InheritableSQLObjects. Ver ticket #1307: http://trac.turbogears.org/ticket/1307 --- diff --git a/sercom/json.py b/sercom/json.py index 65838e8..c8fbc87 100644 --- a/sercom/json.py +++ b/sercom/json.py @@ -6,28 +6,40 @@ # @jsonify can convert your objects to following types: # lists, dicts, numbers and strings -from turbojson.jsonify import jsonify +from turbojson.jsonify import jsonify #, jsonify_sqlobject +from sercom.model import SQLObject, Rol, Usuario, Permiso -from turbojson.jsonify import jsonify_sqlobject -from sercom.model import User, Group, Permission +#@jsonify.when('isinstance(obj, SQLObject') +def jsonify_sqlobject(obj): + result = {} + result['id'] = obj.id + cls = obj.sqlmeta.soClass + for name in cls.sqlmeta.columns.keys(): + if name != 'childName': + result[name] = getattr(obj, name) + while cls.sqlmeta.parentClass: + cls = cls.sqlmeta.parentClass + for name in cls.sqlmeta.columns.keys(): + if name != 'childName': + result[name] = getattr(obj, name) + return result -@jsonify.when('isinstance(obj, Group)') -def jsonify_group(obj): - result = jsonify_sqlobject( obj ) - result["users"] = [u.user_name for u in obj.users] - result["permissions"] = [p.permission_name for p in obj.permissions] +@jsonify.when('isinstance(obj, Rol)') +def jsonify_rol(obj): + result = jsonify_sqlobject(obj) + result["usuarios"] = [u.usuario for u in obj.usuarios] + result["permisos"] = [p.nombre for p in obj.permisos] return result -@jsonify.when('isinstance(obj, User)') +@jsonify.when('isinstance(obj, Usuario)') def jsonify_user(obj): - result = jsonify_sqlobject( obj ) - del result['password'] - result["groups"] = [g.group_name for g in obj.groups] - result["permissions"] = [p.permission_name for p in obj.permissions] + result = jsonify_sqlobject(obj) + del result['contrasenia'] + result["roles"] = [r.nombre for r in obj.roles] + result["permisos"] = [p.nombre for p in obj.permisos] return result -@jsonify.when('isinstance(obj, Permission)') +@jsonify.when('isinstance(obj, Permiso)') def jsonify_permission(obj): - result = jsonify_sqlobject( obj ) - result["groups"] = [g.group_name for g in obj.groups] - return result + return dict(nombre=obj.nombre, descripcion=obj.descripcion) + diff --git a/sercom/model.py b/sercom/model.py index dff54ae..d06a570 100644 --- a/sercom/model.py +++ b/sercom/model.py @@ -194,10 +194,12 @@ class Usuario(InheritableSQLObject, ByObject): #{{{ def _get_permissions(self): # para identity perms = set() - for g in self.groups: - perms.update(g.permisos) + for r in self.roles: + perms.update(r.permisos) return perms + _get_permisos = _get_permissions + def _set_password(self, cleartext_password): # para identity self.contrasenia = encryptpw(cleartext_password)