Skip to content
This repository has been archived by the owner on May 5, 2021. It is now read-only.

Commit

Permalink
Add new convention for user agnostic hooks.
Browse files Browse the repository at this point in the history
  • Loading branch information
GVRV authored and gvangool committed Aug 12, 2015
1 parent 4ee3a11 commit 84c7480
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ INSTALLED_APPS = (
HOOK_EVENTS = {
# 'any.event.name': 'App.Model.Action' (created/updated/deleted)
'book.added': 'bookstore.Book.created',
'book.changed': 'bookstore.Book.updated',
'book.changed': 'bookstore.Book.updated+',
'book.removed': 'bookstore.Book.deleted',
# and custom events, no extra meta data needed
'book.read': 'bookstore.Book.read',
Expand All @@ -87,6 +87,9 @@ HOOK_EVENTS = {
class Book(models.Model):
# NOTE: it is important to have a user property
# as we use it to help find and trigger each Hook
# which is specific to users. If you want a Hook to
# be triggered for all users, add '+' to built-in Hooks
# or pass user_override=False for custom_hook events
user = models.ForeignKey('auth.User')
# maybe user is off a related object, so try...
# user = property(lambda self: self.intermediary.user)
Expand Down
9 changes: 8 additions & 1 deletion rest_hooks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def find_and_fire_hook(event_name, instance, user_override=None):
filters['user'] = instance.user
elif isinstance(instance, User):
filters['user'] = instance
else:
raise Exception(
'{} has no `user` property. REST Hooks needs this.'.format(repr(instance))
)

# NOTE: This is probably up for discussion, but I think, in this
# case, instead of raising an error, we should fire the hook for
Expand All @@ -71,8 +75,11 @@ def distill_model_event(instance, model, action, user_override=None):
if auto:
# break auto into App.Model, Action
maybe_model, maybe_action = auto.rsplit('.', 1)
if model == maybe_model and action == maybe_action:
maybe_action = maybe_action.rsplit('+', 1)
if model == maybe_model and action == maybe_action[0]:
event_name = maybe_event_name
if len(maybe_action) == 2:
user_override = True

if event_name:
find_and_fire_hook(event_name, instance, user_override=user_override)

0 comments on commit 84c7480

Please sign in to comment.