2022-06-03 08:09:47 -06:00
|
|
|
from contextlib import suppress
|
|
|
|
|
|
|
|
|
|
from rest_framework import status
|
|
|
|
|
from rest_framework.permissions import IsAuthenticated
|
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
|
|
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.organization import CurrentOrganizationSerializer
|
|
|
|
|
from apps.auth_token.auth import PluginAuthentication
|
|
|
|
|
from apps.base.messaging import get_messaging_backend_from_id
|
2023-07-21 17:38:58 +02:00
|
|
|
from apps.mobile_app.auth import MobileAppAuthTokenAuthentication
|
2022-06-03 08:09:47 -06:00
|
|
|
from apps.telegram.client import TelegramClient
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
class CurrentOrganizationView(APIView):
|
2023-07-21 17:38:58 +02:00
|
|
|
authentication_classes = (
|
|
|
|
|
MobileAppAuthTokenAuthentication,
|
|
|
|
|
PluginAuthentication,
|
|
|
|
|
)
|
2022-11-29 09:41:56 +01:00
|
|
|
permission_classes = (IsAuthenticated, RBACPermission)
|
2022-06-03 08:09:47 -06:00
|
|
|
|
2022-11-29 09:41:56 +01:00
|
|
|
rbac_permissions = {
|
|
|
|
|
"get": [],
|
|
|
|
|
"put": [RBACPermission.Permissions.OTHER_SETTINGS_WRITE],
|
|
|
|
|
}
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
|
organization = request.auth.organization
|
|
|
|
|
serializer = CurrentOrganizationSerializer(organization, context={"request": request})
|
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
|
|
|
|
def put(self, request):
|
|
|
|
|
organization = self.request.auth.organization
|
2022-08-24 12:04:44 +05:00
|
|
|
prev_state = organization.insight_logs_serialized
|
2022-06-03 08:09:47 -06:00
|
|
|
serializer = CurrentOrganizationSerializer(
|
|
|
|
|
instance=organization, data=request.data, context={"request": request}
|
|
|
|
|
)
|
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GetTelegramVerificationCode(APIView):
|
|
|
|
|
authentication_classes = (PluginAuthentication,)
|
2022-11-29 09:41:56 +01:00
|
|
|
permission_classes = (IsAuthenticated, RBACPermission)
|
|
|
|
|
|
|
|
|
|
rbac_permissions = {
|
|
|
|
|
"get": [RBACPermission.Permissions.INTEGRATIONS_WRITE],
|
|
|
|
|
}
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
|
organization = request.auth.organization
|
|
|
|
|
user = request.user
|
2023-07-25 10:43:23 +01:00
|
|
|
from apps.telegram.models import TelegramChannelVerificationCode
|
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
with suppress(TelegramChannelVerificationCode.DoesNotExist):
|
|
|
|
|
existing_verification_code = organization.telegram_verification_code
|
|
|
|
|
existing_verification_code.delete()
|
|
|
|
|
new_code = TelegramChannelVerificationCode.objects.create(organization=organization, author=user)
|
|
|
|
|
telegram_client = TelegramClient()
|
|
|
|
|
bot_username = telegram_client.api_client.username
|
|
|
|
|
bot_link = f"https://t.me/{bot_username}"
|
2022-10-25 14:53:07 +08:00
|
|
|
return Response(
|
2022-12-06 22:42:58 +08:00
|
|
|
{"telegram_code": str(new_code.uuid_with_org_uuid), "bot_link": bot_link}, status=status.HTTP_200_OK
|
2022-10-25 14:53:07 +08:00
|
|
|
)
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class GetChannelVerificationCode(APIView):
|
|
|
|
|
authentication_classes = (PluginAuthentication,)
|
2022-11-29 09:41:56 +01:00
|
|
|
permission_classes = (IsAuthenticated, RBACPermission)
|
|
|
|
|
|
|
|
|
|
rbac_permissions = {
|
|
|
|
|
"get": [RBACPermission.Permissions.INTEGRATIONS_WRITE],
|
|
|
|
|
}
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
|
organization = request.auth.organization
|
|
|
|
|
backend_id = request.query_params.get("backend")
|
|
|
|
|
backend = get_messaging_backend_from_id(backend_id)
|
|
|
|
|
if backend is None:
|
|
|
|
|
return Response(status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
|
|
|
|
code = backend.generate_channel_verification_code(organization)
|
|
|
|
|
return Response(code)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SetGeneralChannel(APIView):
|
|
|
|
|
authentication_classes = (PluginAuthentication,)
|
2022-11-29 09:41:56 +01:00
|
|
|
permission_classes = (IsAuthenticated, RBACPermission)
|
|
|
|
|
|
|
|
|
|
rbac_permissions = {
|
|
|
|
|
"post": [RBACPermission.Permissions.CHATOPS_UPDATE_SETTINGS],
|
|
|
|
|
}
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
def post(self, request):
|
2023-07-25 10:43:23 +01:00
|
|
|
from apps.slack.models import SlackChannel
|
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
organization = request.auth.organization
|
|
|
|
|
slack_team_identity = organization.slack_team_identity
|
|
|
|
|
slack_channel_id = request.data["id"]
|
|
|
|
|
|
|
|
|
|
slack_channel = SlackChannel.objects.get(
|
|
|
|
|
public_primary_key=slack_channel_id, slack_team_identity=slack_team_identity
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
organization.set_general_log_channel(slack_channel.slack_id, slack_channel.name, request.user)
|
|
|
|
|
|
|
|
|
|
return Response(status=200)
|