Exemplo n.º 1
0
    def post(self, request):
        #获取账号密码
        username = request.POST.get('user')
        email = request.POST.get('email')
        password = request.POST.get('password')

        #检查是否有空文本
        if not all([username, email, password]):
            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 not re.match(r'^[0-9a-zA-Z_]{6,12}$', username):

            return render(request, 'register.html', {'errmsg': '请输入数字或字母的账号'})
        elif (len(username) < 8) or (len(username) > 12):
            return render(request, 'register.html',
                          {'errmsg': '请输入8-12位的账号id'})

        #判断数据库是否存在此账号
        try:
            user = User.objects.get(username)
        except Exception as e:
            user = None
        #如果不为None返回已注册
        if user:
            return render(request, 'register.html', {'errmsg': '用户已经注册'})
        # 创建一个账号
        user = User.objects.create_user(username, email, password)
        # 并且用户激活状态为0
        user.is_active = 0
        # 保存数据
        user.save()

        # 发送激活邮件,包含激活链接:http://127.0.0.1:8....
        # 激活信息中需要包含用户的身份信息,并且要把身份信息加密
        # 加密用户身份信息,生成激活token

        # 设置加密的key,还有密匙的过期时间,使用Serializer方式加密
        serializer = Serializer(SECRET_KEY, 3600)
        # 创建一个字典,登入时候通过字典key获取用户id
        info = {'confirm': user.id}
        # 使用刚才设置的serializer对字典进行加密
        token = serializer.dumps(info)
        # 更改token的编码方式
        token = token.decode()

        #调用celery方式异步发送邮件验证

        send_register_active_email(email, username, token)

        return redirect(reverse('user:login'))
Exemplo n.º 2
0
    def post(self, request):
        username = request.POST.get('user_name', None)
        pwd = request.POST.get('pwd', None)
        cpwd = request.POST.get('cpwd', None)
        email = request.POST.get('email', None)
        allow = request.POST.get('allow', None)

        if not all([username, pwd, 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,
                                        pwd)  # database using password
        user.is_active = 0
        user.save()

        serializer = Serializer(settings.SECRET_KEY, 3600)
        info = {'confirm': user.id}
        token = serializer.dumps(info)
        token = token.decode()
        print('token:', token)

        def sendmail(to_email, username, token):
            subject = 'dailyfresh'
            message = 'regist message'
            sender = settings.EMAIL_FROM
            receiver = [to_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)
            # time.sleep(1)

        # sendmail(email, username, token)
        send_register_active_email(email, username, token)

        return redirect(reverse('user:login'))
Exemplo n.º 3
0
def resend_active_mail(request, uid):
    """ 重新发送激活邮件"""
    # 1,购建一个发送邮件的内容
    # a,构建加密的数据
    mima = make_mima_by_uid(uid)  # str类型的密码
    url = "http://127.0.0.1:8000/user/active/{}".format(mima)
    print('生成的密码是:')
    print(url)
    # 2,发送邮件
    send_register_active_email("*****@*****.**", uid, url)

    return HttpResponse("resend ok")
Exemplo n.º 4
0
    def post(self, request):
        '''进行注册处理'''
        # 接收数据
        username = request.POST.get('user_name')
        password = request.POST.get('pwd')
        password2 = request.POST.get('cpwd')
        email = request.POST.get('email')
        allow = request.POST.get('allow')

        # 进行数据校验
        if not all([username, password, email, allow]):
            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 password != password2:
            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()

        # 发送激活邮件,包含邮件链接:http://127.0.0.1:8000/user/active/3
        # 激活链接中需要包含的用户身份信息,并进行加密

        serializer = Serializer(settings.SECRET_KEY, 3600)

        info = {'confirm': user.id}
        token = serializer.dumps(info)
        token = token.decode()

        # 发邮件
        send_register_active_email(email, username, token)
        # 返回应答
        return redirect(reverse('goods:index'))
Exemplo n.º 5
0
    def post(self,request):
        #接收数据
        username = request.POST.get('user_name');
        passwrod = request.POST.get('pwd');
        cpasswrod = request.POST.get('cpwd');
        email = request.POST.get('email');
        allow = request.POST.get('allow');

        #进行数据校验
        if not all([username,passwrod,email]):
            #数据不完整
            return render(request,'register.html',{'errmsg':"数据不完整"})
        # obj = RegisterVerify(request.POST)
        # res = obj.is_valid()
        # if res:
        #     return render(request,'index.html',{'username':obj.cleaned_data['user']})
        # else:
        #     return render(request,'register.html',{'obj':obj})
        #校验邮箱
        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,passwrod)
        user.is_active=0
        user.save()
        #发送激活邮件,包含激活链接:http://127.0.0.1:8000/user/active/...
        #激活链接中需包含用户的身份信息,并且要把身份信息进行加密

        #加密用户的身份信息,生成激活token
        serializer = Serializer(settings.SECRET_KEY,3600)
        info = {'confirm':user.id}
        token = serializer.dumps(info)
        token = token.decode()  #bytes数据转成utf-8字符串

        #发邮件
        send_register_active_email(email,username,token)
        
        #返回应答,跳转到首页
        return redirect(reverse('goods:index'))
Exemplo n.º 6
0
    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()

        # 发送激活邮件,包含激活链接:http://127.0.0.1:8000/user/active/1
        # 激活链接中需要包含用户的身份信息,并且要把身份信息进行加密

        # 加密用户的身份信息,生成激活token
        serializer = Serializer(settings.SECRET_KEY, 3600)
        info = {'confirm': user.id}
        token = serializer.dumps(info)  # bytes
        token = token.decode()

        # 使用celery,帮助发送邮件
        send_register_active_email(email, username, token)

        # 返回应答,跳转到首页
        return redirect(reverse('goods:index'))
Exemplo n.º 7
0
def register(request):
    if request.session.get('is_login', None):
        return redirect('/index/')

    if request.method == 'POST':
        register_form = forms.RegisterForm(request.POST)
        message = "请检查填写的内容!"
        if register_form.is_valid():
            username = register_form.cleaned_data.get('username')
            password1 = register_form.cleaned_data.get('password1')
            password2 = register_form.cleaned_data.get('password2')
            email = register_form.cleaned_data.get('email')
            sex = register_form.cleaned_data.get('sex')

            if password1 != password2:
                message = '两次输入的密码不同!'
                return render(request, 'register.html', locals())
            else:
                same_name_user = User.objects.filter(name=username)
                if same_name_user:
                    message = '用户名已经存在'
                    return render(request, 'register.html', locals())
                same_email_user = User.objects.filter(email=email)
                if same_email_user:
                    message = '该邮箱已经被注册了!'
                    return render(request, 'register.html', locals())

                new_user = User()
                new_user.name = username
                new_user.password = hash_code(password1)
                new_user.email = email
                new_user.sex = sex
                new_user.save()

                code = make_confirm_string(new_user)  # 确认
                print("确认码为〉〉", code)

                # send_email(email, code)
                # 使用celery进行异步发送注册确认确认
                from celery_tasks.tasks import send_register_active_email  # 导入celery发邮件的方法
                send_register_active_email(email, code)
                message = '请前往邮箱进行确认!'
                return render(request, 'confirm.html', locals())
        else:
            return render(request, 'register.html', locals())
    register_form = forms.RegisterForm()
    return render(request, 'register.html', locals())
Exemplo n.º 8
0
    def post(self, request):
        # 获取用户输入的信息
        username = request.POST.get('user_name')
        password = request.POST.get('pwd')
        allow = request.POST.get('allow')
        email = request.POST.get('email')

        # 数据校验,完整性校验
        if not all([username, password, email]):
            return render(request, 'register.html', {'errmsg': '输入的数据不完整'})

        # 判断邮箱是否复合规则
        if not re.match('^[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()

        # 发送邮件前需要对发送邮件中的链接加密
        serializer = Serializer(settings.SECRET_KEY, 3600)
        info = {'confirm': user.id}
        token = serializer.dumps(info)
        token = token.decode()

        send_register_active_email(email, username, token)

        return redirect(reverse('goods:index'))
Exemplo n.º 9
0
 def post(self, request):
     # 接受数据
     username = request.POST.get('user_name')
     pwd = request.POST.get('pwd')
     cpwd = request.POST.get('cpwd')
     email = request.POST.get('email')
     allow = request.POST.get('allow')
     # 进行数据校验
     if not all([username, pwd, 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, pwd)
     user.is_active = 0
     user.save()
     # 发送激活邮件,包含激活链接:/user/active/3
     serializer = Serializer(settings.SECRET_KEY, 3600)
     info = {'confirm': user.id}
     res = serializer.dumps( info )
     res=res.decode('utf-8')
     #发邮件
     tasks.send_register_active_email(email, username, res)
     # 激活链接中需要包含用户的身份信息
     return redirect(reverse('goods:index'))
Exemplo n.º 10
0
 def thd(self, username, token, email):
     print(456)
     send_register_active_email(username, token, email)
Exemplo n.º 11
0
    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()

        # 这部分使用celery来发送邮件
        # 发送激活链接:http://127.0.0.1:8000/user/active/id
        # 激活链接中需要包含用户的身份信息,并且要把身份形象加密

        # # 加密用户的身份信息,生成激活TOKEN
        serializer = Serializer(settings.SECRET_KEY, 3600)
        info = {'confirm': user.id}
        token = serializer.dumps(info)
        #
        # # 发邮件
        # subject = '天天生鲜欢迎信息'
        # # message = '邮件正文'
        # print(token)
        # print(type(token))
        token = str(token, 'utf-8')
        # print(token)
        # print(type(token))
        # message = '<h1> {} 欢迎您成为天天生鲜注册会员</h1> 请点击下面链接激活您的账户<br/>' \
        #           '<a href="http://127.0.0.1:8000/user/active/{}">' \
        #           'http://127.0.0.1:8000/user/active/{}' \
        #           '</a>'.format(username, token, token)
        # sender = settings.EMAIL_PROM  # 指定发件人
        # receiver = [email] # 收件人列表
        # html_message = message
        # # send_mail(subject, message, sender, receiver) #message转义不了
        # message = ''
        # send_mail(subject, message, sender, receiver, html_message=html_message)

        # celery发邮件
        # send_register_active_email.delery(email, username, token) # 这个不行,没有delery这个方法
        print('------', email, username, token)
        send_register_active_email(email, username, token)
        return HttpResponse('hello')