Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/caxap/rest_condition
Complex permissions flow for django-rest-framework (http://django-rest-framework.org).
https://github.com/caxap/rest_condition
Last synced: 30 days ago
JSON representation
Complex permissions flow for django-rest-framework (http://django-rest-framework.org).
- Host: GitHub
- URL: https://github.com/caxap/rest_condition
- Owner: caxap
- License: mit
- Created: 2013-12-04T14:15:04.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2019-01-04T15:23:56.000Z (almost 6 years ago)
- Last Synced: 2024-10-30T20:53:14.239Z (about 1 month ago)
- Language: Python
- Homepage:
- Size: 16.6 KB
- Stars: 280
- Watchers: 8
- Forks: 29
- Open Issues: 7
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
- awesome-django-rest-framework - rest_condition
README
rest\_condition
===============Complex permissions flow for `django-rest-framework`_.
Installation
------------The easiest way to install the latest version is by using
pip/easy\_install to pull it from PyPI:::
pip install rest_condition
You may also use Git to clone the repository from Github and install it
manually:::
git clone https://github.com/caxap/rest_condition.git
python setup.py installExample
-------.. code:: python
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.permissions import BasePermission
from rest_condition import ConditionalPermission, C, And, Or, Notclass Perm1(BasePermission):
def has_permission(self, request, view):
# You permissions check here
return Trueclass Perm2(BasePermission):
def has_permission(self, request, view):
# You permissions check here
return False# Example of possible expressions
expr1 = Or(Perm1, Perm2) # same as: C(Perm1) | Perm2
expr2 = And(Perm1, Perm2) # same as: C(Perm1) & Perm2
expr3 = Not(Perm1) # same as: ~C(Perm1)
expr4 = And(Not(Perm1), Or(Perm1, Not(Perm2))) # same as: ~C(Perm1) & (C(Perm1) | ~C(Perm2))# Using expressions in API views
class ExampleView(APIView):
permission_classes = [Or(And(Perm1, Perm2), Not(Perm2)), ]
# Or just simple:
# permission_classes = [C(Perm1) & Perm2 | ~C(Perm2), ]def get(self, request, format=None):
content = {'status': 'request was permitted'}
return Response(content)class OtherExampleView(ExampleView):
# Using ConditionalPermission class
permission_classes = [ConditionalPermission, ]
permission_condition = (C(Perm1) & Perm2) | (~C(Perm1) & ~C(Perm2))License
-------The MIT License (MIT)
Contributed by `Max Kamenkov`_
.. _django-rest-framework: http://django-rest-framework.org/
.. _Maxim Kamenkov: https://github.com/caxap/