oncall-engine/engine/apps/grafana_plugin/views/install.py
Innokentii Konstantinov 8abbcee050
Org soft-delete (#1073)
# What this PR does
It introduces soft-delete of organization, since grafana stacks are
soft-deleted too. Also, we had a problem with deleting orgs with large
amounts of alerts, so soft-deletion will fix this problem. I think, that
problem of cleaning alerts of deleted orgs should be solved as a part of
alert retention
2023-01-05 12:42:55 +08:00

26 lines
1.1 KiB
Python

from rest_framework import status
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.views import APIView
from apps.grafana_plugin.permissions import PluginTokenVerified
from apps.user_management.models import Organization
from apps.user_management.sync import sync_organization
from common.api_helpers.mixins import GrafanaHeadersMixin
class InstallView(GrafanaHeadersMixin, APIView):
permission_classes = (PluginTokenVerified,)
def post(self, request: Request) -> Response:
stack_id = self.instance_context["stack_id"]
org_id = self.instance_context["org_id"]
organization = Organization.objects_with_deleted.filter(stack_id=stack_id, org_id=org_id).first()
# If we receive install request to the deleted org - just restore it.
organization.deleted_at = None
organization.api_token = self.instance_context["grafana_token"]
organization.save(update_fields=["api_token", "deleted_at"])
sync_organization(organization)
return Response(status=status.HTTP_204_NO_CONTENT)