def register_email(request): username = request.POST.get("email").strip() password = request.POST.get("passwordB").strip() re_password = request.POST.get("passwordB2").strip() tel = int(request.POST["tel"]) if username == "": return render(request, "users/register.html", {"msg": "用户名称不能为空"}) if len(password) < 3: return render(request, "users/register.html", {"msg": "用户密码长度不能小于3位"}) if re_password == password: if models.Passport.objects.filter(username=username): return render(request, "users/register.html", {"msg": "注册失败用户名已存在"}) else: # 保存用户 s=transaction.savepoint() try: password = utils.md5_by_hmac(password) user = models.Passport(username=username, email=username, password=password, tel=tel) user.save() """ 发送激活邮件 """ send_email.delay(user.id, username, username) transaction.savepoint_commit(s) except: transaction.rollback(s) return render(request, "users/register.html", {"msg": "邮件已发送"}) else: return render(request, "users/register.html", {"msg": "注册失败两次密码不一致"})
def login(request): """ 用户登录功能 """ if request.method == "GET": return render(request, "users/login.html", {}) else: # 接受页面的参数 username = request.POST["username"].strip() password = request.POST["password"].strip() # verify_code = request.POST['verify_code'].strip() try: request.session.get("display") verify_code = request.POST['verify_code'].strip() except: verify_code = "no verify" password = utils.md5_by_hmac(password) # 数据校验 if verify_code == "": return render(request, "users/login.html", {"msg": "验证码不能为空"}) if username == "": return render(request, "users/login.html", {"msg": "用户名称不能为空"}) if password == "": return render(request, "users/login.html", {"msg": "用户密码不能为空"}) users = models.Passport.objects.filter(username=username) # print(verify_code.upper()) if verify_code == "no verify" or verify_code.upper() == request.session.get("code").upper(): if len(users) > 0: if users[0].password == password: # 登录成功,需要保持用户的信息,以表示用户已经登录 request.session["loginUser"] = users[0] request.session["tryTime"] = 0 try: del request.session['display'] except: pass # 获得购物车对象 user = models.Passport.objects.get(pk=request.session.get("loginUser").id) shopcarts = s_models.ShopCart.objects.filter(user=user) # 购物车中有多少类商品 carts_count = len(shopcarts) request.session["carts_count"]=carts_count return redirect("/") else: try: # request.session.get("tryTime") request.session["tryTime"] += 1 except: request.session["tryTime"] = 0 if request.session["tryTime"] > 2: request.session["display"] = 1 nowTryTime = 3 - request.session["tryTime"] if nowTryTime > 0: return render(request, "users/login.html", {"msg": "密码错误!!{tryTime}次后将输入验证码".format(tryTime=str(nowTryTime))}) else: return render(request, "users/login.html", {"msg": "密码错误!!请输入验证码"}) else: return render(request, "users/login.html", {"msg": "该用户名不存在!!"}) else: return render(request, "users/login.html", {"msg": "验证码错误!!"})
def active(request, token): # print("active") print(token) try: users = models.Passport.objects.all() for i in users: if utils.md5_by_hmac(str(i.id)) == token: i.is_active = 1 i.save() return render(request, "users/login.html", {"msg": "用户激活成功!!,请登录"}) except Exception as e: return HttpResponse("激活失败!请联系服务人员!")
def psw_update(request): if request.method == "GET": return render(request, "users/psw_update.html", {}) else: old_pass = request.POST.get("old_pass").strip() new_pass = request.POST.get("new_pass").strip() re_pass = request.POST.get("re_pass").strip() old_pass = utils.md5_by_hmac(old_pass) try: user = models.Passport.objects.get(username=request.session.get("loginUser").username, password=old_pass) print(user) if len(new_pass) > 5 and new_pass == re_pass: new_pass = utils.md5_by_hmac(new_pass) user.password = new_pass print(user) user.save() return redirect("/users/login/") else: return render(request, "users/psw_update.html", {"msg": "密码不符合规定!"}) except: return render(request, "users/psw_update.html", {"msg": "旧密码不正确!"})
def register(request): """ TODO:手机注册 """ if request.method == "GET": return render(request, "users/register.html", {"msg": "请认真填写如下选项"}) elif request.method == "POST": username = request.POST["phone"].strip() password = request.POST["passwordA"] re_password = request.POST.get("passwordA2").strip() tel_code = request.POST['tel_code'].strip() # 数据校验 if username == "": return render(request, "users/register.html", {"msg": "用户名称不能为空"}) if len(password) < 3: return render(request, "users/register.html", {"msg": "用户密码长度不能小于3位"}) # if password.strip() != confirmpwd.strip(): # return render(request, "users/register.html", {"msg": "两次密码不一致"}) if tel_code.upper() != request.session.get("message_code").upper(): return render(request, "users/register.html", {"msg": "验证码错误"}) # 用户名称不能重复 if re_password == password: try: models.Passport.objects.get(username=username) return render(request, "users/register.html", {"msg": "该用户名称已经存在,请重新输入"}) except: s=transaction.savepoint() try: password = utils.md5_by_hmac(password) # print(password) # password = hmac.new(password.encode("utf-8"), "password".encode("uft-8"), "MD5") user = models.Passport(username=username, password=password, tel=username,is_active=True) user.save() transaction.savepoint_commit(s) except: transaction.rollback(s) return render(request, "users/register.html", {"msg": "注册失败,请重新注册"}) else: return render(request, "users/register.html", {"msg": "注册失败,两次输入密码不一致"}) return render(request, "users/login.html", {"msg": "用户注册成功!!,请登录"})
def send_email(uid, username, email): # print(token) token = utils.md5_by_hmac(str(uid)) # 发邮件 subject = '主题信息' message = '' sender = settings.EMAIL_FROM receiver = [email] print(1) html_message = '<h1>%s,信息语句</h1>请点击下面的链接激活您的帐号<br/><a href="http://127.0.0.1:8000/users/active/%s">请点击我</a>' % ( username, token) send_mail(subject, message, sender, receiver, html_message=html_message) print('发送成功。。。') # subject = '主题信息' # message = '' # sender = settings.EMAIL_FROM # receiver = ["*****@*****.**"] # print(1) # send_mail(subject, message, sender, receiver, html_message="<h1>信息语句</h1>") # print(2)