mirror of
https://github.com/PostHog/posthog.git
synced 2024-11-24 09:14:46 +01:00
5513be7731
* Upgraded Ruff linter * Formatted whole codebase with new ruff rules * Revert import removal * Fixed mypi issues or added ignores * Fixed schema formatting * Fixed hogvm failing tests * Remove duplicate key in list
46 lines
1.7 KiB
Python
46 lines
1.7 KiB
Python
import json
|
|
|
|
from django.conf import settings
|
|
from django.core.exceptions import ValidationError
|
|
from django.db import models
|
|
from django.db.models.signals import post_delete, post_save
|
|
from django.dispatch.dispatcher import receiver
|
|
|
|
from posthog.models.signals import mutable_receiver
|
|
from posthog.models.utils import generate_random_token
|
|
from posthog.redis import get_client
|
|
|
|
|
|
class Hook(models.Model):
|
|
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("posthog.Team", related_name="rest_hooks", on_delete=models.CASCADE)
|
|
event = models.CharField("Event", max_length=64, db_index=True)
|
|
resource_id = models.IntegerField(null=True, blank=True)
|
|
target = models.URLField("Target URL", max_length=255)
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
updated = models.DateTimeField(auto_now=True)
|
|
|
|
def clean(self):
|
|
"""Validation for events."""
|
|
if self.event not in settings.HOOK_EVENTS.keys():
|
|
raise ValidationError("Invalid hook event {evt}.".format(evt=self.event))
|
|
|
|
|
|
@receiver(post_save, sender=Hook)
|
|
def hook_saved(sender, instance: Hook, created, **kwargs):
|
|
if instance.event == "action_performed":
|
|
get_client().publish(
|
|
"reload-action",
|
|
json.dumps({"teamId": instance.team_id, "actionId": instance.resource_id}),
|
|
)
|
|
|
|
|
|
@mutable_receiver(post_delete, sender=Hook)
|
|
def hook_deleted(sender, instance: Hook, **kwargs):
|
|
if instance.event == "action_performed":
|
|
get_client().publish(
|
|
"drop-action",
|
|
json.dumps({"teamId": instance.team_id, "actionId": instance.resource_id}),
|
|
)
|