diff --git a/apps/common/sdk/sms/custom.py b/apps/common/sdk/sms/custom.py index 23a03762a..9dff52d00 100644 --- a/apps/common/sdk/sms/custom.py +++ b/apps/common/sdk/sms/custom.py @@ -39,8 +39,7 @@ class CustomSMS(BaseSMSClient): kwargs = {'params': params} try: response = action(url=settings.CUSTOM_SMS_URL, verify=False, **kwargs) - if response.reason != 'OK': - raise JMSException(detail=response.text, code=response.status_code) + response.raise_for_status() except Exception as exc: logger.error('Custom sms error: {}'.format(exc)) raise JMSException(exc) diff --git a/apps/common/utils/verify_code.py b/apps/common/utils/verify_code.py index 115752f82..dea844768 100644 --- a/apps/common/utils/verify_code.py +++ b/apps/common/utils/verify_code.py @@ -13,9 +13,9 @@ from common.utils.random import random_string logger = get_logger(__file__) -@shared_task(verbose_name=_('Send email')) -def send_async(sender): - sender.gen_and_send() +@shared_task(verbose_name=_('Send SMS code')) +def send_sms_async(target, code): + SMS().send_verify_code(target, code) class SendAndVerifyCodeUtil(object): @@ -35,7 +35,7 @@ class SendAndVerifyCodeUtil(object): logger.warning('Send sms too frequently, delay {}'.format(ttl)) raise CodeSendTooFrequently(ttl) - return send_async.apply_async(kwargs={"sender": self}, priority=100) + return self.gen_and_send() def gen_and_send(self): try: @@ -72,13 +72,15 @@ class SendAndVerifyCodeUtil(object): return code def __send_with_sms(self): - sms = SMS() - sms.send_verify_code(self.target, self.code) + send_sms_async.apply_async(args=(self.target, self.code), priority=100) def __send_with_email(self): - subject = self.other_args.get('subject') - message = self.other_args.get('message') - send_mail_async(subject, message, [self.target], html_message=message) + subject = self.other_args.get('subject', '') + message = self.other_args.get('message', '') + send_mail_async.apply_async( + args=(subject, message, [self.target]), + kwargs={'html_message': message}, priority=100 + ) def __send(self, code): """ diff --git a/apps/ops/signal_handlers.py b/apps/ops/signal_handlers.py index 1d1920e05..4eedb7a7b 100644 --- a/apps/ops/signal_handlers.py +++ b/apps/ops/signal_handlers.py @@ -60,7 +60,7 @@ def check_registered_tasks(*args, **kwargs): 'perms.tasks.check_asset_permission_will_expired', 'ops.tasks.create_or_update_registered_periodic_tasks', 'perms.tasks.check_asset_permission_expired', 'settings.tasks.ldap.import_ldap_user_periodic', 'users.tasks.check_password_expired_periodic', - 'common.utils.verify_code.send_async', 'assets.tasks.nodes_amount.check_node_assets_amount_period_task', + 'common.utils.verify_code.send_sms_async', 'assets.tasks.nodes_amount.check_node_assets_amount_period_task', 'users.tasks.check_user_expired', 'orgs.tasks.refresh_org_cache_task', 'terminal.tasks.upload_session_replay_to_external_storage', 'terminal.tasks.clean_orphan_session', 'audits.tasks.clean_audits_log_period', 'authentication.tasks.clean_django_sessions'