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

Bind hooks to groups or users #49

Closed
NDevox opened this issue Jan 19, 2018 · 4 comments
Closed

Bind hooks to groups or users #49

NDevox opened this issue Jan 19, 2018 · 4 comments

Comments

@NDevox
Copy link

NDevox commented Jan 19, 2018

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 the AbstractHook model. Making both user and group 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?

@avelis
Copy link
Contributor

avelis commented Jan 19, 2018

@NDevox This is definitely what the AbstractHook model is for. I suggest for now to roll your own. I am not against PR's though but could make the case that keeping this library simpler maintains its longevity.

@NDevox
Copy link
Author

NDevox commented Jan 23, 2018

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)

@NDevox NDevox closed this as completed Jan 23, 2018
@avelis
Copy link
Contributor

avelis commented Jan 23, 2018

Thanks again for documenting your solution in this issue!

@NDevox
Copy link
Author

NDevox commented Jan 23, 2018

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.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants