Skip to main content

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

Build status PyPI version Python versions License

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

Both actions and conditions are divided into generic, model generic and model specific types:
- generic actions/conditions don’t need a set target type (i.e. log a message on every Monday) - model generic actions/conditions need a set target type, but work with all available models (i.e. log a message when selected target type instance was created) - model specific actions/conditions are implemented with specific model in mind and area available only when it’s selected (i.e. send email to user on every Monday); they usually operate on specific fields (user.email in previous example)

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.

Authors

Developed and maintained by Omni Digital.

Released under MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-conditioner-0.1.0.tar.gz (18.6 kB view details)

Uploaded Source

Built Distribution

django_conditioner-0.1.0-py2.py3-none-any.whl (30.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-conditioner-0.1.0.tar.gz.

File metadata

File hashes

Hashes for django-conditioner-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c6401a4cc7fae5dd5d2825cbcb75b53976215a6da27e21f9a14b5f75dcb6bbf9
MD5 a0276649f78e91e30fb31cda6096187a
BLAKE2b-256 4d09c95ec2ecf967078cbb8e6b725e421f5169cf92661132de93d2d69a9897ad

See more details on using hashes here.

File details

Details for the file django_conditioner-0.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_conditioner-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b0a6b4488e42365cbf37fe12d4afe163b47d0784849614df7839eb25ec569162
MD5 ab44ba6a24e610a2ce74599171805c53
BLAKE2b-256 83bf8ec1db773006bb45153a7f9f2f555c8da03a155659fb3449d296bfc8e3a6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page