Relating local tables to Users/Roles defined by sqlalchemy_driver

0 views
Skip to first unread message

pkn

unread,
Nov 18, 2009, 4:03:34 PM11/18/09
to AuthKit
Hi,

I'm exploring AuthKit for use in a Pylons project. How do I map
relations from tables defined in model/__init__.py to the model
defined in sqlalchemy_driver?

Thanks,

Paul

James Gardner

unread,
Nov 18, 2009, 8:51:59 PM11/18/09
to AuthKit
Hi Paul,

You shouldn't need to. Usually you access the authkit data through the
user manager API. This section of the Pylons Book describes it:

http://pylonsbook.com/en/1.1/simplesite-tutorial-part-3.html#authentication-and-authorization

James

Paul Nesbit

unread,
Nov 19, 2009, 2:00:39 AM11/19/09
to aut...@googlegroups.com
Hi James,

What I'm failing to understand is how to relate non-AuthKit classes to the Users class (and Groups and Roles classes) provided by sqlalchemy_driver.  E.g., How do I relate an e-mail address table to the users table?  That's not made clear in the Pylons Book and API doco.

A look through the AuthKit code, list archives, etc. suggests to me that I'd be better off creating a relationship between an emails table (class Email) than to modify AuthKit to add the attribute to the Users class.  Am I wrong in that approach?

Thanks,

  Paul

P.S.  I'm loving and am excited about Pylons.  And the Pylons Book is one of the best-formed and well-written references and guides that I've used in a long time.  Thanks for your contributions to web development.



--

You received this message because you are subscribed to the Google Groups "AuthKit" group.
To post to this group, send email to aut...@googlegroups.com.
To unsubscribe from this group, send email to authkit+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/authkit?hl=.





--
pa...@nesbit.net | 416.642.6920 | paul.nesbit (GTalk/MSN)

James Gardner

unread,
Nov 19, 2009, 3:47:33 AM11/19/09
to aut...@googlegroups.com
Hi Paul,

I see, in that case you could create your own UsersFromDatabase class
and define a new update_model() method that just returned model. I
haven't tested it but this should work:

from authkit.user.sqlalchemy_driver.sqlalchemy_05 import
UsersFromDatabase

class MyUsersFromDatabase(UsersFromDatabase):
def update_model(self, model):
return model

Then put all the model code from the original function in your Pylons
model with the changes you need. In the settings you can then write this
to use your new model:

authkit.form.authenticate.user.type = path.to.module:MyUsersFromDatabase

I think that should do it. Let me know how you get on.

Cheers,

James

P.S. Thanks for the kind comments.


Paul Nesbit wrote:
> Hi James,
>
> What I'm failing to understand is how to relate non-AuthKit classes to
> the Users class (and Groups and Roles classes) provided by
> sqlalchemy_driver. E.g., How do I relate an e-mail address table to
> the users table? That's not made clear in the Pylons Book and API doco.
>
> A look through the AuthKit code, list archives, etc. suggests to me
> that I'd be better off creating a relationship between an emails table
> (class Email) than to modify AuthKit to add the attribute to the Users
> class. Am I wrong in that approach?
>
> Thanks,
>
> Paul
>
> P.S. I'm loving and am excited about Pylons. And the Pylons Book is
> one of the best-formed and well-written references and guides that
> I've used in a long time. Thanks for your contributions to web
> development.
>
>
>
> On Wed, Nov 18, 2009 at 8:51 PM, James Gardner <ja...@pythonweb.org
> <mailto:ja...@pythonweb.org>> wrote:
>
> Hi Paul,
>
> You shouldn't need to. Usually you access the authkit data through the
> user manager API. This section of the Pylons Book describes it:
>
> http://pylonsbook.com/en/1.1/simplesite-tutorial-part-3.html#authentication-and-authorization
>
> James
>
> On Nov 18, 9:03 pm, pkn <paul.nes...@gmail.com
> <mailto:paul.nes...@gmail.com>> wrote:
> > Hi,
> >
> > I'm exploring AuthKit for use in a Pylons project. How do I map
> > relations from tables defined in model/__init__.py to the model
> > defined in sqlalchemy_driver?
> >
> > Thanks,
> >
> > Paul
>
> --
>
> You received this message because you are subscribed to the Google
> Groups "AuthKit" group.
> To post to this group, send email to aut...@googlegroups.com
> <mailto:aut...@googlegroups.com>.
> To unsubscribe from this group, send email to
> authkit+u...@googlegroups.com
> <mailto:authkit%2Bunsu...@googlegroups.com>.
> For more options, visit this group at
> http://groups.google.com/group/authkit?hl=.
>
>
>
>
>
> --
> pa...@nesbit.net <mailto:pa...@nesbit.net> | 416.642.6920 | paul.nesbit
> (GTalk/MSN)

pkn

unread,
Nov 19, 2009, 11:33:33 PM11/19/09
to AuthKit
That makes good sense. I needed to add UsersFromDatabase.update_model
(self, model) in my own update_model function for things to work.

I had difficulty working out how to map a many-to-many relation
between my Email class and the User class because I can't add
relations in a secondary mapper. I got around that by using a
backref.

Thanks,

Paul

My model/__init.py:

from sqlalchemy import *
from sqlalchemy.orm import *
from myproj.model import meta
from authkit.users.sqlalchemy_driver.sqlalchemy_05 import
UsersFromDatabase
import sys

def init_model(engine):
sm = sessionmaker(autoflush=True, autocommit=False, bind=engine)
meta.engine = engine
meta.Session = scoped_session(sm)

class MyUsersFromDatabase(UsersFromDatabase):

def update_model(self, model):
UsersFromDatabase.update_model(self, model)
metadata = meta.metadata
class Email(object):
def __init__(self, name=None):
self.name = name
def __repr__(self):
return "Email(%(name)s)" % self.__dict__
emails_table = Table(
"emails", metadata,
Column("uid", Integer, primary_key=True),
Column("email", String(255), unique=True, nullable=False),
)
users_emails_table = Table(
"users_emails",
metadata,
Column("user_uid", Integer, ForeignKey("users.uid")),
Column("email_uid", Integer, ForeignKey("emails.uid")),
)
mapper(
Email,
emails_table,
properties={
"users": relation(User, lazy=True,
secondary=users_emails_table, backref='email'),
}
)
model.Email = Email
return model
> >    http://pylonsbook.com/en/1.1/simplesite-tutorial-part-3.html#authenti...
>
> >     James
>
> >     On Nov 18, 9:03 pm, pkn <paul.nes...@gmail.com
> >     <mailto:paul.nes...@gmail.com>> wrote:
> >     > Hi,
>
> >     > I'm exploring AuthKit for use in a Pylons project.   How do I map
> >     > relations from tables defined in model/__init__.py to the model
> >     > defined in sqlalchemy_driver?
>
> >     > Thanks,
>
> >     >   Paul
>
> >     --
>
> >     You received this message because you are subscribed to the Google
> >     Groups "AuthKit" group.
> >     To post to this group, send email to aut...@googlegroups.com
> >     <mailto:aut...@googlegroups.com>.
> >     To unsubscribe from this group, send email to
> >     authkit+u...@googlegroups.com
> >     <mailto:authkit%2Bunsu...@googlegroups.com>.
> >     For more options, visit this group at
> >    http://groups.google.com/group/authkit?hl=.
>
> > --
> > p...@nesbit.net <mailto:p...@nesbit.net> | 416.642.6920 | paul.nesbit

James Gardner

unread,
Nov 20, 2009, 6:30:45 AM11/20/09
to aut...@googlegroups.com
Or you could put *all* the classes and mappers in your model and keep
none of it in the class, but either way is fine. Glad it is working.

James
Reply all
Reply to author
Forward
0 new messages