def send_sms(mobile, code, template): ''' 向指定手机发送验证 ''' sms_config = SMSConfig.get_current() smser = SMSAliyunManager( access_key=sms_config.access_key, access_key_secret=sms_config.access_secret, ) signature = sms_config.signature if is_cn_mobile( mobile) else sms_config.signature_i18n mobile = ''.join([literal for literal in mobile if literal.isdigit()]) # `+86 18812341234` -> `8618812341234` try: smser.send_auth_code( mobile=mobile, vc_code=code, sign_name=signature, template_code=template, ) except RuntimeError as exc: if 'MOBILE_NUMBER_ILLEGAL' in repr(exc): raise ValidationError({'mobile': 'invalid'}) raise exc
def get_template_id(self): # pylint: disable=no-self-use ''' 读取模板id ''' sms_config = SMSConfig.get_current() i18n = not is_cn_mobile( self.validated_data['mobile']) # 国内号码即使加上区号,也无法使用国际模板 return sms_config.get_template_code("code", i18n)
def get_template_id(self): ''' 读取模板ID ''' sms_config = SMSConfig.get_current() i18n = not is_cn_mobile(self.validated_data['mobile']) return sms_config.get_template_code("reset_mobile", i18n) or \ sms_config.get_template_code("code", i18n)
def save(self, *args, **kwargs): # pylint: disable=arguments-differ,signature-differs for unique_feilds in [ 'username', 'email', 'mobile', 'private_email', ]: value = getattr(self, unique_feilds) if not value: continue existed = False if unique_feilds == 'mobile': if not is_mobile(value): raise ValidationError({unique_feilds: ['invalid']}) if is_i18n_mobile(value) and is_cn_mobile(value): existed = User.valid_objects.filter( models.Q(mobile=value) | models.Q(mobile=value.replace(CN_MOBILE_PREFIX, "")) ).exclude(pk=self.pk).exists() # pylint: disable=no-member elif is_native_mobile(value): existed = User.valid_objects.filter( models.Q(mobile=value) | models.Q(mobile=CN_MOBILE_PREFIX + value)).exclude( pk=self.pk).exists() else: existed = User.valid_objects.filter(mobile=value).exclude( pk=self.pk).exists() else: _kwargs = {unique_feilds: value} existed = User.valid_objects.filter(**_kwargs).exclude( pk=self.pk).exists() if existed: msg = "UNIQUE constraint failed: " + \ f"oneid_meta.User UniqueConstraint(fields=['{unique_feilds}'], condition=Q(is_del='False')" print(msg) raise ValidationError({unique_feilds: ['existed']}) super(User, self).save(*args, **kwargs)
def create_user_meta(db_executer, user): """ 创建user记录 :param user:一条dinguser数据 :return: """ # 手机号码校验 # 手机号存在且非中国号码时,需将区号 # 以 `+852 98765432` 的形式附上 mobile = user.get('mobile', '') mobile = f"+{user.get('stateCode', '')} {mobile}" if mobile and not is_cn_mobile( mobile) else mobile user_info = { 'password': '', 'name': user.get('name'), 'private_email': user.get('email', ''), 'email': user.get('orgEmail', ''), 'mobile': mobile, 'employee_number': user.get('jobnumber', ''), 'gender': UNKNOWN_GENDER, 'ding_user': { 'uid': user.get('userid'), 'account': user.get('mobile', ''), 'data': json.dumps({ 'uname': user.get('name'), 'position': user.get('position', ''), 'tel': user.get('tel', ''), 'workplace': user.get('workPlace', ''), 'remark': user.get('remark', ''), 'email': user.get('email', ''), 'orgemail': user.get('orgEmail', ''), 'jobnumber': user.get('jobnumber', ''), 'ishide': user.get('isHide', ''), 'issenior': user.get('issenior', ''), }) } } # 通过钉钉uid匹配 exist_ding_user = DingUser.valid_objects.filter( uid=user.get('userid')).first() if exist_ding_user: return db_executer.update_user(exist_ding_user.user, user_info) # 通过手机号匹配 exist_mobile_user = User.valid_objects.filter(mobile=mobile).first() if exist_mobile_user: return db_executer.update_user(exist_mobile_user, user_info) # 通过私人邮箱匹配 private_email = user.get('email', '') if private_email != '': exist_private_email_user = User.valid_objects.filter( private_email=private_email).first() if exist_private_email_user: return db_executer.update_user(exist_private_email_user, user_info) # 通过企业邮箱匹配 email = user.get('orgEmail', '') if email != '': exist_email_user = User.valid_objects.filter(email=email).first() if exist_email_user: return db_executer.update_user(exist_email_user, user_info) user_info['username'] = gen_uid(user_info['name'], cls=User, uid_field='username') return db_executer.create_user(user_info)