Skip to content

Commit

Permalink
Add a api.event helper
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Oct 30, 2018
1 parent 5340532 commit 77e9d20
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 25 deletions.
26 changes: 6 additions & 20 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,19 @@ matter):
if not challenge.is_active:
return
yield {
"group": 'challenge_created',
"key": 'challenge_created_%s' % challenge.pk,
"context": {"challenge": challenge},
}
context = {"challenge": challenge}
yield api.event("challenge_created", challenge.pk, context)
if (date.today() - challenge.start_date).days > 2:
if challenge.donations.count() < 2:
yield {
"group": 'challenge_inactivity_2d',
"key": 'challenge_inactivity_2d_%s' % challenge.pk,
"context": {"challenge": challenge},
}
yield api.event("challenge_inactivity_2d", challenge.pk, context)
if (challenge.end_date - date.today()).days <= 2:
yield {
"group": 'challenge_ends_2d',
"key": 'challenge_ends_2d_%s' % challenge.pk,
"context": {"challenge": challenge},
}
yield api.event("challenge_ends_2d", challenge.pk, context)
if challenge.end_date < date.today():
yield {
"group": 'challenge_ended',
"key": 'challenge_ended_%s' % challenge.pk,
"context": {"challenge": challenge},
}
yield api.event("challenge_ended", challenge.pk, context)
Send mails related to challenges (uses django-authlib's
Expand Down
8 changes: 8 additions & 0 deletions spark/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
HANDLERS = []


def event(group, instance_id, context=None):
return {
"group": group,
"key": "{}_{}".format(group, instance_id),
"context": {} if context is None else context,
}


def only_new_events(iterable):
from .models import Event

Expand Down
9 changes: 4 additions & 5 deletions spark/spark_generators/api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from spark.api import event


def pure_function_memoizer():
MEMO = {}

Expand Down Expand Up @@ -27,8 +30,4 @@ def events_from_generators(generators=None):
if not condition["test"](context[condition["variable"]]):
break
else:
yield {
"group": generator["group"],
"key": "{}_{}".format(generator["group"], candidate.pk),
"context": context,
}
yield event(generator["group"], candidate.pk, context)

0 comments on commit 77e9d20

Please sign in to comment.