Create simple 'if this then that' style rules in your Django application. Comes with a bunch of ready to use actions and conditions, but is also easily extensible and allows model specific actions/conditions.
Project description
Create simple ‘if this then that’ style rules in your Django application. Comes with a bunch of ready-to-use actions and conditions, but is also easily extensible and allows model specific actions/conditions.
Conditioner helps you create simple rules that consist of a condition (if this), and an action (then that). It’s currently a work in progress, but we successfully use it in production with custom conditions and actions that allow us to send ‘reminder’ emails to users before their license expires.
It was created to ‘scratch an itch’, and for the foreseeable future we will prioritise implementing use-cases needed by us as. The overall goal, however, is to create a set of useful actions and conditions that could be applied to a number of different scenarios.
Oh, and shout-out to django-polymorphic without which this whole thing would be much harder to do.
Installation
>From PyPI:
$ pip install django-conditioner
Usage
If you want to use the already implemented actions and conditions then all you need to do is install the package, add conditioner to your list of INSTALLED_APPS and run $ python manage.py migrate.
You should then see a Conditioner section with a Rule child in Django Admin. Adding a new one should be pretty self-explanatory.
Advanced usage
Actions and conditions types
Adding new actions and/or conditions
One of the main goals was to make conditioner as flexible as possible and make adding new actions/conditions as easy as possible.
Creating the action
All actions need to inherit from BaseAction and implement run_action(). Model generic actions should set model_specific to True, model specific actions should set it to return the needed model class. If your action is model specific then model instance will be passed to run_action() method as instance named argument.
Making sure that the action is picked up by Django
You’ll need to make sure that your newly created action is picked up by Django. Assuming that it lives in an actions.py file inside sample_module module, your sample_module/apps.py should look something like this:
from django.apps import AppConfig
class SampleModuleAppConfig(AppConfig):
name = 'sample_module'
def ready(self):
# Make sure that all models are imported
from sample_module import actions # noqa
Registering action to Django Admin
Finally you’ll need to hook up your action to the Rule Django Admin. You do that by adding it to the list of available inline polymorphic models:
from polymorphic.admin import StackedPolymorphicInline
from conditioner.admin import ActionInline
from sample_module.actions import SampleModuleAction
# Register `SampleModuleAction` action to 'conditioner' Django Admin
class SampleModuleActionInline(StackedPolymorphicInline.Child):
model = SampleModuleAction
ActionInline.child_inlines.append(SampleModuleActionInline)
Assuming you put the code above in the sample_module/conditioner.py file, you’ll need to make sure that it’s also picked up by Django by adding from sample_module import conditioner to your app config ready() method.
You should now see your custom action in Django Admin.
API
There’s no proper documentation as of now, but the code is commented and should be pretty straightforward to use.
That said - feel free to open a GitHub issue if anything is unclear.
Tests
Package was tested with the help of tox on Python 3.4, 3.5 and 3.6 with Django 1.8, 1.9 and 1.10 (see tox.ini).
To run tests yourself you need to run tox inside the repository:
$ tox
Contributions
Package source code is available at GitHub.
Feel free to use, ask, fork, star, report bugs, fix them, suggest enhancements, add functionality and point out any mistakes. Thanks!
New actions and conditions more then welcome but should be first discussed and agreed upon.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-conditioner-0.1.0.tar.gz
.
File metadata
- Download URL: django-conditioner-0.1.0.tar.gz
- Upload date:
- Size: 18.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c6401a4cc7fae5dd5d2825cbcb75b53976215a6da27e21f9a14b5f75dcb6bbf9 |
|
MD5 | a0276649f78e91e30fb31cda6096187a |
|
BLAKE2b-256 | 4d09c95ec2ecf967078cbb8e6b725e421f5169cf92661132de93d2d69a9897ad |
File details
Details for the file django_conditioner-0.1.0-py2.py3-none-any.whl
.
File metadata
- Download URL: django_conditioner-0.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 30.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b0a6b4488e42365cbf37fe12d4afe163b47d0784849614df7839eb25ec569162 |
|
MD5 | ab44ba6a24e610a2ce74599171805c53 |
|
BLAKE2b-256 | 83bf8ec1db773006bb45153a7f9f2f555c8da03a155659fb3449d296bfc8e3a6 |