def post(self, request): # 获取参数 old_password = request.POST.get('old_password') new_password = request.POST.get('new_password') new_password2 = request.POST.get('new_password2') # 校验参数 if not all([old_password, new_password, new_password2]): return http.HttpResponseForbidden('参数不全') if not re.match(r'^[0-9a-zA-Z_-]{8,20}$', new_password): return http.HttpResponseBadRequest('请输入8-20位的密码') if new_password != new_password2: return http.HttpResponseBadRequest('两次输入的密码不一致') # 判断旧密码是否正确 if not request.user.check_password(old_password): return render(request, 'user_center_pass.html', {'origin_password_errmsg': '请输入正确的旧密码'}) # 更新新密码 try: request.user.set_password(new_password) request.user.save() except Exception as e: logger.error(e) return render(request, 'user_center_pass.html', {'change_password_errmsg': '修改新密码失败'}) # 退出登录, 删除登录信息 logout(request) # 跳转到登录页面 response = redirect(reverse('users:login')) # 删除cookie response.delete_cookie('username') return response
def delete(self, request, address_id): # 删除地址 try: # 查询要删除的地址 address = Address.objects.get(id=address_id) # 将地址逻辑删除设置为True address.is_deleted = True address.save() except Exception as e: logger.error(e) return http.JsonResponse({ 'code': RETCODE.DBERR, 'errmsg': '删除地址失败' }) return http.JsonResponse({'code': RETCODE.OK, 'errmsg': '删除地址成功'})
def put(self, request, address_id): try: # 接收参数,查询地址 address = Address.objects.get(id=address_id) # 设置地址为默认地址 request.user.default_address = address request.user.save() except Exception as e: logger.error(e) return http.JsonResponse({ 'code': RETCODE.DBERR, 'errmsg': '设置默认地址失败' }) # 响应设置默认地址 return http.JsonResponse({'code': RETCODE.OK, 'errmsg': '设置默认地址成功'})
def put(self, request, address_id): # 获取参数 json_dict = json.loads(request.body.decode()) title = json_dict.get('title') try: # 查询地址 address = Address.objects.get(id=address_id) # 设置新的地址标题 address.title = title address.save() except Exception as e: logger.error(e) return http.JsonResponse({ 'code': RETCODE.DBERR, 'errmsg': '设置地址标题失败' }) # 响应删除地址结果 return http.JsonResponse({'code': RETCODE.OK, 'errmsg': '设置地址标题成功'})
def get(self, request): # 接收参数 token = request.GET.get('token') # 校验参数: 判断token是否为空和过期,提取user if not token: return http.HttpResponseBadRequest('缺少token') user = check_verify_email_token(token) if not user: return http.HttpResponseBadRequest('无效的token') # 修改email_active的值位true try: user.email_active = True user.save() except Exception as e: logger.error(e) return http.HttpResponseServerError('激活邮件失败') # 返回验证结果 # return http.HttpResponse('激活成功') return redirect(reverse('users:center'))
def put(self, request): """实现添加邮箱逻辑""" # 接收axios body = request.body body_str = body.decode() data = json.loads(body_str) # 检验参数 email = data.get('email') if not email: return http.HttpResponseBadRequest('缺少email参数') if not re.match(r"^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$", email): return http.HttpResponseBadRequest('email参数有误') # 更新数据 try: request.user.email = email request.user.save() except Exception as e: logger.error(e) return http.JsonResponse({'code': 0, 'errmsg': '添加邮箱失败'}) # 给邮箱发送链接 # from django.core.mail import send_mail # # subject, message, from_email, recipient_list # subject = "美多商城激活邮件" # message = "" # from_email = "欢乐玩家<*****@*****.**>" # # 收件人列表 # recipient_list = ["*****@*****.**"] # html_message = "<a href='http://www.huyouni.com'>戳我有惊喜</a>" # send_mail(subject=subject, # message=message, # from_email=from_email, # recipient_list=recipient_list, # html_message=html_message) # 异步发送验证邮件 from celery_tasks.email.tasks import send_verify_email verify_url = generate_verify_email_url(request.user) send_verify_email.delay(email, verify_url) # 响应添加邮箱结果 return http.JsonResponse({"code": 0, "errmsg": "添加邮箱成功"})
def put(self, request, address_id): # 接收参数 json_dict = json.loads(request.body.decode()) receiver = json_dict.get('receiver') province_id = json_dict.get('province_id') city_id = json_dict.get('city_id') district_id = json_dict.get('district_id') place = json_dict.get('place') mobile = json_dict.get('mobile') tel = json_dict.get('tel') email = json_dict.get('email') # 校验参数 if not all( [receiver, province_id, city_id, district_id, place, mobile]): return http.HttpResponseBadRequest('缺少必传参数') if not re.match(r'^1[3-9]\d{9}$', mobile): return http.HttpResponseBadRequest('参数mobile有误') if tel: if not re.match( r'^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$', tel): return http.HttpResponseBadRequest('参数tel有误') if email: if not re.match( r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return http.HttpResponseBadRequest('参数email有误') # 判断地址是否存在,并更新地址信息 try: Address.objects.filter(id=address_id).update( user=request.user, title=receiver, receiver=receiver, province_id=province_id, city_id=city_id, district_id=district_id, place=place, mobile=mobile, tel=tel, email=email) except Exception as e: logger.error(e) return http.JsonResponse({ 'code': RETCODE.DBERR, 'errmsg': '更新地址失败' }) # 构造响应数据 address = Address.objects.get(id=address_id) address_dict = { "id": address_id, "title": address.title, "receiver": address.receiver, "province": address.province.name, "city": address.city.name, "district": address.district.name, "place": address.place, "mobile": address.mobile, "tel": address.tel, "email": address.email } # 响应更新地址结果 return http.JsonResponse({ 'code': RETCODE.OK, 'errmsg': '更新地址成功', 'address': address_dict })
def post(self, request): # 判断地址上限,最多20个 count = request.user.addresses.count() if count >= 20: return http.JsonResponse({ 'code': RETCODE.THROTTLINGERR, 'errmsg': '超过地址上限' }) # 接收参数 json_dict = json.loads(request.body.decode()) receiver = json_dict.get('receiver') province_id = json_dict.get('province_id') city_id = json_dict.get('city_id') district_id = json_dict.get('district_id') place = json_dict.get('place') mobile = json_dict.get('mobile') tel = json_dict.get('tel') email = json_dict.get('email') # 校验参数 if not all( [receiver, province_id, city_id, district_id, place, mobile]): return http.HttpResponseBadRequest('参数不全') if not re.match(r'^1[3-9]\d{9}$', mobile): return http.HttpResponseBadRequest('参数mobile有误') if tel: if not re.match( r'^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$', tel): return http.HttpResponseBadRequest('参数tel有误') if email: if not re.match( r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return http.HttpResponseBadRequest('参数email有误') # 保存地址信息 try: address = Address.objects.create(user=request.user, title=receiver, receiver=receiver, province_id=province_id, city_id=city_id, district_id=district_id, place=place, mobile=mobile, tel=tel, email=email) # 设置默认地址 if not request.user.default_address: request.user.default_address = address request.user.save() except Exception as e: logger.error(e) return http.JsonResponse({ 'code': RETCODE.DBERR, 'errmsg': '新增地址失败' }) # 新增地址成功,并将地址响应给前端实现局部刷新 address_dict = { 'id': address.id, 'title': address.title, 'receiver': address.receiver, 'province': address.province.name, 'city': address.city.name, 'district': address.district.name, 'place': address.place, 'mobile': address.mobile, 'tel': address.tel, 'email': address.email } # 响应保存结果 return http.JsonResponse({ 'code': RETCODE.OK, 'errmsg': '新增地址成功', 'address': address_dict })