Ejemplo n.º 1
0
def register(request):
    if request.method == 'GET':
        data = {
            'title': '注册',
        }

        return render(request, 'user/register.html', context=data)
    elif request.method == "POST":
        username = request.POST.get('username')
        email = request.POST.get('email')
        password = request.POST.get('password')
        icon = request.FILES.get('icon')

        password = hash_str(password)

        user = AXFUser()
        user.u_username = username
        user.u_password = password
        user.u_email = email
        user.u_icon = icon

        user.save()

        u_token = uuid.uuid4().hex

        cache.set(u_token, user.id, timeout=68 * 60 * 24)

        send_email_activate(username, email, u_token)

        return redirect(reverse('axf:login'))
Ejemplo n.º 2
0
def register(request):
    if request.method == 'GET':

        data = {
            'title': '注册',
        }

        error_message = request.session.get('error_message')

        if error_message:
            del request.session['error_message']
            data['error_message'] = error_message

        return render(request, 'user/register.html', context=data)

    elif request.method == 'POST':

        if str(request.POST['verify_code']).lower() != request.session.get(
                'verify_code'):
            request.session['error_message'] = '验证码不正确'

            return redirect(reverse('App:register'))

        user = Users()

        user.u_username = request.POST['username']

        user.u_email = request.POST['email']

        password = request.POST['password']
        user.u_password = hash_str(password)

        user.u_icon = request.FILES['icon']

        user.save()

        u_token = uuid.uuid4().hex

        cache.set(u_token, user.id, timeout=60 * 60 * 3)

        send_email_activate(username=user.u_username,
                            email=user.u_email,
                            u_token=u_token)

        return redirect(reverse('App:login'))
Ejemplo n.º 3
0
def login(request):
    if request.method == 'GET':

        error_message = request.session.get('error_message')

        data = {
            'title': '登陆',
        }

        if error_message:
            del request.session['error_message']

            data['error_message'] = error_message

        return render(request, 'user/login.html', context=data)

    elif request.method == 'POST':

        username = request.POST['username']
        password = request.POST['password']
        password = hash_str(password)

        try:
            match_user = Users.objects.get(
                Q(u_username=username) | Q(u_email=username))

            if match_user.u_password == password:

                if not match_user.is_active:
                    request.session['error_message'] = '用户未激活,请前往邮箱激活'
                    return redirect(reverse('App:login'))

                request.session['user_id'] = match_user.id

                return redirect(reverse('App:mine'))

            else:
                request.session['error_message'] = '密码错误或账户不存在'
                return redirect(reverse('App:login'))

        except Exception as e:
            request.session['error_message'] = '密码错误或账户不存在'
            return redirect(reverse('App:login'))
Ejemplo n.º 4
0
def reset_password(request):

    if request.method == "GET":

        p_token = request.GET.get('p_token')

        email = cache.get(p_token)

        if email:
            data = {
                'title': '重设密码',
            }
            cache.set('reset_user_email', email, 60 * 5)

            return render(request, 'user/reset_password.html', data)

        request.session['error_message'] = '链接过期,请重新申请!'
        return redirect(reverse('App:forget_password'))

    elif request.method == "POST":

        try:

            email = cache.get('reset_user_email')

            reset_user = Users.objects.get(u_email=email)

            reset_user.u_password = hash_str(request.POST.get('new_password'))

            print(reset_user.u_password)

            reset_user.save()

            request.session['error_message'] = '密码重设成功!'

            return redirect(reverse('App:login'))

        except:

            request.session['error_message'] = '链接过期,请重新申请!'

            return redirect(reverse('App:forget_password'))
Ejemplo n.º 5
0
def login(request):
    if request.method == 'GET':
        error_message = request.session.get('error_message')

        data = {
            'title': '登录',
        }

        # 存在则传递过去,并删除
        if error_message:
            del request.session['error_message']
            data['error_message'] = error_message

        return render(request, 'user/login.html', context=data)
    elif request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')

        users = AXFUser.objects.filter(u_username=username)
        if users.exists():
            user = users.first()
            if hash_str(password) == user.u_password:
                # 判断激活
                if user.is_active:
                    request.session['user_id'] = user.id
                    return redirect(reverse('axf:mine'))
                else:
                    print('未激活')
                    request.session['error_message'] = '未激活'
                    return redirect(reverse('axf:login'))
            else:
                print('密码错误')
                request.session['error_message'] = '密码错误'
                return redirect(reverse('axf:login'))
        else:
            print('用户不存在')
            request.session['error_message'] = '用户不存在'
            return redirect(reverse('axf:login'))