2022-06-03 08:09:47 -06:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2023-03-22 00:57:20 +08:00
|
|
|
from django_filters import rest_framework as filters
|
|
|
|
|
from rest_framework.decorators import action
|
2022-06-03 08:09:47 -06:00
|
|
|
from rest_framework.exceptions import NotFound
|
2023-03-22 00:57:20 +08:00
|
|
|
from rest_framework.filters import SearchFilter
|
2022-06-03 08:09:47 -06:00
|
|
|
from rest_framework.permissions import IsAuthenticated
|
2023-03-22 00:57:20 +08:00
|
|
|
from rest_framework.response import Response
|
2022-06-03 08:09:47 -06:00
|
|
|
from rest_framework.viewsets import ModelViewSet
|
|
|
|
|
|
2022-10-28 10:44:37 +02:00
|
|
|
from apps.alerts.models import CustomButton
|
2022-11-29 09:41:56 +01:00
|
|
|
from apps.api.permissions import RBACPermission
|
2022-06-03 08:09:47 -06:00
|
|
|
from apps.api.serializers.custom_button import CustomButtonSerializer
|
|
|
|
|
from apps.auth_token.auth import PluginAuthentication
|
2023-03-22 00:57:20 +08:00
|
|
|
from common.api_helpers.filters import ByTeamModelFieldFilterMixin, ModelFieldFilterMixin, TeamModelMultipleChoiceFilter
|
2022-09-12 17:26:23 +03:00
|
|
|
from common.api_helpers.mixins import PublicPrimaryKeyMixin, TeamFilteringMixin
|
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
|
|
|
|
|
|
|
|
|
2023-03-22 00:57:20 +08:00
|
|
|
class CustomButtonFilter(ByTeamModelFieldFilterMixin, ModelFieldFilterMixin, filters.FilterSet):
|
|
|
|
|
team = TeamModelMultipleChoiceFilter()
|
|
|
|
|
|
|
|
|
|
|
2022-09-12 17:26:23 +03:00
|
|
|
class CustomButtonView(TeamFilteringMixin, PublicPrimaryKeyMixin, ModelViewSet):
|
2022-06-03 08:09:47 -06:00
|
|
|
authentication_classes = (PluginAuthentication,)
|
2022-11-29 09:41:56 +01:00
|
|
|
permission_classes = (IsAuthenticated, RBACPermission)
|
|
|
|
|
|
|
|
|
|
rbac_permissions = {
|
|
|
|
|
"metadata": [RBACPermission.Permissions.OUTGOING_WEBHOOKS_READ],
|
2023-03-22 00:57:20 +08:00
|
|
|
"filters": [RBACPermission.Permissions.OUTGOING_WEBHOOKS_READ],
|
2022-11-29 09:41:56 +01:00
|
|
|
"list": [RBACPermission.Permissions.OUTGOING_WEBHOOKS_READ],
|
|
|
|
|
"retrieve": [RBACPermission.Permissions.OUTGOING_WEBHOOKS_READ],
|
|
|
|
|
"create": [RBACPermission.Permissions.OUTGOING_WEBHOOKS_WRITE],
|
|
|
|
|
"update": [RBACPermission.Permissions.OUTGOING_WEBHOOKS_WRITE],
|
|
|
|
|
"partial_update": [RBACPermission.Permissions.OUTGOING_WEBHOOKS_WRITE],
|
|
|
|
|
"destroy": [RBACPermission.Permissions.OUTGOING_WEBHOOKS_WRITE],
|
2022-06-03 08:09:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
model = CustomButton
|
|
|
|
|
serializer_class = CustomButtonSerializer
|
|
|
|
|
|
2023-03-22 00:57:20 +08:00
|
|
|
filter_backends = [SearchFilter, filters.DjangoFilterBackend]
|
|
|
|
|
search_fields = ["public_primary_key", "name"]
|
|
|
|
|
filterset_class = CustomButtonFilter
|
|
|
|
|
|
|
|
|
|
def get_queryset(self, ignore_filtering_by_available_teams=False):
|
2022-06-03 08:09:47 -06:00
|
|
|
queryset = CustomButton.objects.filter(
|
|
|
|
|
organization=self.request.auth.organization,
|
|
|
|
|
)
|
2023-03-22 00:57:20 +08:00
|
|
|
if not ignore_filtering_by_available_teams:
|
2023-03-22 15:43:32 +08:00
|
|
|
queryset = queryset.filter(*self.available_teams_lookup_args).distinct()
|
2023-03-22 00:57:20 +08:00
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
def get_object(self):
|
2022-09-12 17:26:23 +03:00
|
|
|
# get the object from the whole organization if there is a flag `get_from_organization=true`
|
|
|
|
|
# otherwise get the object from the current team
|
|
|
|
|
get_from_organization = self.request.query_params.get("from_organization", "false") == "true"
|
|
|
|
|
if get_from_organization:
|
|
|
|
|
return self.get_object_from_organization()
|
|
|
|
|
return super().get_object()
|
|
|
|
|
|
|
|
|
|
def get_object_from_organization(self):
|
|
|
|
|
# use this method to get the object from the whole organization instead of the current team
|
2022-06-03 08:09:47 -06:00
|
|
|
pk = self.kwargs["pk"]
|
|
|
|
|
organization = self.request.auth.organization
|
|
|
|
|
try:
|
2023-05-23 17:23:06 +01:00
|
|
|
obj = (
|
|
|
|
|
organization.custom_buttons.filter(*self.available_teams_lookup_args)
|
|
|
|
|
.distinct()
|
|
|
|
|
.get(public_primary_key=pk)
|
|
|
|
|
)
|
2022-06-03 08:09:47 -06:00
|
|
|
except ObjectDoesNotExist:
|
|
|
|
|
raise NotFound
|
|
|
|
|
|
|
|
|
|
# May raise a permission denied
|
|
|
|
|
self.check_object_permissions(self.request, obj)
|
|
|
|
|
|
|
|
|
|
return obj
|
|
|
|
|
|
|
|
|
|
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-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
def perform_update(self, serializer):
|
2022-08-24 12:04:44 +05:00
|
|
|
prev_state = serializer.instance.insight_logs_serialized
|
2022-06-03 08:09:47 -06: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-03 08:09:47 -06: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-03 08:09:47 -06:00
|
|
|
instance.delete()
|
2023-03-22 00:57:20 +08:00
|
|
|
|
|
|
|
|
@action(methods=["get"], detail=False)
|
|
|
|
|
def filters(self, request):
|
|
|
|
|
filter_name = request.query_params.get("search", None)
|
|
|
|
|
api_root = "/api/internal/v1/"
|
|
|
|
|
|
|
|
|
|
filter_options = [
|
|
|
|
|
{
|
|
|
|
|
"name": "team",
|
|
|
|
|
"type": "team_select",
|
|
|
|
|
"href": api_root + "teams/",
|
|
|
|
|
"global": True,
|
|
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
if filter_name is not None:
|
|
|
|
|
filter_options = list(filter(lambda f: filter_name in f["name"], filter_options))
|
|
|
|
|
|
|
|
|
|
return Response(filter_options)
|