Beispiel #1
0
def raw_custom_event(sender,
                     event_name,
                     payload,
                     user=None,
                     send_hook_meta=True,
                     instance=None,
                     api_application=None,
                     environment=None,
                     **kwargs):
    """
    Give a full payload
    """
    HookModel = get_hook_model()

    filters = {'event': event_name, 'user': user}
    if hasattr(HookModel, 'api_application') and api_application:
        filters.pop('user')
        filters['api_application'] = api_application
    if hasattr(HookModel, 'environment') and environment:
        filters.pop('user')
        filters['environment'] = environment

    hooks = HookModel.objects.filter(**filters)

    for hook in hooks:
        new_payload = payload
        if send_hook_meta:
            new_payload = {'hook': hook.dict(), 'data': payload}

        hook.deliver_hook(instance, payload_override=new_payload)
Beispiel #2
0
 def test_get_custom_hook_model(self):
     # Using the default Hook model just to exercise get_hook_model's
     # lookup machinery.
     from rest_hooks.utils import get_hook_model
     from rest_hooks.models import AbstractHook
     HookModel = get_hook_model()
     self.assertIs(HookModel, Hook)
     self.assertTrue(issubclass(HookModel, AbstractHook))
Beispiel #3
0
 def test_get_custom_hook_model(self):
     # Using the default Hook model just to exercise get_hook_model's
     # lookup machinery.
     from rest_hooks.utils import get_hook_model
     from rest_hooks.models import AbstractHook
     HookModel = get_hook_model()
     self.assertIs(HookModel, Hook)
     self.assertTrue(issubclass(HookModel, AbstractHook))
Beispiel #4
0
    def run(self, target, payload, instance=None, hook_id=None, **kwargs):
        """
        target:     the url to receive the payload.
        payload:    a python primitive data structure
        instance:   a possibly null "trigger" instance
        hook:       the defining Hook object (useful for removing)
        """
        response = requests.post(url=target,
                                 data=json.dumps(payload,
                                                 cls=DjangoJSONEncoder),
                                 headers={'Content-Type': 'application/json'})

        if response.status_code == 410 and hook_id:
            HookModel = get_hook_model()
            hook = HookModel.object.get(id=hook_id)
            hook.delete()
Beispiel #5
0
 def run(self, target: str, payload: dict, hook_id: str) -> None:
     try:
         response = requests.post(
             url=target,
             data=json.dumps(payload, cls=DjangoJSONEncoder),
             headers={"Content-Type": "application/json"},
         )
         if response.status_code == 410 and hook_id:
             # Delete hook on our side if it's gone on Zapier's
             Hook = get_hook_model()
             Hook.objects.filter(id=hook_id).delete()
             return
         if response.status_code >= 500:
             response.raise_for_status()
     except requests.ConnectionError:
         delay_in_seconds = 2 ** self.request.retries
         self.retry(countdown=delay_in_seconds)
Beispiel #6
0
    def run(self, target, payload, instance=None, hook_id=None, **kwargs):
        """
        target:     the url to receive the payload.
        payload:    a python primitive data structure
        instance:   a possibly null "trigger" instance
        hook:       the defining Hook object (useful for removing)
        """
        response = requests.post(
            url=target,
            data=json.dumps(payload, cls=DjangoJSONEncoder),
            headers={'Content-Type': 'application/json'}
        )

        if response.status_code == 410 and hook_id:
            HookModel = get_hook_model()
            hook = HookModel.object.get(id=hook_id)
            hook.delete()
Beispiel #7
0
def raw_custom_event(sender,
                     event_name,
                     payload,
                     user,
                     send_hook_meta=True,
                     instance=None,
                     **kwargs):
    """
    Give a full payload
    """
    HookModel = get_hook_model()
    hooks = HookModel.objects.filter(user=user, event=event_name)

    for hook in hooks:
        new_payload = payload
        if send_hook_meta:
            new_payload = {'hook': hook.dict(), 'data': payload}

        hook.deliver_hook(instance, payload_override=new_payload)
Beispiel #8
0
from django.contrib import admin
from django.conf import settings
from django import forms
from rest_hooks.utils import get_hook_model

if getattr(settings, 'HOOK_EVENTS', None) is None:
    raise Exception("You need to define settings.HOOK_EVENTS!")


HookModel = get_hook_model()


class HookForm(forms.ModelForm):
    """
    Model form to handle registered events, asuring
    only events declared on HOOK_EVENTS settings
    can be registered.
    """

    class Meta:
        model = HookModel
        fields = ['user', 'target', 'event']

    def __init__(self, *args, **kwargs):
        super(HookForm, self).__init__(*args, **kwargs)
        self.fields['event'] = forms.ChoiceField(choices=self.get_admin_events())

    @classmethod
    def get_admin_events(cls):
        return [(x, x) for x in getattr(settings, 'HOOK_EVENTS', None).keys()]