When you need logic...
A tool to manage logical rules throughout your application. Logical rules are more powerful than permission or rule tables because they are written in python. Register a rule once and work with it throughout your app, from templates to generic view mixins. Instead of cluttering your models with rule-style and permission-style methods define those rules in rules.py and then get easy access to them in your views and templates.
Use pip to install from PyPI:
pip install django-logical-rules
Add storages to your settings.py file:
INSTALLED_APPS = (
...
'django-logical-rules',
...
)
If you want to use the messaging features, install Django messages framework.
Simply add rules.py to your app and use them throughout your app. Here’s what a rule looks like:
import logical_rules
def user_can_edit_mymodel(object, user):
"""
Confirms a user can edit a specific model
...owners only!
"""
return object.owner == user
logical_rules.site.register("user_can_edit_mymodel", user_can_edit_mymodel)
To include your models in the registry you will need to do run the autodiscover, a bit like django.contrib.admin (I generally put this in my urls.py):
import logical_rules
logical_rules.autodiscover()
Performance varies mainly around how you write your rules. Often it’s a good idea to use caching in your rules when a permission isn’t changing frequently.