Here's some code I use to set up a Mongo connection on the root for each
request (along with some other stuff):
"""
from pyramid.authentication import AuthTktAuthenticationPolicy
from pyramid.config import Configurator
from pyramid.events import subscriber
from pyramid.events import NewRequest
from pyramid.security import Allow
from pyramid.security import Authenticated
from pyramid.security import authenticated_userid
from pyramid_beaker import session_factory_from_settings
from gridfs import GridFS
import pymongo
class API(object):
""" All 'API' objects are used by renderer_globals_factory """
def __init__(self, context, request):
self.context = context
self.request = request
class UserAPI(API):
@property
def name(self):
return authenticated_userid(self.request)
def globals_factory(system):
request, context = system['request'], system['context']
return {
'user':UserAPI(context, request),
# .. other "API" objects
}
class Root(object):
__acl__ = ((Allow, Authenticated,
'something_only_authenticated_can_do'),)
def __init__(self, request):
self.request = request
def main(global_config, **settings):
authn_policy = AuthTktAuthenticationPolicy('seekriT')
config = Configurator(settings=settings,
authentication_policy=authn_policy,
root_factory=Root)
config.scan('mypackage')
config.add_static_view('static', 'mypackage:static/',
cache_max_age=86400)
config.registry.mongo_conn = pymongo.Connection(settings['db_uri'])
session_factory = session_factory_from_settings(settings)
config.set_session_factory(session_factory)
config.set_renderer_globals_factory(globals_factory)
config.include(add_routes)
return config.make_wsgi_app()
def add_routes(config):
config.add_route('dashboard', '/')
# .. other routes ...
@subscriber(NewRequest)
def add_mongo_db(event):
reg = event.request.registry
settings = reg.settings
db_name = settings['db_name']
db = reg.mongo_conn[db_name]
event.request.db = db
event.request.fs = GridFS(db)
"""
The Mongo database is then available as request.db in every view.
Authentication/authorization is normal Pyramid stuff. The code above
sets a root object with a single ACL. You can get fancier as necessary.
- C
On Tue, 2010-12-28 at 21:49 -0800, Detectedstealth wrote:
> Does anyone know of any tutorial or basic details on integrating these
> two? Need to know basic integration along with Authentication.
>
> I don't expect code just pointed into the right direction. Starting my
> first Pyramid app.
>
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en.