Edit me on GitHub

CouchDB and Pyramid

If you want to use CouchDB (via the couchdbkit package) in Pyramid, you can use the following pattern to make your CouchDB database available as a request attribute. This example uses the starter scaffold. (This follows the same pattern as the MongoDB and Pyramid example.)

First add configuration values to your development.ini file, including your CouchDB URI and a database name (the CouchDB database name, can be anything).

1
2
3
4
 [app:main]
 # ... other settings ...
 couchdb.uri = http://localhost:5984/
 couchdb.db = mydb

Then in your __init__.py, set things up such that the database is attached to each new request:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
from pyramid.config import Configurator
from couchdbkit import *


def main(global_config, \**settings):
    """ This function returns a Pyramid WSGI application.
    """
    config = Configurator(settings=settings)
    config.registry.db = Server(uri=settings['couchdb.uri'])

    def add_couchdb(request):
        db = config.registry.db.get_or_create_db(settings['couchdb.db'])
        return db

    config.add_request_method(add_couchdb, 'db', reify=True)

    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_route('home', '/')
    config.scan()
    return config.make_wsgi_app()

Note

Configurator.add_request_method has been available since Pyramid 1.4. You can use Configurator.set_request_property for Pyramid 1.3.

At this point, in view code, you can use request.db as the CouchDB database connection. For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from pyramid.view import view_config

@view_config(route_name='home', renderer='templates/mytemplate.pt')
def my_view(request):
    """ Get info for server
    """
    return {
        'project': 'pyramid_couchdb_example',
        'info': request.db.info()
    }

Add info to home template:

1
 <p>${info}</p>

CouchDB Views

First let’s create a view for our page data in CouchDB. We will use the ApplicationCreated event and make sure our view containing our page data. For more information on views in CouchDB see Introduction to CouchDB views. In __init__.py:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from pyramid.events import subscriber, ApplicationCreated

@subscriber(ApplicationCreated)
def application_created_subscriber(event):
    registry = event.app.registry
    db = registry.db.get_or_create_db(registry.settings['couchdb.db'])

    pages_view_exists = db.doc_exist('lists/pages')
    if pages_view_exists == False:
        design_doc = {
            '_id': '_design/lists',
            'language': 'javascript',
            'views': {
                'pages': {
                    'map': '''
                        function(doc) {
                            if (doc.doc_type === 'Page') {
                                emit([doc.page, doc._id], null)
                            }
                        }
                    '''
                }
            }
        }
        db.save_doc(design_doc)

CouchDB Documents

Now we can let’s add some data to a document for our home page in a CouchDB document in our view code if it doesn’t exist:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import datetime

from couchdbkit import *

class Page(Document):
    author = StringProperty()
    page = StringProperty()
    content = StringProperty()
    date = DateTimeProperty()

@view_config(route_name='home', renderer='templates/mytemplate.pt')
def my_view(request):

    def get_data():
        return list(request.db.view('lists/pages', startkey=['home'], \
                endkey=['home', {}], include_docs=True))

    page_data = get_data()

    if not page_data:
        Page.set_db(request.db)
        home = Page(
            author='Wendall',
            content='Using CouchDB via couchdbkit!',
            page='home',
            date=datetime.datetime.utcnow()
        )
        # save page data
        home.save()
        page_data = get_data()

    doc = page_data[0].get('doc')

    return {
        'project': 'pyramid_couchdb_example',
        'info': request.db.info(),
        'author': doc.get('author'),
        'content': doc.get('content'),
        'date': doc.get('date')
    }

Then update your home template again to add your custom values:

1
2
3
4
5
 <p>
     ${author}<br />
     ${content}<br />
     ${date}<br />
 </p>

Table Of Contents

Previous topic

SQLAlchemy

Next topic

MongoDB and Pyramid