def get(self, request, token): """ 进行用户电邮激活 """ # 进行解密 serializer = Serializer(settings.SECRET_KEY, 3600) try: info = serializer.loads(token) # 获取用户id user_id = info['comfirm'] # 激活用户 user = User.objects.get(id=user_id) user.email_activate = 1 # 获取用户资料 名字及电邮 username = user.username email = user.email user.save() # 向管理员发送用户激活电邮 send_register_email.delay(email, username, user_id) # 返回应答 return HttpResponse( '<h1>用户激活成功,还需要管理员激活。</h1><a href="http://120.197.59.97:57893/user/login">点击登录</a>', content_type='text/html;charset=utf-8') except SignatureExpired as e: return HttpResponse('激活邮件已经过期')
def post(self, request): # 进行注册处理 # 接收数据 username = request.POST.get('user_name') password = request.POST.get('pwd') email = request.POST.get('email') # 数据校验 if not all([username, password, email]): return render(request, 'register.html', {'errmsg': '数据不完整'}) # 校验用户名是否重复 try: user = User.objects.get(username=username) except User.DoesNotExist: # 说明用户名不存在 user = None if user: # 满足条件说明用户名已存在 return render(request, 'register.html', {'errmsg': '用户名已存在'}) # 业务处理 user = User.objects.create_user(username, email, password) user.is_active = 0 user.save() # 发送激活邮件 serializer = Serializer(settings.SECRET_KEY, 3600) info = {'confirm': user.id} token = serializer.dumps(info).decode() send_register_email.delay(email, username, token) # 返回应答,跳转到首页 return redirect(reverse('goods:index'))
def post(self, request): # 进行注册处理接收数据 username = request.POST.get('user_name') password = request.POST.get('pwd') email = request.POST.get('email') allow = request.POST.get('allow') # 进行数据校验 if not all([username, password, email]): # 数据不完整 return render(request, 'register.html', {'errmsg': '数据不完整'}) # 校验邮箱 if not re.match(r'^[a-z0-9][\w.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return render(request, 'register.html', {'errmsg': '邮箱格式不正确'}) if allow != 'on': return render(request, 'register.html', {'errmsg': '请同意协议'}) # 校验用户名是否重复 try: user = User.objects.get(username=username) except User.DoesNotExist: # 用户名不存在 user = None if user: # 用户名已存在 return render(request, 'register.html', {'errmsg': '用户名已存在'}) # 进行业务处理:进行用户注册 user = User.objects.create_user(username, email, password) user.is_active = 0 user.save() # 发送激活邮件,包含激活链接:127.0.0.1:8000/user/active/3 # 激活链接中需包含用户的身份信息,并且要把身份信息进行加密 # 加密用户的身份信息,生成激活的token serializer = Serializer(settings.SECRET_KEY, 3600) info = {'confirm': user.id} token = serializer.dumps(info) token = token.decode() # 发邮件 # subject = '天天生鲜欢迎信息' # message = '' # sender = settings.EMAIL_FROM # receiver = [email] # html_message = '<h1>%s,欢迎您成为天天生鲜注册会员</h1>请点击下面链接激活您的账户<br/><a href="http://127.0.0.1:8000/user/active/%s">http://127.0.0.1:8000/user/active/%s</a>' % ( # username, token, token) # send_mail(subject, message, sender, receiver, html_message=html_message) send_register_email.delay(email, username, token) # 返回应答,跳转到首页 return HttpResponseRedirect(reverse('goods:index'))
def post(self, request): # 1. 接收数据 user_name = request.POST.get('user_name') pwd = request.POST.get('pwd') email = request.POST.get('email') allow = request.POST.get('allow') # 2. 数据校验 # all(iteranle)方法,可当迭代对象对象的所有元素不为0、''、False或者空对象,则返回True,否则返回False if not all([user_name, pwd, email]): # 2.1 判断数据是否完整 return render(request, 'register.html', {'errormsg': "您输入的内容不完整!"}) if not re.match(r'^[a-z0-9][\w.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): # 2.2 判断邮箱是否正确 return render(request, 'register.html', {'errormsg': '邮箱格式不正确!'}) if allow != 'on': # 2.3 判断用户是否同意用户协议 return render(request, 'register.html', {"errormsg": "请同意用户协议!"}) try: # 2.4 判断账户是否已经被注册, user = User.objects.get(username=user_name) except User.DoesNotExist as e: # 账户未被注册 user = None if user: return render(request, 'register.html', {'errormsg': '该账户已经被注册!'}) # 3. 进行注册流程 user = User.objects.create_user(username=user_name, password=pwd, email=email) user.is_active = 0 user.save() # 4. 发送激活邮件 serializer = Serializer(settings.SECRET_KEY, 3600) info = {'confirm': user.id} token = serializer.dumps(info) token = token.decode() send_register_email.delay(user_name, email, token) return redirect(reverse('goods:index'))
def post(self, request): """ 进行注册处理 """ # 接收数据 username = request.POST.get('user_name') password = request.POST.get('pwd') email = request.POST.get('email') allow = request.POST.get('allow') # 进行数据校验 if not all([username, password, email]): return render(request, 'register.html', {'errmsg':'数据不完整'}) # 校验邮箱 if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return render(request, 'register.html', {'errmsg':'邮箱格式不正确'}) if allow != 'on': return render(request, 'register.html', {'errmsg':'请同意协议'}) # 检验用户名是否重复 try: user = User.objects.get(username=username) except User.DoesNotExist: # 用户名不存在 user = None if user: return render(request, 'register.html', {'errmsg':'用户名已经存在'}) # 进行业务处理 user = User.objects.create_user(username, email, password) user.is_active = 0 user.save() """ 发送激活邮件 """ # 加密用户身份信息 生成激活cookie serializer = Serializer(settings.SECRET_KEY, 3600) info = {'comfirm':user.id} token = serializer.dumps(info) token = token.decode() # 发送邮件 send_register_email.delay(email, username, token) # 返回应答 return redirect(reverse('user:login'))
def post(self, request): # 接收数据 username = request.POST.get('user_name') password = request.POST.get('pwd') cpwd = request.POST.get('cpwd') email = request.POST.get('email') allow = request.POST.get('allow') #检验数据 if not all([username, password, cpwd, email]): context = {'errmessage': '数据输入不完整'} return render(request, 'register.html', context=context) try: user = User.objects.get(username=username) except User.DoesNotExist: user = None if user: context = {'errmessage': '用户名已经存在'} return render(request, 'register.html', context=context) if password != cpwd: return render(request, 'register.html', context={'errmessage': '两次输入的密码不一致'}) if not re.match(r'^[a-z0-9][\w.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return render(request, 'register.html', context={'errmessage': '邮箱输入的格式不正确'}) if allow != 'on': return render(request, 'register.html', context={'errmessage': '请阅读协议是否同意'}) user = User.objects.create_user(username=username, password=password, email=email) user.is_active = 0 user.save() serializer = Serializer(settings.SECRET_KEY, 3600) info = {'confirm': user.id} token = serializer.dumps(info) token = token.decode() # html_message = '<h1>{0}, 欢迎您成为天天生鲜注册会员</h1>请点击下面链接激活您的账户<br/><a href="http://127.0.0.1:8000/user/active/{1}">http://127.0.0.1:8000/user/active/{2}</a>'.format( # username, token, token) send_register_email.delay(email, username, token) return HttpResponse('邮件已经发送到你的邮箱')
def post(self, request): username = request.POST['user_name'] pwd = request.POST['pwd'] email = request.POST['email'] # create_user()方法会对密码进行hash # user = User.objects.create_user(username, email, pwd) # 账户的激活状态为0表示未激活 user = User.objects.create_user(username, email, pwd) user.is_active = 0 user.save() # 发送激活链接,生成加密的token active_id = {'confirm': username} token = Serializer(settings.SECRET_KEY, 3600).dumps(active_id) # print(type(token)) token = str(token, encoding='utf-8') # 将任务发送到Redis消息队列中,异步执行 send_register_email.delay(username, token, email) return redirect(reverse('product:home'))
def post(self, request): username = request.POST.get('user_name') password = request.POST.get('pwd') email = request.POST.get('email') allow = request.POST.get('allow') # 用户输入的内容校验是否为空 if not all([username, password, email]): return render(request, 'register.html', {'errmsg': '数据不完整'}) # 校验邮箱 if not re.match(r'^[a-z0-9][\w.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return render(request, 'register.html', {'errmsg': '邮箱格式不正确'}) # 校验是否同意协议 if allow != 'on': return render(request, 'register.html', {'errmsg': '请先同意协议'}) # 校验用户是否存在 if User.objects.filter(Q(username=username) | Q(email=username)): return render(request, 'register.html', {'errmsg': '用户已存在或者邮箱已经注册'}) user = User.objects.create_user(username, email, password) user.is_active = False user.save() # 对用户身份信息进行加密 s = serializer(settings.SECRET_KEY, 300) info = {'user_id': user.id} token = s.dumps(info) token = token.decode('utf-8') # 发送激活用户邮件 send_register_email.delay(username, email, token) # celery 异步 email_name = re.split('@|\.', email)[1] return render(request, 'register_success.html', {'name': email_name})