Package couchdbkit :: Package ext :: Package pylons
[hide private]
[frames] | no frames]

Source Code for Package couchdbkit.ext.pylons

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