2021-03-24 11:01:35 +00:00
|
|
|
from rest_framework.views import Response
|
|
|
|
from rest_framework.generics import GenericAPIView
|
2021-05-17 06:20:51 +00:00
|
|
|
from rest_framework.exceptions import APIException
|
|
|
|
from rest_framework import status
|
2021-03-24 11:01:35 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
2021-10-18 03:25:39 +00:00
|
|
|
from django.conf import settings
|
2021-10-21 08:50:11 +00:00
|
|
|
from common.sdk.im.dingtalk import DingTalk
|
2021-03-24 11:01:35 +00:00
|
|
|
from .. import serializers
|
|
|
|
|
|
|
|
|
|
|
|
class DingTalkTestingAPI(GenericAPIView):
|
|
|
|
serializer_class = serializers.DingTalkSettingSerializer
|
2022-03-11 11:32:23 +00:00
|
|
|
rbac_perms = {
|
|
|
|
'POST': 'settings.change_auth'
|
|
|
|
}
|
2021-03-24 11:01:35 +00:00
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
serializer = self.serializer_class(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
2021-10-18 03:25:39 +00:00
|
|
|
app_key = serializer.validated_data['DINGTALK_APPKEY']
|
|
|
|
agent_id = serializer.validated_data['DINGTALK_AGENTID']
|
|
|
|
app_secret = serializer.validated_data.get('DINGTALK_APPSECRET') \
|
|
|
|
or settings.DINGTALK_APPSECRET \
|
|
|
|
or ''
|
2021-03-24 11:01:35 +00:00
|
|
|
|
|
|
|
try:
|
2021-10-18 03:25:39 +00:00
|
|
|
dingtalk = DingTalk(appid=app_key, appsecret=app_secret, agentid=agent_id)
|
2021-05-17 06:20:51 +00:00
|
|
|
dingtalk.send_text(['test'], 'test')
|
2021-05-19 07:51:55 +00:00
|
|
|
return Response(status=status.HTTP_200_OK, data={'msg': _('Test success')})
|
2021-05-17 06:20:51 +00:00
|
|
|
except APIException as e:
|
2021-10-18 03:25:39 +00:00
|
|
|
if 'errmsg' in e.detail:
|
2021-05-19 07:51:55 +00:00
|
|
|
error = e.detail['errmsg']
|
2021-10-18 03:25:39 +00:00
|
|
|
else:
|
2021-05-19 07:51:55 +00:00
|
|
|
error = e.detail
|
|
|
|
return Response(status=status.HTTP_400_BAD_REQUEST, data={'error': error})
|