Пример #1
0
def 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.set_password(password)
                a.email = email
                a.save()
                #发送邮箱验证码
                send_email_code(email,1)
                return HttpResponse('请尽快前往您的邮箱激活账户,否则无法登陆')
        else:
            return render(request,'users/register.html',{
                'user_register_form':user_register_form
            })
Пример #2
0
def user_register(request):
    if request.method == "GET":
        # 这里使用UserRegisterForm不是为了验证,而是为了使用验证码
        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_is_exists = UserProfile.objects.filter(
                Q(username=email) | Q(email=email)).exists(
                )  # 使用 exists比使用 count 或者直接判断 QuerySet 更加高效
            if user_is_exists:
                msg = "用户已经存在,请直接 <a style='color:green;font-weight:bold' href='/users/login'>登录</a> 吧!"
                msg = mark_safe(msg)
                return render(request, 'users/register.html', {'msg': msg})
            else:
                user = UserProfile()
                user.username = email
                user.set_password(password)
                user.email = email
                # user.image =
                user.save()
                send_email_code(email, 1)
                return HttpResponse('请尽快前往您的邮箱激活,否则无法登录!')
                # return redirect('index')
        else:
            return render(request, 'users/register.html',
                          {'user_register_form': user_register_form})
Пример #3
0
def async_send_email(email, verify_type):
    """异步发送邮件
    :param email: string
    :param verify_type: int
    :return: bool. if successfully send email return True else return False
    """
    send_success = send_email_tool.send_email_code(email=email,
                                                   verify_type=verify_type)
    return send_success
Пример #4
0
def user_forget_pwd(request):
    if request.method == 'GET':
        user_forget_pwd_form = UserForgetPwdForm()
        return render(request, 'users/forget-pwd.html',
                      {'user_forget_pwd_form': user_forget_pwd_form})
    else:
        user_forget_pwd_form = UserForgetPwdForm(request.POST)
        if user_forget_pwd_form.is_valid():
            email = user_forget_pwd_form.cleaned_data['email']
            user_list = UserProfile.objects.filter(email=email)
            if user_list.exists():
                send_email_code(email, 2)
                return HttpResponse('请尽快去您的邮箱重置密码!')
            else:
                return render(request, 'users/forget-pwd.html',
                              {'msg': '用户不存在!'})
        else:
            return render(request, 'users/forget-pwd.html',
                          {'user_forget_pwd_form': user_forget_pwd_form})
Пример #5
0
 def post(self, request):
     user_update_email_form = UserUpdateEmailForm(request.POST)
     if user_update_email_form.is_valid():
         email = user_update_email_form.cleaned_data['email']
         user_list = UserProfile.objects.filter(
             Q(email=email) | Q(username=email))
         if user_list.exists():
             return JsonResponse({'status': 'fail', 'msg': '邮箱已经被绑定'})
         else:
             # 我们在发送邮箱验证码之前,应该去邮箱验证码的表当中去查找,看之前有没有往当前这个邮箱发送过修改邮箱这个类型的验证码
             email_ver_list = EmailVerifyCode.objects.filter(email=email,
                                                             send_type=3)
             # 如果发送过验证码,那么我们就拿到最近发送的这一个
             if email_ver_list.exists():
                 email_first = email_ver_list.order_by('-add_time')[0]
                 # 判断当前时间和最近发送的验证码添加时间之差
                 if (datetime.datetime.now() -
                         email_first.add_time).seconds > 60:
                     # 如果我们重新发送了新的验证码,那么以前最近发的就被清了
                     send_email_code(email, 3)
                     email_first.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': '您的邮箱有问题'})
Пример #6
0
def user_change_email(request):
    user_change_email_form = UserChangeEmailForm(request.POST)
    #校验邮箱是否正确
    if user_change_email_form.is_valid():
        email = user_change_email_form.cleaned_data['email']
        #邮箱在数据库中是否已经存在
        email_list = UserProfile.objects.filter(Q(email=email)|Q(username=email))
        if email_list:
            return JsonResponse({'status': 'err', 'msg': '该邮箱已存在'})
        else:
            email_code_list = EmailVerifyCode.objects.filter(email=email,send_type=3)
            if email_code_list:
                email_verify = email_code_list.order_by('-add_time')[0]
                if (datetime.now() - email_verify.add_time).seconds > 60:
                    email_verify.delete()
                    send_email_code(email, 3)
                    return JsonResponse({'status': 'ok', 'msg': '验证码发送成功,请前往邮箱查看'})
                else:
                    return JsonResponse({'status': 'ok', 'msg': '验证码已发送,1分钟之后再试'})
            else:
                send_email_code(email,3)
                return JsonResponse({'status': 'ok','msg':'验证码发送成功,请前往邮箱查看'})
    else:
        return JsonResponse({'status':'err','msg':'邮箱输入有误'})
Пример #7
0
def ajax_send_email(request):
    if request.method == 'GET':
        email = str(request.GET.get("email"))
        # 此处的邮件类型直接转为int可能会抛异常:request.GET.get("verify_type")的值不合法时会抛异常
        verify_type_str = request.GET.get("verify_type")
        verify_type = int(verify_type_str) if verify_type_str else -1
        send_success = send_email_code(email=email, verify_type=verify_type)
        if send_success:
            return JsonResponse({
                "completed": "true",
                "msg": "邮件已发送至邮箱,请查收",
            })
        else:
            return JsonResponse({
                "completed": "false",
                "msg": "邮件发送失败,请重试!",
            })
    else:
        return redirect(reverse('forbidden_403'))