Search code examples
pythonpython-3.xmongodbpymongopyramid

Can't get Pyramid to recognize Mongo


I am trying to get the Pyramid Web framework to handle a request using Mongo but I am a relative newbie to both. I cannot get my view to recognize a database attached to a request.

In development.ini:

###
# configure mongodb
###
mongo_uri = mongodb://localhost:27017/nomad

The __init__.py imports and main function:

# imports for Mongodb
from urllib.parse import urlparse
from gridfs import GridFS
from pymongo import MongoClient


def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    config = Configurator(settings=settings)
    init_includes(config)
    init_routing(config)

    db_url = urlparse(settings['mongo_uri'])
    config.registry.db = MongoClient(
        host=db_url.hostname,
        port=db_url.port,
    )

    def add_db(request):
        db = config.registry.db[db_url.path[1:]]
        if db_url.username and db_url.password:
            db.authenticate(db_url.username, db_url.password)
        return db

    def add_fs(request):
        return GridFS(request.db)

    config.add_request_method(add_db, 'db', reify=True)
    config.add_request_method(add_fs, 'fs', reify=True)
    config.scan()
    return config.make_wsgi_app()

In jobscontroller.py, which is the handler view making the request:

import pyramid_handlers

from nomad.controllers.base_controller import BaseController

class JobsController(BaseController):

    @pyramid_handlers.action(renderer='templates/jobs/index.pt')
    def index(request):
        all_jobs = request.db['jobs'].find()
        return {'all_jobs': all_jobs}

I get an error:

all_jobs = request.db['jobs'].find()
AttributeError: 'JobsController' object has no attribute 'db'

I am using Pyramid handlers to manage routing and views, and I know that all of this works because all my routes resolve and deliver web pages. It's only the jobs controller that's funky, and only after I tried adding that request.db call.

Can someone help me understand what's going on?


Solution

  • You're not referring to the request - you're referring to the object itself (usually named self, but you have named it request - which would work if it was just a function and not a method on an object). Since you're inside an object of a class, the first parameter is always the object itself:

    class JobsController(BaseController):
        @pyramid_handlers.action(renderer='templates/jobs/index.pt')
        def index(self, request):
            all_jobs = request.db['jobs'].find()
            return {'all_jobs': all_jobs}