2022-06-03 08:09:47 -06:00
|
|
|
from django_filters import rest_framework as filters
|
|
|
|
|
from rest_framework.permissions import IsAuthenticated
|
2022-06-17 15:34:59 +03:00
|
|
|
from rest_framework.viewsets import ModelViewSet
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
from apps.auth_token.auth import ApiTokenAuthentication
|
2022-06-17 15:34:59 +03:00
|
|
|
from apps.public_api.serializers.action import ActionCreateSerializer, ActionUpdateSerializer
|
2022-06-03 08:09:47 -06:00
|
|
|
from apps.public_api.throttlers.user_throttle import UserThrottle
|
2023-08-22 14:05:52 -06:00
|
|
|
from apps.webhooks.models import Webhook
|
2022-06-03 08:09:47 -06:00
|
|
|
from common.api_helpers.filters import ByTeamFilter
|
2022-06-17 15:34:59 +03:00
|
|
|
from common.api_helpers.mixins import PublicPrimaryKeyMixin, RateLimitHeadersMixin, UpdateSerializerMixin
|
2022-06-03 08:09:47 -06:00
|
|
|
from common.api_helpers.paginators import FiftyPageSizePaginator
|
2022-08-24 12:04:44 +05:00
|
|
|
from common.insight_log import EntityEvent, write_resource_insight_log
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
|
2022-06-17 15:34:59 +03:00
|
|
|
class ActionView(RateLimitHeadersMixin, PublicPrimaryKeyMixin, UpdateSerializerMixin, ModelViewSet):
|
2024-03-28 11:37:22 -04:00
|
|
|
"""
|
|
|
|
|
This endpoint is deprecated and webhooks should be used instead. This view should remain in the
|
|
|
|
|
codebase in order to support terraform configurations that are still referencing it.
|
|
|
|
|
"""
|
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
authentication_classes = (ApiTokenAuthentication,)
|
|
|
|
|
permission_classes = (IsAuthenticated,)
|
|
|
|
|
pagination_class = FiftyPageSizePaginator
|
|
|
|
|
throttle_classes = [UserThrottle]
|
|
|
|
|
|
2023-10-03 08:33:05 -06:00
|
|
|
model = Webhook
|
2022-06-17 15:34:59 +03:00
|
|
|
serializer_class = ActionCreateSerializer
|
|
|
|
|
update_serializer_class = ActionUpdateSerializer
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
filter_backends = (filters.DjangoFilterBackend,)
|
|
|
|
|
filterset_class = ByTeamFilter
|
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
|
action_name = self.request.query_params.get("name", None)
|
2023-08-22 14:05:52 -06:00
|
|
|
queryset = Webhook.objects.filter(organization=self.request.auth.organization)
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
if action_name:
|
|
|
|
|
queryset = queryset.filter(name=action_name)
|
|
|
|
|
|
2023-06-07 12:51:58 +02:00
|
|
|
return queryset.order_by("id")
|
2022-06-17 15:34:59 +03:00
|
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
|
serializer.save()
|
2022-08-24 12:04:44 +05:00
|
|
|
write_resource_insight_log(
|
|
|
|
|
instance=serializer.instance,
|
|
|
|
|
author=self.request.user,
|
|
|
|
|
event=EntityEvent.CREATED,
|
|
|
|
|
)
|
2022-06-17 15:34:59 +03:00
|
|
|
|
|
|
|
|
def perform_update(self, serializer):
|
2022-08-24 12:04:44 +05:00
|
|
|
prev_state = serializer.instance.insight_logs_serialized
|
2022-06-17 15:34:59 +03:00
|
|
|
serializer.save()
|
2022-08-24 12:04:44 +05:00
|
|
|
new_state = serializer.instance.insight_logs_serialized
|
|
|
|
|
write_resource_insight_log(
|
|
|
|
|
instance=serializer.instance,
|
|
|
|
|
author=self.request.user,
|
|
|
|
|
event=EntityEvent.UPDATED,
|
|
|
|
|
prev_state=prev_state,
|
|
|
|
|
new_state=new_state,
|
|
|
|
|
)
|
2022-06-17 15:34:59 +03:00
|
|
|
|
|
|
|
|
def perform_destroy(self, instance):
|
2022-08-24 12:04:44 +05:00
|
|
|
write_resource_insight_log(
|
|
|
|
|
instance=instance,
|
|
|
|
|
author=self.request.user,
|
|
|
|
|
event=EntityEvent.DELETED,
|
|
|
|
|
)
|
2022-06-17 15:34:59 +03:00
|
|
|
instance.delete()
|