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-05-17 06:20:51 +00:00
|
|
|
from settings.models import Setting
|
2021-03-24 11:01:35 +00:00
|
|
|
from common.permissions import IsSuperUser
|
2021-05-17 06:20:51 +00:00
|
|
|
from common.message.backends.dingtalk import DingTalk
|
2021-03-24 11:01:35 +00:00
|
|
|
|
|
|
|
from .. import serializers
|
|
|
|
|
|
|
|
|
|
|
|
class DingTalkTestingAPI(GenericAPIView):
|
|
|
|
permission_classes = (IsSuperUser,)
|
|
|
|
serializer_class = serializers.DingTalkSettingSerializer
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
serializer = self.serializer_class(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
|
|
|
dingtalk_appkey = serializer.validated_data['DINGTALK_APPKEY']
|
|
|
|
dingtalk_agentid = serializer.validated_data['DINGTALK_AGENTID']
|
2021-05-17 06:20:51 +00:00
|
|
|
dingtalk_appsecret = serializer.validated_data.get('DINGTALK_APPSECRET')
|
|
|
|
|
|
|
|
if not dingtalk_appsecret:
|
|
|
|
secret = Setting.objects.filter(name='DINGTALK_APPSECRET').first()
|
2021-05-19 07:51:55 +00:00
|
|
|
if secret:
|
|
|
|
dingtalk_appsecret = secret.cleaned_value
|
|
|
|
|
|
|
|
dingtalk_appsecret = dingtalk_appsecret or ''
|
2021-03-24 11:01:35 +00:00
|
|
|
|
|
|
|
try:
|
2021-05-17 06:20:51 +00:00
|
|
|
dingtalk = DingTalk(appid=dingtalk_appkey, appsecret=dingtalk_appsecret, agentid=dingtalk_agentid)
|
|
|
|
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-05-19 07:51:55 +00:00
|
|
|
try:
|
|
|
|
error = e.detail['errmsg']
|
|
|
|
except:
|
|
|
|
error = e.detail
|
|
|
|
return Response(status=status.HTTP_400_BAD_REQUEST, data={'error': error})
|