From 9a61fcfae96620b6b54c1823a4a2df8ef08c8d50 Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Thu, 22 Feb 2007 18:51:21 +0000 Subject: [PATCH] =?utf8?q?Arreglar=20jsonify.=20Arreglar=20tanto=20jsonifi?= =?utf8?q?caci=C3=B3n=20de=20clases=20propias=20(las=20pocas=20que=20crea?= =?utf8?q?=20autom=C3=A1ticamente=20TG)=20y=20el=20jsonify=5Fsqlobject()?= =?utf8?q?=20que=20no=20soporta=20InheritableSQLObjects.=20Ver=20ticket=20?= =?utf8?q?#1307:=20http://trac.turbogears.org/ticket/1307?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- sercom/json.py | 46 +++++++++++++++++++++++++++++----------------- sercom/model.py | 6 ++++-- 2 files changed, 33 insertions(+), 19 deletions(-) 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) -- 2.43.0