On Thu, 2011-01-27 at 11:47 -0800, Seth wrote:
> I've bumped into an issue with using mongoengine and pyramid when
> mongoengine needs to authenticate to mongodb. My app can't connect to
> the database when using the paster server, although all the tests run
> fine.
> 
> 
> After a little googling, I came across this discussion which describes
> issues with mongoengine's global connection object and suggests
> wrapping the connection inside middleware.
> 
> 
> What would the suggested approach be in Pyramid? Would it be
> recommended to go the middleware route or would it be more "pyramidic"
> to add this sort of thing to the "new request" event?

FTR, I don't use mongoengine, but I do use Mongo with Pyramid.  I set up
a database in a new request subscriber.  Here's my __init__.py:

from pyramid.authentication import AuthTktAuthenticationPolicy
from pyramid.config import Configurator
from pyramid.events import subscriber
from pyramid.interfaces import INewRequest
from pyramid.security import Allow
from pyramid.security import Authenticated

from pyramid_beaker import session_factory_from_settings

import deform
from pkg_resources import resource_filename

from gridfs import GridFS
import pymongo

from gd2 import rglobals
from gd2.son import ColanderNullTransformer
from gd2.son import DecimalTransformer
from gd2.util import configure_mailer

class Root(object):
    __acl__ = ((Allow, Authenticated, 'add_vendor'),)
    def __init__(self, request):
        self.request = request

def main(global_config, **settings):
    authn_policy = AuthTktAuthenticationPolicy('klksi')
    configure_mailer(settings)

    config = Configurator(settings=settings,
                          authentication_policy=authn_policy,
                          root_factory=Root)
    config.scan('gd2')

    config.override_resource('deform:static/', 'gd2:static/')
    config.add_static_view('gstatic', 'gd2:static/',
cache_max_age=86400)
    config.add_static_view('dstatic', 'deform:static/',
cache_max_age=86400)

    db_uri = settings['db_uri']

    conn = pymongo.Connection(db_uri)
    config.registry.settings['db_conn'] = conn

    deform_templates = resource_filename('deform', 'templates')
    gd2_widget_templates = resource_filename('gd2.views',
'templates/widget')
    search_path = (gd2_widget_templates, deform_templates)
    deform.Form.set_zpt_renderer(search_path)

    session_factory = session_factory_from_settings(settings)
    config.set_session_factory(session_factory)

config.set_renderer_globals_factory(rglobals.default_globals_factory)

    config.include(add_routes)

    return config.make_wsgi_app()

def add_routes(config):
    config.add_route('dashboard', '/')
    # .. more routes ..

@subscriber(INewRequest)
def add_mongo_db(event):
    settings = event.request.registry.settings
    db = settings['db_conn'][settings['db_name']]
    db.add_son_manipulator(ColanderNullTransformer())
    db.add_son_manipulator(DecimalTransformer())
    event.request.db = db
    event.request.fs = GridFS(db)
    




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

Reply via email to