def put(self, request): ''' 实现添加邮箱逻辑 :param request: :return: ''' json_str = request.body.decode() json_dict = json.loads(json_str) email = json_dict.get('email') # 校验参数 ---->正则 if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return HttpResponseForbidden('参数email有误') # 给 email 赋值 try: request.user.email = email request.user.save() except Exception as e: logger.error(e) return JsonResponse({'code': RETCODE.EMAILERR, 'errmsg': '添加邮箱失败'}) # 4.异步发送邮件 verify_url = generate_verify_email_url(request.user) from celery_tasks.email.tasks import send_verify_email send_verify_email.delay(email, verify_url) return JsonResponse({'code': RETCODE.OK, 'errmsg': '添加邮箱成功'})
def put(self, request): # 1、提取参数 data = json.loads(request.body.decode()) email = data.get('email') # 2、校验参数 if not email: return JsonResponse({'code': 400, 'errmsg': '缺少email'}) if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return JsonResponse({'code': 400, 'errmsg': '邮箱格式有误!'}) # 3、数据处理(部分更新) ———— 更新邮箱 user = request.user try: user.email = email user.email_active = False user.save() except Exception as e: print(e) # ======发送邮箱验证邮件======= verify_url = user.generate_verify_email_url() send_verify_email.delay(email, verify_url) # 异步调用! # 4、构建响应 return JsonResponse({'code': 0, 'errmsg': 'ok'})
def put(self, request): """实现添加邮箱逻辑""" # 接收参数 json_dict = json.loads(request.body.decode()) email = json_dict.get('email') # 校验参数 if not email: return http.JsonResponse({'code': 400, 'errmsg': '缺少email参数'}) if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return http.JsonResponse({'code': 400, 'errmsg': '参数email有误'}) # 赋值 email 字段 try: request.user.email = email request.user.save() except Exception as e: logger.error(e) return http.JsonResponse({'code': 400, 'errmsg': '添加邮箱失败'}) from celery_tasks.email.tasks import send_verify_email # 调用发送的函数: verify_url = request.user.generate_verify_email_url() send_verify_email.delay(email, verify_url) return http.JsonResponse({'code': 0, 'errmsg': '添加邮箱成功'})
def put(self, request): """实现添加邮箱逻辑""" # 接收请求体数据参数 json_dict = json.loads(request.body.decode()) email = json_dict.get("email") # 校验参数 if not email: return http.HttpResponseForbidden({ 'code': RETCODE.NECESSARYPARAMERR, 'errmsg': '缺少email参数' }) if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return http.HttpResponseForbidden({ 'code': RETCODE.EMAILERR, 'errmsg': '邮箱格式错误' }) # 修改用户的email字段 user = request.user # user.email = email # user.save() User.objects.filter(username=user.username, email='').update(email=email) # 在此就应该对当前设置的邮箱发一封激活邮件 # from django.core.mail import send_mail # send_mail() # 生成邮箱激活url verify_url = generate_email_verify_url(user) # 使用celery异步发送邮件 send_verify_email.delay(email, verify_url) # 响应 return http.JsonResponse({'code': RETCODE.OK, 'errmsg': '添加邮箱成功'})
def put(self, request): """实现添加邮箱逻辑""" # 接收参数 json_dict = json.loads(request.body.decode()) email = json_dict.get('email') # 校验参数 if not email: return http.HttpResponseForbidden('缺少email参数') if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return http.HttpResponseForbidden('参数email有误') # 赋值 email 字段 try: request.user.email = email request.user.save() except Exception as e: logger.error(e) # DBERR = "5000" return http.JsonResponse({ 'code': RETCODE.DBERR, 'errmsg': '添加邮箱失败' }) verify_url = request.user.generate_verify_email_url() send_verify_email.delay(email, verify_url) # 响应添加邮箱结果 return http.JsonResponse({'code': RETCODE.OK, 'errmsg': '添加邮箱成功'})
def put(self, request): # 接收请求体email数据 json_dict = json.loads(request.body.decode()) email =json_dict.get('email') # 校验 if all([email]) is None: return http.HttpResponseForbidden('清输入邮箱') if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return http.HttpResponseForbidden('邮箱格式错误') # 获取user user = request.user # 设置user.email字段 user.email = email # 用save保存 user.save() # 发送验证邮件,用celery verify_url = generate_verify_email_url(user) # 生成激活链接 send_verify_email.delay(email,verify_url) # 响应 return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
def put(self, request): '''保存数据哭''' # 1.接受参数 dict = json.loads(request.body.decode()) email = dict.get('email') # 2.检验参数:判断该参数是否有值 if not email: return JsonResponse({'code': 400, 'errmsg': '缺少必传参数'}) # 3.检验email的格式 if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return JsonResponse({'code': 400, 'errmsg': 'email格式不正确'}) try: # 4.把前端发送的email复制给用户 request.user.email = email # 5.保存 request.user.save() except Exception as e: logger.error(e) return JsonResponse({'code': 400, 'errmsg': '添加邮箱失败'}) # 响应添加邮箱结果 # 6.给挡墙邮箱发送一份信 # # 调用发送的函数: # # 用定义好的函数替换原来的字符串: verify_url = request.user.generate_access_token() # # 发送验证链接: send_verify_email.delay(email, verify_url) # 7.返回json 结果 return JsonResponse({'code': 0, 'errmsg': '添加邮箱成功'})
def put(self,request): '''实现邮箱添加逻辑''' # 接收参数 json_str = request.body.decode() json_dict = json.loads(json_str) email = json_dict['email'] print("邮箱是:",email) # 校验参数 if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return http.HttpResponseForbidden('参数email有误') # 赋值email字段 try: request.user.email = email request.user.save() except Exception as e: logger.error(e) return http.JsonResponse({'code': RETCODE.DBERR, 'errmsg': '添加邮箱失败'}) # 拼接url from apps.users.utils import generate_verify_email_url verify_url = generate_verify_email_url(request.user) # 4.异步发送邮件 from celery_tasks.email.tasks import send_verify_email send_verify_email.delay(email, verify_url) # 5.响应添加邮箱结果 return http.JsonResponse({'code': RETCODE.OK, 'errmsg': '添加邮箱成功'})
def update(self, instance, validated_data): #获取邮箱 email = validated_data.get('email') #保存数据 instance.email = email instance.save() #发送激活邮件 #这是同步发送邮件 # subject = '美多商城注册邮件' # message = '' # from_email = settings.EMAIL_FROM # recipient_list = [email] # html_message = "<h1>激活邮件</h1>" # send_mail(subject=subject, # message=message, # from_email=from_email, # recipient_list=recipient_list, # html_message=html_message # ) verify_url = instance.generic_email_token(email) # subject = "美多商城邮箱验证" # html_message = '<p>尊敬的用户您好!</p>' \ # '<p>感谢您使用美多商城。</p>' \ # '<p>您的邮箱为:%s 。请点击此链接激活您的邮箱:</p>' \ # '<p><a href="%s">%s<a></p>' % (email, verify_url, verify_url) # send_mail(subject, "", settings.EMAIL_FROM, [email], html_message=html_message) from celery_tasks.email.tasks import send_verify_email send_verify_email.delay(email, verify_url) return instance
def put(self, request): """ 获取用户邮箱,修改数据库中的邮箱 :param request: :return: """ # 1.接受参数 json_dict = json.loads(request.body.decode()) email = json_dict.get('email') # 2. 校验参数 if not email: return http.HttpResponseForbidden('缺少必传参数') if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return http.HttpResponseForbidden('email格式不正确') # 3. 修改保存到数据库 try: request.user.email = email request.user.save() except Exception as e: logger.error(e) return http.JsonResponse({ 'code': RETCODE.DBERR, 'errmsg': '写入数据库出错' }) verify_url = request.user.generate_verify_url(email) # 发送验证码待做 send_verify_email.delay(email, verify_url) # 4.返回 return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'ok'})
def put(self, request): # 接收请求体email数据 json_dict = json.loads(request.body.decode()) email = json_dict.get('email') # 校验 if all([email]) is None: return http.HttpResponseForbidden('缺少邮箱数据') if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return http.HttpResponseForbidden('邮箱格式有误') # 获取到user user = request.user # 设置user.email字段 user.email = email # 调用save保存 user.save() # 在此地还要发送一个邮件到email # from django.core.mail import send_mail # # send_mail(邮件主题, 普通邮件正文, 发件人邮箱, [收件人邮件], html_message='超文本邮件内容') # send_mail('美多', '', '美多商城<*****@*****.**>', [email], html_message='收钱了') # verify_url = 'http://www.meiduo.site:8000/emails/verification/?token=2' verify_url = generate_verify_email_url(user) # 生成邮箱激活url send_verify_email.delay(email, verify_url) # 响应 return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
def put(self, request): # 1. 接收参数,非表单请求体数据,通过request.body json_str = request.body.decode() json_dict = json.loads(json_str) email = json_dict.get('email') # 2.校验参数 if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return http.HttpResponseForbidden('参数email有误') # 3. 将用户输入传用度的邮箱保存到用户数据库的email字段中 try: request.user.email = email # request.user当前登录的对象绑定emial字段 request.user.save() except Exception as e: logger.error(e) return http.JsonResponse({ 'code': RETCODE.DBERR, 'errmsg': '添加邮箱失败' }) # 4. 调用加密后的验证邮件链接方法 verify_url = generate_verify_email_url(request.user) # 从当前登录用户中取出user # 使用celery发送邮件 send_verify_email.delay(email, verify_url) # 接收邮件地址,验证邮件链接 # 响应结果 return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
def put(self, request): # 1.接收数据 json_dict = json.loads(request.body.decode()) email = json_dict.get("email") # 2.校验 if not email: return http.HttpResponseForbidden('缺少email参数') if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return http.HttpResponseForbidden('参数email有误') # 3.修改用户的email字段 user = request.user # user.email = email # user.save() User.objects.filter(username=user.username, email="").update(email=email) # 再次给用户发一封激活邮件 verify_url = generate_email_verify_url(user) # 生成激活邮箱url send_verify_email.delay(email, verify_url) # 使用异步任务 # 4.响应 return http.JsonResponse({"code": RETCODE.OK, "errmsg": "添加邮箱成功"})
def update(self, instance, validated_data): instance.email = validated_data['email'] instance.save() # 生成激活连接 verify_url = instance.generate_email_url() send_verify_email.delay(instance.email, verify_url) return instance
def put(self, request): """实现添加邮箱的逻辑""" # 1.接受参数 json_dict = json.loads(request.body.decode()) email = json_dict.get('email') # 2.校验参数 if not email: return http.HttpResponseForbidden('缺少必传参数') if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return http.HttpResponseForbidden('参数email有误') # 3.更新 try: request.user.email = email request.user.save() except Exception as e: logger.error(e) return http.JsonResponse({ 'code': RETCODE.DBERR, 'errmsg': '添加邮箱失败' }) # 发送邮件 from celery_tasks.email.tasks import send_verify_email # 异步发送电子邮件 verify_url = request.user.generate_verify_email_url() send_verify_email.delay(email, verify_url) # 4.响应 return http.JsonResponse({ 'code': RETCODE.OK, 'errmsg': 'ok' })
def update(self, instance, validated_data): """设置用户邮箱,给用户邮箱发送激活邮件""" # 设置用户邮箱 email = validated_data['email'] instance.email = email instance.save() # 给用户邮箱发送激活邮件 # 激活邮件中需要包含激活链接: # http://www.meiduo.site:8080/success_verify_email.html?user_id=<user_id> # 为了防止用户进行恶意请求,在产生激活链接时,将用户信息进行加密生成token,将加密后的token放在激活链接中 # http://www.meiduo.site:8080/success_verify_email.html?token=<token> verify_url = instance.generate_verify_email_url() # TODO: 发送邮件 # subject = "美多商城邮箱验证" # html_message = '<p>尊敬的用户您好!</p>' \ # '<p>感谢您使用美多商城。</p>' \ # '<p>您的邮箱为:%s 。请点击此链接激活您的邮箱:</p>' \ # '<p><a href="%s">%s<a></p>' % (email, verify_url, verify_url) # send_mail(subject, '', settings.EMAIL_FROM, [email], html_message=html_message) # 发出发送邮件的任务 from celery_tasks.email.tasks import send_verify_email send_verify_email.delay(email, verify_url) return instance
def put(self, request): # 1.提取参数 data = json.loads(request.body.decode()) email = data.get('email') # 2.校验参数 if not email: return JsonResponse({'code': 400, 'errmsg': '没有传出机密文件的工具'}) if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return JsonResponse({'code': 0, 'errmsg': '你是在使用打狗棒通信吗?!'}) # 3.数据处理 user = request.user try: user.email = email user.email_active = False user.save() except Exception as e: logger.error(e) return JsonResponse({'code': 400, 'errmsg': '添加卧底同僚失败'}) # 发送邮箱验证邮件 verify_url = user.generate_verify_email_url() send_verify_email.delay(email, verify_url) # 数据响应 return JsonResponse({'code': 0, 'errmsg': 'ok'})
def update(self, instance, validated_data): instance.email = validated_data.get('email') email = instance.email instance.save() verify_url = instance.generate_verify_email_url() send_verify_email.delay(email, verify_url) return instance
def put(self, request): json_dict = json.loads(request.body.decode()) email = json_dict.get('email') if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return HttpResponseForbidden('邮箱格式错误') # 获取当前登录用户 user = request.user try: # 每次都修改email # user.email = email # user.save() # 只修改一次 User.objects.filter(username=user.username, email='').update(email=email) except Exception as e: logger.error(e) return JsonResponse({'code': RETCODE.DBERR, 'errmsg': '添加邮箱失败'}) verify_url = generate_verify_email_url(user) # 异步任务发送验证邮件 send_verify_email.delay(email, verify_url) # 响应添加邮箱结果 return JsonResponse({'code': RETCODE.OK, 'errmsg': '添加邮箱成功'})
def put(self, request): """实现添加邮箱逻辑""" # 接收参数 json_dict = json.loads(request.body.decode()) email = json_dict.get('email') # 校验参数 if not email: return JsonResponse({'code': 400, 'errmsg': '缺少email参数'}) if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return JsonResponse({'code': 400, 'errmsg': '参数email有误'}) # 赋值 email 字段 try: # HttpRequest.user实际上是由一个定义在django.contrib.auth.models # 中的usermodel类所创建的对象。 request.user.email = email request.user.save() except Exception as e: logger.error(e) return JsonResponse({'code': 400, 'errmsg': '添加邮箱失败'}) verify_url = request.user.generate_verify_email_url() send_verify_email.delay(email, verify_url) # 响应添加邮箱结果 return JsonResponse({'code': 0, 'errmsg': 'ok'})
def put(self, request): # 1.接收 请求体非表单参数 json json_bytes = request.body json_str = json_bytes.decode() json_dict = json.loads(json_str) email = json_dict.get('email') # 2.正则校验邮箱 if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return http.HttpResponseForbidden('参数email有误') # 3.修改 数据库中 email 值 try: request.user.email = email request.user.save() except Exception as e: logger.error(e) return http.JsonResponse({'code': RETCODE.DBERR, 'errmsg': '添加邮箱失败'}) # 自动发邮件(需要加密) token_value = { 'user_id': request.user.id, 'email': email } # 加密 --->对象方法SecretOauth() from utils.secret import SecretOauth secret_str = SecretOauth().dumps(token_value) # http://www.meiduo.site:8000/emails/verification/?token={%22user_id%22:%201,%20%22email%22:%20%[email protected]%22} verify_url = settings.EMAIL_ACTIVE_URL + "?token=" + secret_str from celery_tasks.email.tasks import send_verify_email send_verify_email.delay(email, verify_url) # 4.返回前端结果 return http.JsonResponse({'code': RETCODE.OK, 'errmsg': '添加邮箱成功'})
def put(self, request): """ :param request:请求对象 :return:添加邮箱结果 """ # 接收参数 json_str = request.body.decode() json_dict = json.loads(json_str) email = json_dict.get('email') # 校验参数 if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return http.HttpResponseForbidden('参数email有误') # 将用户传入的邮箱保存到用户数据库的email字段中 try: request.user.email = email request.user.save() except Exception as e: logger.error(e) return http.JsonResponse({ 'code': RETCODE.DBERR, 'errmsg': '添加邮箱失败' }) # 生成邮箱激活链接 verify_url = generate_verfy_email_url(request.user) # 异步发送验证邮件 # send_verify_email(email, verify_url) # 错误写法 send_verify_email.delay(email, verify_url) # 记得调用delay # 响应添加邮箱结果 return http.JsonResponse({'code': RETCODE.OK, 'errmsg': '添加邮箱成功'})
def put(self, request): '''保存email到数据库, 给邮箱发送邮件''' #1.接收参数(json) dict = json.loads(request.body.decode()) email = dict.get('email') #2.检验参数:判断该参数是否有值 if not email: return JsonResponse({'code': 400, 'errmsg': '缺少必传参数'}) #3. 检验email的格式 if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return JsonResponse({'code': 400, 'errmsg': 'email格式不对'}) try: #4. 把前端发送的email赋值给当前用户 request.user.email = email #5. 保存 request.user.save() except Exception as e: return JsonResponse({'code': 400, 'errmsg': '保存邮箱失败'}) # 变换 email 的格式: 从 '*****@*****.**' 变为: '<*****@*****.**>' email = '<' + email + '>' #6. 给当前邮箱发送一封信 verify_url = request.user.generate_access_token() send_verify_email.delay(email, verify_url) #7. 返回json参数 return JsonResponse({'code': 0, 'errmsg': 'ok'})
def put(self, request): # 接收參數 json_str = request.body.decode() json_dict = json.loads(json_str) email = json_dict.get('email') # 校驗參數 if not email: return http.HttpResponseForbidden('缺少email參數') if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return http.HttpResponseForbidden('參數email有誤') # 將用戶傳入的email保存到用戶表的email欄位 try: request.user.email = email request.user.save() except Exception as e: logger.error(e) return http.JsonResponse({'code': RETCODE.DBERR, 'errmsg': '新增郵箱失敗'}) # 發送驗證郵件 verify_url = generate_verify_email_url(request.user) send_verify_email.delay(email, verify_url) # 響應結果 return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
def put(self, request): # 接收数据 json_dict = json.loads(request.body.decode()) email = json_dict.get('email') if User.objects.filter(email=email): return http.JsonResponse({ 'code': RETCODE.PARAMERR, 'errmsg': '存在' }) # 校验 if email is None: return http.HttpResponseForbidden('缺少必传参数') if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return http.HttpResponseForbidden('邮件格式不正确') # 处理业务逻辑 user = request.user # user.email = email # 此写法,在当前场景会随着重新发送邮箱时,重复设置邮箱 # user.save() User.objects.filter(id=user.id, email='').update( email=email) # 邮箱只要设置成功了,此代码都是无效的修改 # 在此顺带的发一个激活邮件出去 # from django.core.mail import send_mail # send_mail(subject='主题', message='邮件普通正文', from_email='发件人', recipient_list='收件人,必须是列表', # html_message='<a href='xxx'>xdfsfd<a>') # verify_url = 'http://www.meiduo.site:8000/emails/verification/?token=3' # 生成激活url verify_url = generate_verify_email_url(user) # celery异步发邮件 send_verify_email.delay(email, verify_url) # 响应 return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})
def put(self, request): """实现添加邮箱逻辑""" # 接收参数 json_dict = json.loads(request.body.decode()) email = json_dict.get('email') # 校验参数 if not email: return http.JsonResponse({'code': 400, 'errmsg': '缺少email参数'}) if not re.match(r'^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$', email): return http.JsonResponse({'code': 400, 'errmsg': '参数email有误'}) # 赋值 email 字段 try: request.user.email = email request.user.save() except Exception as e: logger.error(e) return http.JsonResponse({'code': 400, 'errmsg': '添加邮箱失败'}) # 变换 email 的格式: 从 '*****@*****.**' 变为: '<*****@*****.**>' email = '<' + email + '>' # 从 celery_tasks 中导入: from celery_tasks.email.tasks import send_verify_email # 调用发送的函数: # 用定义好的函数替换原来的字符串: verify_url = request.user.generate_verify_email_url() # 发送验证链接: send_verify_email.delay(email, verify_url) return http.JsonResponse({'code': 0, 'errmsg': '添加邮箱成功'})
def put(self, request): """ 实现添加邮箱逻辑 :param request: :return: """ # 接受参数 json_dict = json.loads(request.body.decode()) email = json_dict.get('email') # 检验参数 if not email: return http.HttpResponseForbidden('缺少email参数') if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return http.HttpResponseForbidden('请输入正确email') # 赋值 email 字段 保存 try: request.user.email = email request.user.save() except Exception as f: logger.error(f) return http.JsonResponse({ 'code': RETCODE.DBERR, 'errmsg': '邮箱添加失败' }) #导入邮件异步发送程序 from celery_tasks.email.tasks import send_verify_email verify_url = request.user.generate_verify_email_url() send_verify_email.delay(email, verify_url) return http.JsonResponse({'code': RETCODE.OK, 'errmsg': '10k'})
def put(self, request): # 更新、修改 json_str = request.body.decode() json_dict = json.loads(json_str) email = json_dict.get('email') if email is None: return http.HttpResponseForbidden('缺少必传参数') if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return http.HttpResponseForbidden('参数有误') # 保存到数据库 try: # User.object.create(email=email) request.user.email = email request.user.save() except Exception as e: logger.error(e) return http.JsonResponse({ 'code': RETCODE.DBERR, 'errmsg': '邮箱格保存失败' }) # 异步发送邮件 verify_url = generate_verify_email_url( request.user) # request.user 当前登录用户 send_verify_email.delay(email, verify_url) # email 收件人邮箱 return http.JsonResponse({'code': RETCODE.OK, 'errmsg': '邮箱格保存成功'})
def put(self, request): """实现添加邮箱逻辑""" # 接收参数 json_dict = json.loads(request.body.decode()) email = json_dict['email'] # 校验参数 if not email: return http.HttpResponseForbidden('缺少必传参数') if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return http.HttpResponseForbidden('参数email有误') # 赋值 email 字段 try: request.user.email = email request.user.save() except Exception as e: logger.error(e) return http.JsonResponse({'code': RETCODE.DBERR, 'errmsg': '添加邮箱失败'}) # 导入: from celery_tasks.email.tasks import send_verify_email # 异步发送验证邮件 verify_url = request.user.generate_verify_email_url() # 发送验证链接: send_verify_email.delay(email, verify_url) # 响应添加邮箱结果 return http.JsonResponse({'code': RETCODE.OK, 'errmsg': '添加邮箱成功'})
def update(self, instance, validated_data): '''此方法重写目的不是为了修改,而是借用此时机 发送激活邮箱''' print('instance--------->', instance) print('validated_data---------->', validated_data) instance.email = validated_data.get('email') instance.save() print('instance--------->', type(instance)) ''' instance---------> liuhaoli validated_data----------> {'email': '*****@*****.**'} instance---------> <class 'users.models.User'> ''' # 将来需要在此继续写发邮箱的功能 # 异步发邮件 # ⽣成激活链接 # token 后面需要传用户对象,user,直接在模型中定义,self即用户对象 # http://www.meiduo.site:8080/success_verify_email.html?token=1' # verify_url = '使⽤itsdangerous⽣成激活链接' verify_url = instance.generate_email_verify_url() send_verify_email.delay(instance.email, verify_url=verify_url) return instance
def put(self,request): #取出请求参数中的email json_str = request.body.decode() json_dict = json.loads(json_str) email = json_dict.get('email') #校验email格式是否满足要求 # if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): # return http.HttpResponseForbidden('参数email有误') #保存email try : request.user.email = email request.user.save() except Exception as e: logger.error(e) return http.JsonResponse({'code': RETCODE.DBERR,'errmsg':'添加邮箱失败'}) token_value = { 'user_id': request.user.id, 'email': email } from utils.secret import SecretOauth secret_str = SecretOauth().dumps(token_value) verify_url = settings.EMAIL_ACTIVE_URL + "?token=" + secret_str from celery_tasks.email.tasks import send_verify_email send_verify_email.delay(email, verify_url) # 4. 返回前端结果 return http.JsonResponse({'code': RETCODE.OK, 'errmsg': '添加邮箱成功'})