Package: django-conditioner
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 todjango-polymorphicwithout which this whole thing would be much harder to do.Installation>From PyPI:$pipinstalldjango-conditionerUsageIf you want to use the already implemented actions and conditions then
all you need to do is install the package, addconditionerto your
list ofINSTALLED_APPSand run$ python manage.py migrate.You should then see aConditionersection with aRulechild in
Django Admin. Adding a new one should be pretty self-explanatory.Advanced usageActions and conditions typesBoth 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.emailin previous example)Adding new actions and/or conditionsOne of the main goals was to make conditioner as flexible as possible
and make adding new actions/conditions as easy as possible.Creating the actionAll actions need to inherit fromBaseActionand implementrun_action(). Model generic actions should setmodel_specifictoTrue, model specific actions should set it to return the needed
model class. If your action is model specific then model instance will
be passed torun_action()method asinstancenamed argument.Making sure that the action is picked up by DjangoYou’ll need to make sure that your newly created action is picked up by
Django. Assuming that it lives in anactions.pyfile insidesample_modulemodule, yoursample_module/apps.pyshould look
something like this:fromdjango.appsimportAppConfigclassSampleModuleAppConfig(AppConfig):name='sample_module'defready(self):# Make sure that all models are importedfromsample_moduleimportactions# noqaRegistering action to Django AdminFinally you’ll need to hook up your action to theRuleDjango Admin.
You do that by adding it to the list of available inline polymorphic
models:frompolymorphic.adminimportStackedPolymorphicInlinefromconditioner.adminimportActionInlinefromsample_module.actionsimportSampleModuleAction# Register `SampleModuleAction` action to 'conditioner' Django AdminclassSampleModuleActionInline(StackedPolymorphicInline.Child):model=SampleModuleActionActionInline.child_inlines.append(SampleModuleActionInline)Assuming you put the code above in thesample_module/conditioner.pyfile, you’ll need to make sure that it’s also picked up by Django by
addingfrom sample_module import conditionerto your app configready()method.You should now see your custom action in Django Admin.APIThere’s no proper documentation as of now, but the code is commented andshouldbe pretty straightforward to use.That said - feel free to open aGitHub
issueif anything is unclear.TestsPackage was tested with the help oftoxon Python 3.4, 3.5 and 3.6
with Django 1.8, 1.9 and 1.10 (seetox.ini).To run tests yourself you need to runtoxinside the repository:$toxContributionsPackage source code is available atGitHub.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.AuthorsDeveloped and maintained byOmni
Digital.Released underMIT
License.