0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-24 18:07:17 +01:00
posthog/ee/api/hooks.py
Michael Matloka 43b09727d6
Fix REST hook deletion (#2893)
* Fix REST hook deletion

* Fix hook deletion better

* Add API DELETE test
2021-01-08 15:15:48 +01:00

35 lines
1.1 KiB
Python

from django.conf import settings
from rest_framework import exceptions, serializers, viewsets
from rest_framework.permissions import IsAuthenticated
from ee.models.hook import Hook
from posthog.api.routing import StructuredViewSetMixin
from posthog.permissions import OrganizationMemberPermissions
class HookSerializer(serializers.ModelSerializer):
class Meta:
model = Hook
fields = ("id", "created", "updated", "event", "target", "resource_id", "team")
read_only_fields = ("team",)
def validate_event(self, event):
if event not in settings.HOOK_EVENTS:
raise exceptions.ValidationError(detail=f"Unexpected event {event}")
return event
class HookViewSet(StructuredViewSetMixin, viewsets.ModelViewSet):
"""
Retrieve, create, update or destroy webhooks.
"""
queryset = Hook.objects.all()
ordering = "-created_at"
permission_classes = [IsAuthenticated, OrganizationMemberPermissions]
serializer_class = HookSerializer
def perform_create(self, serializer):
user = self.request.user
serializer.save(user=user, team=user.team)