Esempio n. 1
0
def user_register(request):
    if request.method == 'GET':
        #这里实例化forms类,目的不是为了验证,而是为了使用验证码
        user_register_form = UserRegisterForm()
        return render(request, 'users/register.html',
                      {'user_register_form': user_register_form})
    else:
        user_register_form = UserRegisterForm(request.POST)
        if user_register_form.is_valid():
            email = user_register_form.cleaned_data['email']
            password = user_register_form.cleaned_data['password']

            user_list = UserProfile.objects.filter(
                Q(username=email) | Q(email=email))
            if user_list:
                return render(request, 'users/register.html',
                              {'msg': '用户已经存在'})
            else:
                a = UserProfile()
                a.username = email
                a.set_password(password)
                a.email = email
                a.save()
                send_email_code(email, 1)
                return HttpResponse('请尽快前往您的邮箱激活,否则无法登陆')
                # return redirect(reverse('index'))
        else:
            return render(request, 'users/register.html',
                          {'user_register_form': user_register_form})
Esempio n. 2
0
def user_changeemail(request):
    '''
    当用户修改邮箱,点击获取验证码的时候,会通过这个函数进行处理,发送邮箱验证码
    :param request:http请求对象
    :return:返回json数据
    '''
    user_changeemail_form = UserChangeEmailForm(request.POST)
    if user_changeemail_form.is_valid():
        email = user_changeemail_form.cleaned_data['email']
        user_list = UserProfile.objects.filter(Q(email=email) | Q(username=email))
        if user_list:
            return JsonResponse({'status': 'fail', 'msg': '该邮箱已被人使用'})
        else:
            # 我们在发送邮箱验证码之前,应该去邮箱验证码的表当中去查找,看之前有没有往这个邮箱发送过修改邮箱这个类型的验证码
            email_ver_list = EmailVerifyCode.objects.filter(email=email, send_type=3)
            # 如果发送过验证码,那么我们就拿到最近发送的一个
            if email_ver_list:
                email_ver = email_ver_list.order_by('-add_time')[0]
            # 判断当前时间和最近发送的验证码添加时间之差
                if (datetime.now() - email_ver.add_time).seconds > 60:
                    # 如果我们重新发送了新的验证码,那么最近发的就被清除了
                    send_email_code(email, 3)
                    email_ver.delete()
                    return JsonResponse({'status': 'ok', 'msg': '请尽快去邮箱当中获取验证码'})
                else:
                    return JsonResponse({'status': 'fail', 'msg': '请不要重复发送验证码,1分钟后重试'})
            else:
                send_email_code(email, 3)
                return JsonResponse({'status': 'ok', 'msg': '请尽快去邮箱当中获取验证码'})
    else:
        return JsonResponse({'status': 'fail', 'msg': '邮箱格式有问题'})
Esempio n. 3
0
def user_forget(request):
    if request.method == 'GET':
        user_forget_form = UserForgetForm()
        return render(request, 'users/forgetpwd.html',
                      {'user_forget_form': user_forget_form})
    else:
        user_forget_form = UserForgetForm(request.POST)
        if user_forget_form.is_valid():
            email = user_forget_form.cleaned_data['email']
            user_list = UserProfile.objects.filter(Q(username=email) | Q(email=email))
            if not user_list:
                return render(request, "users/login.html", {'msg': '该用户不存在'})
            else:
                send_email_code(email, 2)  # 2表示修改密码邮件
                return redirect(reverse('users:user_login'))
        else:
            return render(request, 'users/forgetpwd.html',
                          {'user_forget_form': user_forget_form})
Esempio n. 4
0
def user_forget(request):
    if request.method == 'GET':
        user_forget_form = UserForgetForm()
        return render(request, 'users/forgetpwd.html',
                      {'user_forget_form': user_forget_form})
    else:
        user_forget_form = UserForgetForm(request.POST)
        if user_forget_form.is_valid():
            email = user_forget_form.cleaned_data['email']
            user_list = UserProfile.objects.filter(email=email)
            if user_list:
                send_email_code(email, 2)
                return HttpResponse('请尽快去您的邮箱去重置密码')
            else:
                return render(request, 'users/forgetpwd.html', {
                    'msg': '用户不存在',
                    'user_forget_form': user_forget_form
                })
        else:
            return render(request, 'users/forgetpwd.html',
                          {'user_forget_form': user_forget_form})
Esempio n. 5
0
def user_register(request):
    if request.method == 'GET':
        user_register_form = UserRegisterForm()
        return render(request, 'users/register.html',
                      {'user_register_form': user_register_form})
    else:
        user_register_form = UserRegisterForm(request.POST)
        if user_register_form.is_valid():
            email = user_register_form.cleaned_data['email']
            password = user_register_form.cleaned_data['password']
            user_list = UserProfile.objects.filter(Q(username=email) | Q(email=email))
            if user_list:
                return render(request, "users/register.html", {'msg': '该邮箱已经注册'})
            else:
                a = UserProfile()
                a.username = email
                a.email = email
                a.set_password(password)
                a.save()
                send_email_code(email, 1)  # 1表示注册邮件
                return redirect(reverse('users:user_login'))
        else:
            return render(request, 'users/register.html',
                          {'user_register_form': user_register_form})