项目作者: JonnoFTW

项目描述 :
Simple library to integrate mongodb into your pyramid application
高级语言: Mako
项目地址: git://github.com/JonnoFTW/pyramid_mongodb2.git
创建时间: 2018-07-26T02:43:05Z
项目社区:https://github.com/JonnoFTW/pyramid_mongodb2

开源协议:MIT License

下载


Pyramid Mongodb

A simple library to integrate mongodb into your pyramid application. Comes with a debugtoolbar.

Features

  • Supports multiple databases
  • Configuration only setup
  • Integrated debugtoolbar with:
    • Shows db response times
    • explain() for cursor results
    • Connection information
    • Database and collection stats
  • Avoids recreating and closing MongoClient on every request.

Setup

  1. pip install pyramid_mongodb2

Add the following to your application’s ini file, (include pyramid_mongodb2:MongoToolbar in debugtoolbar.includes if you want to debug):

  1. [app:main]
  2. mongo_uri = mongodb://username:password@mongodb.host.com:27017/authdb
  3. mongo_dbs =
  4. foo
  5. bar
  6. baz-quux
  7. foo-test = foo
  8. pyramid.includes =
  9. pyramid_mako
  10. pyramid_debugtoolbar
  11. pyramid_mongodb2
  12. debugtoolbar.includes =
  13. pyramid_mongodb2:MongoToolbar

The code will use config.add_request_method() to add a Database object to your requests, where each database is accessible by db_database_name, as defined in your configuration.

Note: database names with hyphens in them will be converted to underscores, that is database baz-quux will be accessible by request.db_baz_quux.

When doing foo-test = foo, the mongodb database with name foo-test will be assigned to request.db_foo.
This helps when testing so that you can use a separate database for development, testing and production without
changing your application code, or if you just want to alias a database name.

In your code where you can access request, you now have the following variables:

  1. request.db
  2. request.db_foo
  3. request.db_bar
  4. request.db_baz_quux
  5. request.db_foo

request.db is the MongoClient object, should you ever need it.

In your view code, you can do this:

  1. from pyramid.view import view_config
  2. @view_config(route_name='home', renderer="templates/landing.mako")
  3. def my_view(request):
  4. return {
  5. 'some_data': request.db_foo.some_collection.find({'a': {'$gte': 5}}, {'_id': False}),
  6. 'other_data': request.db_bar.visitors.insert_one({'person': request.remote_addr}),
  7. }

Debugging

With debugging enabled, all queries will be logged in request.query_log, when the debugtoolbar is opened, you can
then view the execution time and explain() of cursor results. You can also see connection settings and stats for
databases and collections.

Screenshots

Here’s what the toolbar looks like in action:

Clicking the database or collection name will take you to the relevant section of the collections tab. Clicking the operation name will take you to its pymongo documentation.
debug1

Clicking the explain button will show you the explain() result for a cursor.
debug2
You can view detailed connection information here, clicking the field name will take you to the pymongo documentation for that field.
debug3
This page show dbstats for all connected databases used in this request and their collections.
debug4
Here we can see the use of multiple databases in a single project.
debug5