I'm trying to tweak the TurboGears identity management to use the
PostgreSQL users and groups instead of its own user and group tables.
The users/groups must be mapped to a "User"/"Group" class with standard
attribute names and additional properties "groups"/"users" to determine
the groups of which a user is a member / the members of a group.

I have realized it as follows, but the artificial association table
pg_groupusers feels a bit awkward. Is there an easier way?

pg_user = Table('pg_user', metadata,
    Column('usesysid', Integer, primary_key=True),
    Column('usename', String, unique=True),
    Column('usesuper', Boolean))

pg_group = Table('pg_group', metadata,
    Column('grosysid', Integer, primary_key=True),
    Column('groname', String, unique=True),
    Column('grolist', String))

pg_groupusers = select(
    [pg_user.c.usesysid, pg_group.c.grosysid],
    pg_user.c.usesysid==func.any(pg_group.c.grolist)
    ).alias('pg_groupusers')

class User(object):
    def __init__(self, user_id, user_name, is_super):
            self.user_id = user_id
            self.user_name = user_name
            self.is_super = is_super

class Group(object):
    def __init__(self, group_id, group_name, group_list):
            self.group_id = group_id
            self.group_name = group_name
            self.group_list = group_list

mapper(User, pg_user, properties={
    'user_id': pg_user.c.usesysid,
    'user_name': pg_user.c.usename,
    'is_super': pg_user.c.usesuper,
    'groups': relation(Group, secondary=pg_groupusers,
        primaryjoin=pg_user.c.usesysid==pg_groupusers.c.usesysid,
        secondaryjoin=pg_group.c.grosysid==pg_groupusers.c.grosysid)})

mapper(Group, pg_group, properties={
    'group_id': pg_group.c.grosysid,
    'group_name': pg_group.c.groname,
    'users': relation(User, secondary=pg_groupusers,
        primaryjoin=pg_group.c.grosysid==pg_groupusers.c.grosysid,
        secondaryjoin=pg_user.c.usesysid==pg_groupusers.c.usesysid)})


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sqlalchemy
-~----------~----~----~----~------~----~------~--~---

Reply via email to