开发者

Using repoze.what with declarative syntax in pylons

I'm writing an application in Pylons and I want to add an authorization scheme. I've chosen repoze.what. I followed the tutorial from Pylons cookbook:

http://wiki.pylonshq.com/display/pylonscookbook/Authorization+with+repoze.what

The problem is that in lib/auth.py I need to include models for User, Group and Permission. Using declarative base in model, it gives me an error when I want to deploy:

sqlalchemy.exc.UnboundExecutionError: No engine is bound to this Table's MetaData.
Pass a开发者_开发百科n engine to the Table via autoload_with=<someengine>, or associate the MetaData
with an engine via metadata.bind=<someengine>

I found similar problem here:

SQLAlchemy declarative syntax with autoload (reflection) in Pylons

I have all the models for authorization declared in a separate file from __init__.py. I also followed all indications from above question but there is still something wrong. Anyone found a solution?


I think I found a solution. I just create an engine in the module that contains models for authorization and I bind it to the metadata. I have no idea why init_model does not do this on its own. So this is not a problem of declarative syntax. Sorry for inconvenience.


In the model __init__.py you need to bind the engine to the session.

def init_model(engine):
    """Call me before using any of the tables or classes in the model"""
    ## Reflected tables must be defined and mapped here
    #global reflected_table
    #reflected_table = sa.Table("Reflected", meta.metadata, autoload=True,
    #                           autoload_with=engine)
    #orm.mapper(Reflected, reflected_table)

    session = orm.sessionmaker(bind=engine, autoflush=True, autocommit=False)
    meta.metadata.bind = engine
    meta.engine = engine
    meta.Session = orm.scoped_session(session)

Resources: http://docs.pylonsproject.org/projects/pylons_framework/dev/advanced_models.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜