This repository was archived by the owner on May 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 135
Bind hooks to groups or users #49
Comments
@NDevox This is definitely what the |
Done. For those interested the code looks like this: # models.py
from django.contrib.auth.models import Group
from django.conf import settings
from django.db import models
class CustomHook(AbstractHook):
is_active = models.BooleanField(default=True)
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
related_name='%(class)ss',
on_delete=models.CASCADE,
null=True,
blank=True
)
group = models.ForeignKey(
Group,
related_name='%(class)ss',
on_delete=models.CASCADE,
null=True
)
def __str__(self):
return '{} to {} for {}'.format(
self.event,
self.target,
self.group
)
# utils.py
from django.contrib.auth.models import User
from rest_hooks.models import HOOK_EVENTS
from . import models
def find_and_fire_hook(event_name, instance, user_override=None):
if event_name not in HOOK_EVENTS.keys():
raise ValueError(
'"{}" does not exist in `settings.HOOK_EVENTS`.'.format(event_name)
)
filters = {
'event': event_name,
'is_active': True,
}
if hasattr(instance, 'group'):
filters['group'] = instance.group
if user_override:
filters['user'] = user_override
elif hasattr(instance, 'user'):
filters['user'] = instance.user
elif isinstance(instance, User):
filters['user'] = instance
for hook in models.CustomHook.objects.filter(**filters):
hook.deliver_hook(instance) |
Thanks again for documenting your solution in this issue! |
No problem :-) I'm all too aware of https://xkcd.com/979/ |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Similar to issue #33 I'm interested in this project but need send webhooks based on an object belonging to a group and not just a user.
By the look of this we could do it by adding a
group
property to theAbstractHook
model. Making bothuser
andgroup
optional but that at least one must be filled.And then amending the
find_and_fire_hook
function to look for groups as well.I can set up a fork to get this working. Would you be interested in merging it in if I do? Or should I roll my own version?
The text was updated successfully, but these errors were encountered: