Skip to content

Commit 84c7480

Browse files
GVRVgvangool
authored andcommitted
Add new convention for user agnostic hooks.
1 parent 4ee3a11 commit 84c7480

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ INSTALLED_APPS = (
7575
HOOK_EVENTS = {
7676
# 'any.event.name': 'App.Model.Action' (created/updated/deleted)
7777
'book.added': 'bookstore.Book.created',
78-
'book.changed': 'bookstore.Book.updated',
78+
'book.changed': 'bookstore.Book.updated+',
7979
'book.removed': 'bookstore.Book.deleted',
8080
# and custom events, no extra meta data needed
8181
'book.read': 'bookstore.Book.read',
@@ -87,6 +87,9 @@ HOOK_EVENTS = {
8787
class Book(models.Model):
8888
# NOTE: it is important to have a user property
8989
# as we use it to help find and trigger each Hook
90+
# which is specific to users. If you want a Hook to
91+
# be triggered for all users, add '+' to built-in Hooks
92+
# or pass user_override=False for custom_hook events
9093
user = models.ForeignKey('auth.User')
9194
# maybe user is off a related object, so try...
9295
# user = property(lambda self: self.intermediary.user)

rest_hooks/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ def find_and_fire_hook(event_name, instance, user_override=None):
4545
filters['user'] = instance.user
4646
elif isinstance(instance, User):
4747
filters['user'] = instance
48+
else:
49+
raise Exception(
50+
'{} has no `user` property. REST Hooks needs this.'.format(repr(instance))
51+
)
4852

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

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

0 commit comments

Comments
 (0)