Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kozea/flask-alcool
ALCOOOL
https://github.com/kozea/flask-alcool
acl flask python
Last synced: about 4 hours ago
JSON representation
ALCOOOL
- Host: GitHub
- URL: https://github.com/kozea/flask-alcool
- Owner: Kozea
- License: other
- Created: 2015-07-30T09:58:02.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-11-08T08:07:30.000Z (about 14 hours ago)
- Last Synced: 2024-11-08T09:19:20.929Z (about 13 hours ago)
- Topics: acl, flask, python
- Language: Python
- Size: 9.77 KB
- Stars: 6
- Watchers: 29
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Flask-Alcool
Flask-Alcool is a Flask extension to provide access control list like rights
management with decorators on routes. It also provide a jinja extension to
display only authorized links.## Install
```
pip install Flask-Alcool
```## Examples
### Protect routes
```python
from flask.ext.alcool import alcool, allow_if
from myproject import app@alcool
def drunk(**kwargs):
return kwargs.get('drunk', False)@alcool
def major(**kwargs):
value = kwargs.get('value', 0)
return True if value > 18 else False@app.route('/test//')
@allow_if(drunk | major)
def protected_route(value, drunk):
return "route is major or drunk or both"@app.route('/drive/')
@allow_if(~drunk)
def drive(drunk):
return "route is sober : drive safely"@app.route('/welcome//')
@allow_if(major & ~drunk)
def welcome_home(drunk, value):
return "route is major and sober"
```In real life you may want to place all alcool decorated function in a
conditions.py file then do something like:```python
import conditions as Is@app.route('/test//')
@allow_if(Is.drunk | Is.major)
def protected_route(value, drunk):
return "route is major or drunk or both"
```Available alcool operators are:
- ``a & b`` → ``a and b``
- ``a | b`` → ``a or b``
- ``a ^ b`` → ``a xor b``
- ``~ a`` → ``not a``### Display links only for authorized routes
Use this snippet to configure Jinja2:
```python
from flask.ext.alcool import Alcool
alcool = Alcool(app)
```Then, in templates you can use the new {% auth %} block:
```html+jinja
{% auth 'drive', {'drunk': false} %}
This text will be displayed
{% endauth %}
{% auth 'drive', {'drunk': true} %}
This text will not be displayed
{% endauth %}
```