futurice/oncondition

Name: oncondition

Owner: Futurice

Description: null

Created: 2016-03-02 14:57:35.0

Updated: 2016-03-03 07:46:24.0

Pushed: 2016-08-04 15:02:27.0

Homepage: null

Size: 16

Language: Python

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

On Condition Build Status

Run actions based on conditions.

Why?

To provide transparency into 'What happens When?' and 'Is This working?' when bundling arbitrary logic into your application. For example creating email notifications based on the form filledness criterias is a good use-case.

Environment Settings
RY_MODULE = "foo.app"
Optional Environment Settings
ODEL = "oncondition.Event"
AITMODEL = "oncondition.EventWaiting"
Usage

An Event, that fires when any User has changes:

 oncondition.events import Event
s MyEvent(Event):
def condition(self, instance, changes):
    return 5>4

def action(self, instance, context):
    self.mail(subject='[EVENT] Breakthrough!', body="--", to=self.recipients())
    self.log("My Event fired! Recipients: [%s]"%self.recipients())

Register the Event:

 oncondition.events import event_model
t_model().objects.get_or_create(name="my-event", cls="project.MyEvent", model="auth.User", recipients="me@company.com,you@company.com")
Delayed Events

On Condition supports any number of CONDITION_NAME -methods to run for a single Event, with their respective (optional) 'CONDITION_NAME_failure' -methods. Out of the box `time_conditionhas a respective ``time_condition_failure```, that creates an EventWaiting -instance. This allows to poll changes to an Event out of a request/response cycle. When the time_condition is met, it is marked as done.

CELERYBEAT_SCHEDULE = {
    'waiting-events': {
        'task': 'oncondition.tasks.handle_timed_events',
        'schedule': timedelta(minutes=60),
    }
}
Project integration and testing

To test against Celery, provide your own CELERY_CONFIG_MODULE (see app_test_runner.py), eg:

nage.py
if 'test' in sys.argv:
    os.environ["CELERY_CONFIG_MODULE"] = "foo.tests.celeryconfig"
o.tests.celeryconfig
 foo.celeryconfig import *
RY_ALWAYS_EAGER = True
RY_EAGER_PROPAGATES_EXCEPTIONS = True
Best practices

Extend oncondition.models.Event and point `ET_MODEL, ``ET_WAITMODEL``` to these.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.