Clone wiki

repoze.what.plugins.mongodb / Home

Welcome

This project provides repoze.what adapter plugins so that it can be used with MongoDB.

Note: I have only tested this using mongokit, so file a new bug if you run into any problems.

Installation

You can install this from PyPi using pip or easy_install:

pip install repoze.what.plugins.mongodb

Usage

import pymongo

from repoze.what.plugins.mongodb.adapters import MongoGroupAdapter, MongoPermissionAdapter

conncetion = pymongo.Connection('localhost')
db = connection['db_name']

groupadapter = MongoGroupAdapter(db)
permissionadapter = MongoPermissionAdapter(db)

Translations

The adapters provide the ability to set translations for things like document names and field names. Here is an example on how to set translations:

groupadapter.translations['userdoc'] = 'users'
groupadapter.translations['groupsname'] = 'groupname'
...

Valid keys for the translations are:

  • username (Default: 'username'): The field containing the user's username.
  • usergroups (Default: 'groups'): The field containing the groups of an individual user.
  • grouppermissions (Default: 'permissions'): The field containing the permissions of an individual group.
  • permissionsname (Default: 'name'): The field containing the permission names.
  • groupsname (Default: 'name'): The field containing the group names.
  • groupdoc (Default: 'auth.groups'): The document containing the groups.
  • permissiondoc (Default: 'auth.permissions'): The document containing the permissions.
  • userdoc (Default: 'auth.users'): The document containing the users.

Sample Documents

The following are sample MongoKit documents:

from mongokit import *

class Permission(Document):
    structure = {
        'name': unicode,
    }
    
    use_dot_notation = True
    required_fields = ['name']

class Group(Document):
    structure = {
        'name': unicode,
        'permissions': [Permission],
    }
    
    use_dot_notation = True
    required_fields = ['name']

class User(Document):
    structure = {
        'username': unicode,
        'email': unicode,
        'password': unicode,
        'fullname': unicode,
        'active': bool,
        'groups': [Group],
        'created': datetime.datetime,
    }
    
    use_dot_notation = True
    required_fields = ['username', 'email', 'password', 'active']
    default_values = {'created': datetime.datetime.utcnow}

In the User class, you will also need a method to set the password so it isn't stored in plain text.

Updated