Example #1
0
def reset_password(request):
    if request.method == "POST":
        email = request.POST.get('email')
        password = request.POST.get("password")
        yzm = request.POST.get("yzm")
        print(request.POST)
        if yzm.upper() == request.session.get("password_reset").upper():
            user_cur = User.objects.get(email=email)

            user_cur.set_password(password)
            user_cur.save()
            flag = True
            msg = "密码重置成功,请登录"
            _type = "重置密码通知"
            email_content = \
                """
                您刚才<span style="color:red;">重置密码</span><br>
                密码重置成功<br>
                若不是您本人操作,请尽快联系本站点
                """
            activate_mail(user_cur.email, _type, email_content)
        else:
            flag = False
            msg = "验证码错误,请重新获取验证码"
        yzm = request.session.pop("password_reset", None)
        return JsonResponse({"flag": flag, "msg": msg})
Example #2
0
def update_password(request):
    """修改密码"""
    if request.method == "POST":
        print(request.POST)
        new_password = request.POST.get("new_password")
        user_cur = User.objects.get(user_id=request.user.user_id)
        user_cur.set_password(new_password)
        user_cur.save()
        _type = "修改密码通知"
        email_content = \
            """
            您<span style="color:red;">修改密码</span><br>
            修改密码成功
            若不是您本人操作,请尽快联系本站点
            """
        activate_mail(user_cur.email, _type, email_content)
        logout(request)
        return redirect("accounts:login")
Example #3
0
def user_login(request):
    """用户登录"""
    if request.method == 'POST':
        context = {"flag": False, "msg": "用户名或密码错误"}
        data = request.POST
        username = data.get('username')
        password = data.get('password')
        if "@" in username:
            user = User.objects.filter(email=username).first()
        else:
            user = User.objects.filter(username=username).first()
        if user:
            if user.is_active:
                user = authenticate(username=user.username, password=password)
                if user:
                    login(request, user)
                    context['flag'] = True
                    context['msg'] = '登录成功'
            else:
                token = token_confirm.generate_validate_token(
                    userdata=user.username)
                token_url = '/'.join([DOMAIN, 'accounts/activate', token])
                email_content = \
                    """
                    确认无误后,请在<span style='color:red;'>一小时之内</span>点击<a style='color:blue' href='{token_url}'>链接</a>激活邮箱<br>
                    若无法跳转,请直接在浏览器打开此链接:<br><b style='color:blue'>{token_url}</b>
                    """.format(
                        token_url=token_url)
                _type = "注册"
                activate_mail(user.email, _type, email_content)
                context["msg"] = "您的邮箱未激活,请前往{}邮箱查看激活邮件".format(user.email)

        return JsonResponse(context)
    else:
        if request.user.is_authenticated:
            return redirect('blog:blog_list')
        else:
            return render(request, "accounts/login.html")
Example #4
0
def bindemail(request):
    """
    绑定邮箱
    :param request:
    :return:
    """
    try:
        oauth = OAuthUser.objects.get(id=request.session.get("oauthid"))
    except ObjectDoesNotExist as e:
        # print((e)
        return render(request, '404.html', status=404)

    if request.method == "GET":
        return render(request, 'oauth/bindemail.html')
    else:
        data = request.POST
        email = data.get('email')
        username = data.get('username')
        if User.objects.filter(username=username).exists():
            flag = False
            msg = '用户名已存在'
            return JsonResponse({'flag': flag, 'msg': msg})
        if User.objects.filter(email=email).exists():
            flag = False
            msg = '邮箱已注册'
            return JsonResponse({'flag': flag, 'msg': msg})
        user = oauth.user
        user.username = username
        user.email = email
        user.save()
        token = token_confirm.generate_validate_token(userdata=email)
        token_url = '/'.join([DOMAIN, 'accounts/activate', token])
        email_content = \
            """
            您刚才通过{oauth_type}快捷注册了本站<br>
            您的默认密码为<span style='color:red;'>{user_password}<b>请务必更改密码!!!</b></span><br />
            确认无误后,请在<span style='color:red;'>一小时之内</span>点击<a style='color:blue' href='{token_url}'>链接</a>激活邮箱<br>
            若无法跳转,请直接在浏览器打开此链接:<br><b style='color:blue'>{token_url}</b>
            """.format(
                token_url=token_url, user_password=oauth.type+oauth.openid, oauth_type=oauth.type, userid=username)
        _type = "注册"

        context = activate_mail(email, _type, email_content)
        notify.send(user,
                    recipient=User.objects.filter(is_superuser=True),
                    target=user,
                    verb='{}用户通过{}操作{},验证邮件发送结果{}'.format(
                        email, oauth.type, _type, context['flag']))
        return JsonResponse(context)
Example #5
0
def email_result(request):
    if request.method == "POST":
        email_type = request.POST.get("email_type")
        if email_type == "reg":
            email = request.POST.get("email")
            token = token_confirm.generate_validate_token(userdata=email)
            token_url = '/'.join([DOMAIN, 'accounts/activate', token])
            email_content = \
                """
                确认无误后,请在<span style='color:red;'>一小时之内</span>点击<a style='color:blue' href='{token_url}'>链接</a>激活邮箱<br>
                若无法跳转,请直接在浏览器打开此链接:<br><b style='color:blue'>{token_url}</b>
                """.format(
                    token_url=token_url)
            _type = "注册"

        elif email_type == "email_change":
            _type = "更改邮箱"
            email = request.user.email
            yzm = active_yzm()
            request.session["email_yzm"] = yzm
            email_content = \
                """
                您正在<span style="color:red;">更改邮箱地址</span><br>
                您本次操作的验证码:<b>{yzm}</b><br>
                若不是您本人操作,请尽快更改密码
                """.format(yzm=yzm)
        elif email_type == "password_reset":
            _type = "重置密码"
            email = request.POST.get('email')
            yzm = active_yzm()
            request.session["password_reset"] = yzm
            email_content = \
                """
                您正在尝试<span style="color:red;">重置密码</span><br>
                您本次操作的验证码:<b>{yzm}</b><br>
                若不是您本人操作,请尽快更改密码
                """.format(yzm=yzm)
        else:
            return JsonResponse({"error": "未知错误"})
        email_statu = activate_mail(email, _type, email_content)
        notify.send(User.objects.get(email=email),
                    recipient=User.objects.filter(is_superuser=True),
                    target=User.objects.get(email=email),
                    verb='{}用户通过账号操作{},验证邮件发送结果{}'.format(
                        email, _type, email_statu['flag']))
        return JsonResponse(email_statu)