GithubHelp home page GithubHelp logo

pombredanne / rest_condition Goto Github PK

View Code? Open in Web Editor NEW

This project forked from caxap/rest_condition

0.0 0.0 0.0 172 KB

Complex permissions flow for django-rest-framework (http://django-rest-framework.org).

License: MIT License

Python 100.00%

rest_condition's Introduction

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 install

Example

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, Not


class Perm1(BasePermission):

   def has_permission(self, request, view):
        # You permissions check here
        return True

       
class 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 Maxim Kamenkov, PandaDoc.com

rest_condition's People

Contributors

caxap avatar skade avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.