将配置改到类属性中

pull/10409/head
jiangweidong 2023-05-08 15:36:33 +08:00 committed by Jiangjie.Bai
parent 1fef9a2cf0
commit 3f1858a105
4 changed files with 45 additions and 47 deletions

View File

@ -2,6 +2,7 @@ from functools import lru_cache
from rest_framework.request import Request from rest_framework.request import Request
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.utils.module_loading import import_string
from django.conf import settings from django.conf import settings
from django.db.utils import IntegrityError from django.db.utils import IntegrityError
from django.views import View from django.views import View
@ -20,18 +21,16 @@ logger = get_logger(__file__)
class BaseLoginCallbackView(AuthMixin, FlashMessageMixin, View): class BaseLoginCallbackView(AuthMixin, FlashMessageMixin, View):
def __init__(self): client_type_path = ''
super().__init__() client_auth_params = {}
self.client_type = None user_type = ''
self.client_auth_params = {} auth_backend = None
self.user_type = '' create_user_if_not_exist_setting = ''
self.auth_backend = None # 提示信息
self.create_user_if_not_exist_setting = '' msg_client_err = _('Error')
# 提示信息 msg_user_not_bound_err = _('Error')
self.msg_client_err = _('Error') msg_user_need_bound_warning = _('Error')
self.msg_user_not_bound_err = _('Error') msg_not_found_user_from_client_err = _('Error')
self.msg_user_need_bound_warning = _('Error')
self.msg_not_found_user_from_client_err = _('Error')
def verify_state(self): def verify_state(self):
raise NotImplementedError raise NotImplementedError
@ -42,10 +41,11 @@ class BaseLoginCallbackView(AuthMixin, FlashMessageMixin, View):
@property @property
@lru_cache(maxsize=1) @lru_cache(maxsize=1)
def client(self): def client(self):
if self.client_type is None or not self.client_auth_params: if not all([self.client_type_path, self.client_auth_params]):
raise NotImplementedError raise NotImplementedError
client_init = {k: getattr(settings, v) for k, v in self.client_auth_params.items()} client_init = {k: getattr(settings, v) for k, v in self.client_auth_params.items()}
return self.client_type(**client_init) client_type = import_string(self.client_type_path)
return client_type(**client_init)
def create_user_if_not_exist(self, user_id, **kwargs): def create_user_if_not_exist(self, user_id, **kwargs):
user = None user = None

View File

@ -198,18 +198,19 @@ class DingTalkQRLoginView(DingTalkQRMixin, METAMixin, View):
class DingTalkQRLoginCallbackView(DingTalkQRMixin, BaseLoginCallbackView): class DingTalkQRLoginCallbackView(DingTalkQRMixin, BaseLoginCallbackView):
permission_classes = (AllowAny,) permission_classes = (AllowAny,)
def __init__(self): client_type_path = 'common.sdk.im.dingtalk.DingTalk'
super(DingTalkQRLoginCallbackView, self).__init__() client_auth_params = {
self.client_type = DingTalk 'appid': 'DINGTALK_APPKEY', 'appsecret': 'DINGTALK_APPSECRET',
self.client_auth_params = {'appid': 'DINGTALK_APPKEY', 'appsecret': 'DINGTALK_APPSECRET', 'agentid': 'DINGTALK_AGENTID'} 'agentid': 'DINGTALK_AGENTID'
self.user_type = 'dingtalk' }
self.auth_backend = 'AUTH_BACKEND_DINGTALK' user_type = 'dingtalk'
self.create_user_if_not_exist_setting = 'DINGTALK_CREATE_USER_IF_NOT_EXIST' auth_backend = 'AUTH_BACKEND_DINGTALK'
create_user_if_not_exist_setting = 'DINGTALK_CREATE_USER_IF_NOT_EXIST'
self.msg_client_err = _('DingTalk Error') msg_client_err = _('DingTalk Error')
self.msg_user_not_bound_err = _('DingTalk is not bound') msg_user_not_bound_err = _('DingTalk is not bound')
self.msg_user_need_bound_warning = _('Please login with a password and then bind the DingTalk') msg_user_need_bound_warning = _('Please login with a password and then bind the DingTalk')
self.msg_not_found_user_from_client_err = _('Failed to get user from DingTalk') msg_not_found_user_from_client_err = _('Failed to get user from DingTalk')
class DingTalkOAuthLoginView(DingTalkOAuthMixin, View): class DingTalkOAuthLoginView(DingTalkOAuthMixin, View):

View File

@ -158,15 +158,14 @@ class FeiShuQRLoginView(FeiShuQRMixin, View):
class FeiShuQRLoginCallbackView(FeiShuQRMixin, BaseLoginCallbackView): class FeiShuQRLoginCallbackView(FeiShuQRMixin, BaseLoginCallbackView):
permission_classes = (AllowAny,) permission_classes = (AllowAny,)
def __init__(self): client_type_path = 'common.sdk.im.feishu.FeiShu'
super(FeiShuQRLoginCallbackView, self).__init__() client_auth_params = {'app_id': 'FEISHU_APP_ID', 'app_secret': 'FEISHU_APP_SECRET'}
self.client_type = FeiShu user_type = 'feishu'
self.client_auth_params = {'app_id': 'FEISHU_APP_ID', 'app_secret': 'FEISHU_APP_SECRET'} auth_backend = 'AUTH_BACKEND_FEISHU'
self.user_type = 'feishu' create_user_if_not_exist_setting = 'FEISHU_CREATE_USER_IF_NOT_EXIST'
self.auth_backend = 'AUTH_BACKEND_FEISHU'
self.create_user_if_not_exist_setting = 'FEISHU_CREATE_USER_IF_NOT_EXIST' msg_client_err = _('FeiShu Error')
msg_user_not_bound_err = _('FeiShu is not bound')
msg_user_need_bound_warning = _('Please login with a password and then bind the FeiShu')
msg_not_found_user_from_client_err = _('Failed to get user from FeiShu')
self.msg_client_err = _('FeiShu Error')
self.msg_user_not_bound_err = _('FeiShu is not bound')
self.msg_user_need_bound_warning = _('Please login with a password and then bind the FeiShu')
self.msg_not_found_user_from_client_err = _('Failed to get user from FeiShu')

View File

@ -193,18 +193,16 @@ class WeComQRLoginView(WeComQRMixin, METAMixin, View):
class WeComQRLoginCallbackView(WeComQRMixin, BaseLoginCallbackView): class WeComQRLoginCallbackView(WeComQRMixin, BaseLoginCallbackView):
permission_classes = (AllowAny,) permission_classes = (AllowAny,)
def __init__(self): client_type_path = 'common.sdk.im.wecom.WeCom'
super(WeComQRLoginCallbackView, self).__init__() client_auth_params = {'corpid': 'WECOM_CORPID', 'corpsecret': 'WECOM_SECRET', 'agentid': 'WECOM_AGENTID'}
self.client_type = WeCom user_type = 'wecom'
self.client_auth_params = {'corpid': 'WECOM_CORPID', 'corpsecret': 'WECOM_SECRET', 'agentid': 'WECOM_AGENTID'} auth_backend = 'AUTH_BACKEND_WECOM'
self.user_type = 'wecom' create_user_if_not_exist_setting = 'WECOM_CREATE_USER_IF_NOT_EXIST'
self.auth_backend = 'AUTH_BACKEND_WECOM'
self.create_user_if_not_exist_setting = 'WECOM_CREATE_USER_IF_NOT_EXIST'
self.msg_client_err = _('WeCom Error') msg_client_err = _('WeCom Error')
self.msg_user_not_bound_err = _('WeCom is not bound') msg_user_not_bound_err = _('WeCom is not bound')
self.msg_user_need_bound_warning = _('Please login with a password and then bind the WeCom') msg_user_need_bound_warning = _('Please login with a password and then bind the WeCom')
self.msg_not_found_user_from_client_err = _('Failed to get user from WeCom') msg_not_found_user_from_client_err = _('Failed to get user from WeCom')
class WeComOAuthLoginView(WeComOAuthMixin, View): class WeComOAuthLoginView(WeComOAuthMixin, View):