forked from niallo/pyramid_mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request niallo#2 from jvanasco/master
i standardized the integration a bit, so this package is more than a template
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
""" | ||
simplified mongodb integration | ||
1. Add two lines to app/__init__.py ( the import and the call to initialize_mongo_db ) | ||
import python_mongodb | ||
def main(global_config, **settings): | ||
## ... | ||
# Initialize mongodb , which is a subscriber | ||
python_mongodb.initialize_mongo_db( config , settings ) | ||
## ... | ||
return config.make_wsgi_app() | ||
2. in each of your envinronment.ini files, have: | ||
mongodb.use = true | ||
mongodb.uri = mongodb://localhost | ||
mongodb.name = myapp | ||
if "mongodb.use" is not "true", then it won't be configured -- so you can do your local development / tests / etc without having mongodb running ( should you want to ) | ||
""" | ||
import pymongo | ||
from gridfs import GridFS | ||
|
||
|
||
def initialize_mongo_db( config, settings ): | ||
if ( 'mongodb.use' in settings ) and ( settings['mongodb.use'] == 'true' ): | ||
conn = pymongo.Connection( settings['mongodb.uri'] ) | ||
config.registry.settings['!mongodb.conn'] = conn | ||
config.add_subscriber(add_mongo_db, 'pyramid.events.NewRequest') | ||
|
||
|
||
def add_mongo_db(event): | ||
settings = event.request.registry.settings | ||
db = settings['!mongodb.conn'][settings['mongodb.name']] | ||
event.request.mongodb = db | ||
event.request.gridfs = GridFS(db) |