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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
# -*- coding: utf-8 -
#
# This file is part of couchdbkit released under the MIT license.
# See the NOTICE for more information.
"""Pylons extension to simplify using couchdbkit with pylons. This features the
following:
* Simple configuration
* Authentication
* View synchronization
* Testing
Configuration
-------------
Add this to your ini file:
couchdb.uri = http://localhost:5984
couchdb.dbname = mydbname
cookies.secret = randomuniquestringforauth
And this into environment.py:
from couchdbkit.ext.pylons import init_from_config
init_from_config(config)
Authentication
--------------
You first need to define a User model, add this into model/user.py:
from couchdbkit import StringProperty
from couchdbkit.ext.pylons.auth.model import User as UserBase
class User(UserBase):
first_name = StringProperty()
last_name = StringProperty()
email = StringProperty()
Then add this into middleware.py:
from yourapp.model.user import User
from couchdbkit.ext.pylons.auth.basic import AuthBasicMiddleware
app = AuthBasicMiddleware(app, config, User)
NOTE: This authentication by default uses sha-256 hashing with a salt, the behaviour
can be changed by overriding methods.
Now we need the views required for authentication:
Create yourapp/_design/user/views/by_login/map.js and make it look like this:
function(doc) {
if(doc.doc_type == "User") {
emit(doc.login, doc);
}
}
And yourapp/_design/group/views/by_name/map.js:
function(doc) {
if(doc.doc_type == "Group") {
emit(doc.name, doc);
}
}
And yourapp/_design/group/views/show_permissions/map.js:
function(doc) {
if (doc.doc_type == "Group") {
for (var i = 0; i < doc.permissions.length; i++) {
emit(doc.name, doc.permissions[i].name);
}
}
}
View synchronization
--------------------
This will sync yourapp/_design to the CouchDB database described in the config.
couchdbkit has a built-in syncdb command that will automatically sync it. We
need to open up setup.py and add the command there as an entry point:
[paste.paster_command]
syncdb = couchdbkit.ext.pylons.commands:SyncDbCommand
And then add 'couchdbkit' to paster_plugins in the same file.
Syncing the database is then as simple as: paster syncdb /path/to/config.ini
Testing
-------
This will make it easier to create unit and functional tests that use couchdb
and load fixtures, this is not done yet and is TBC.
"""
from .db import init_from_config
|