0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-28 00:46:45 +01:00
posthog/ee/models/hook.py
Michael Matloka 8a629179a9
Organizations – models (#1674)
* Update only models

* Bring in line with master and use uuid1_macless

* Update models and annotation scope support

* Delete test_team_model.py

* Update user creation, team retrieval and fix tests

* Make fixes

* Rename migration

* Fix migrating from master

* Bring back previous company_name max_length

* Use get_price_id()

* Temporarily disable team member deletion

* Update user joining and leaving, and billing

* Improve first_name handling

* Update warning

* Update TestTeamUser

* Fix migration

* Update 0085_org_models.py

* Improve bootstrapping

* Move multitenancy price tests to posthog-production

* Update team_user.py

* Update setup_review.py

* Enhance opt_slash_path

* Update team.py

* Fix default test email

* Fix typing
2020-09-24 00:53:51 +02:00

37 lines
1.4 KiB
Python

import json
from typing import Optional
from django.db import models
from rest_hooks.models import AbstractHook
from ee.tasks.hooks import DeliverHook
from posthog.models import Team
from posthog.models.utils import generate_random_token
class Hook(AbstractHook):
id = models.CharField(primary_key=True, max_length=50, default=generate_random_token)
user = models.ForeignKey("posthog.User", related_name="rest_hooks", on_delete=models.CASCADE)
team = models.ForeignKey(Team, related_name="rest_hooks", on_delete=models.CASCADE)
resource_id = models.IntegerField(null=True, blank=True)
def find_and_fire_hook(
event_name: str,
instance: models.Model,
user_override: Optional[Team] = None,
payload_override: Optional[dict] = None,
):
hooks = Hook.objects.select_related("user").filter(event=event_name, team=user_override)
if event_name == "action_performed":
# action_performed is a resource_id-filterable hook
hooks = hooks.filter(models.Q(resource_id=instance.pk) | models.Q(resource_id__isnull=True))
for hook in hooks:
if hook.user.is_feature_available("zapier"):
hook.deliver_hook(instance, payload_override)
def deliver_hook_wrapper(target, payload, instance, hook):
# pass IDs not objects because using pickle for objects is a bad thing
DeliverHook.apply_async(kwargs=dict(target=target, payload=payload, hook_id=hook.id))