Home

About

An expressive and concise mini permission module for Django views.

This module consists of 2 core components: P class and permission_required decorator. It supports bitwise operators AND , OR , XOR and invert ' ~ ' for constructing complex permission checking.

This is inspired by Q of django.db.

Examples

Senario 1:
Only family member can add new family member

# permissions.py
def is_family_member(request, family_id):
    return request.user.is_family_member(family_id)


# views.py
from permissions import *
from pobject import P
from pobject.decorators import permission_required

@permission_required('', is_family_member)
def add_family_member(request, family_id):
    # adds a family member and returns response


If the current user is not a family member, an HttpForbidden will be raised. Otherwise, the view will be executed.

Senario 2:
Only parent can delete a family member

# permissions.py
def is_father(request, family_id, member_id):
    return request.user.is_father(family_id)

def is_mother(request, family_id, member_id):
    return request.user.is_father(family_id)


# views.py
from permissions import *
from pobject import P
from pobject.decorators import permission_required

@permission_required('', P(is_father) | P(is_mother))
def delete_family_member(request, family_id, member_id):
    # deletes family member member_id and returns a response


Senario 3:
Only family members can make announcement

# permissions.py
def is_child(request, family_id):
    return request.user.is_child(family_id)

def is_neighbor(request, family_id):
    return request.user.is_neighbor(family_id)


# views.py
from permissions import *
from pobject import P
from pobject.decorators import permission_required

@permission_required('', (P(is_father) | P(is_mother) | P(is_child)) & ~(neighbor))
def announce(request, family_id):
    # makes announcement and returns a response


As you can see, it is possible to create permission checking of any level of complexity using bitwise operators similar to Q object in django.db.

The supported bitwise operators are summarized follow:

  • & for AND
  • | for OR
  • ^ for XOR
  • ~ negates a P object

More examples can be found in tests.py.

Dependencies

  1. exceptional_middelware

Installation

There is no official release yet since I am not sure how well it works or even accepted by the community. But if you decide to give it a try, do the following:

$ hg clone https://phuihock@bitbucket.org/phuihock/django-pobject
$ cd django-pobject
$ python bootstrap.py -d
$ bin/buildout
$ bin/django test pobject


I hope you like it!

Updated

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.