Beispiel #1
0
def user_register(request):
    """
    用户注册路由
    """
    m = request.method
    if m == 'get':
        return render_template(request, 'user_register.html')
    elif m == 'post':
        d = request.form()
        account = d['account']
        password = d['password']
        username = d['username']
        repeat_password = d['repeat_password']
        if repeat_password == password:
            u, result = User.register(account, password, username)
            if u is not None:
                r = Response(request, request.setting)
                session = u.create_session()
                r.add_cookie('session', session, Path='/')
                return redirect(request, '/user', response=r)
            else:
                return error(request, 400, result)
        else:
            return error(request, 400, '两次密码不一致')
    else:
        return error(request, 405)
Beispiel #2
0
def user_logout(request):
    """
    用户登出路由
    """
    session = request.cookies.get('session', None)
    if session is not None:
        user = User.current_user(session)
        user.logout()
        return redirect(request, '/user')
    else:
        return error(request, 400, '用户未登陆')
Beispiel #3
0
def note_delete(request):
    """
    留言删除路由
    """
    note_id = request.query.get('id', None)
    if note_id is not None:
        Note.delete([
            note_id,
        ])
        return redirect(request, '/note')
    else:
        return error(request, 404)
Beispiel #4
0
def note_add(request):
    """
    留言增加路由
    """
    m = request.method
    if m == 'get':
        return render_template(request, 'note_add.html')
    elif m == 'post':
        d = request.form()
        session = request.cookies.get('session', None)
        user = User.current_user(session)
        d.update({
            'user': user.id,
        })
        note = Note.new(d)
        note.save()
        return redirect(request, '/note')
    else:
        return error(request, 405)
Beispiel #5
0
def user_login(request):
    """
    用户登录路由
    """
    m = request.method
    if m == 'get':
        return render_template(request, 'user_login.html')
    elif m == 'post':
        d = request.form()
        account = d['account']
        password = d['password']
        u = User.login(account, password)
        if u is not None:
            r = Response(request, request.setting)
            session = u.create_session()
            r.add_cookie('session', session, Path='/')
            return redirect(request, '/user', response=r)
        else:
            return error(request, 400, '账号密码错误')
    else:
        return error(request, 405)
Beispiel #6
0
def note_edit(request):
    """
    留言编辑路由
    """
    note_id = request.query.get('id', None)
    if note_id is not None:
        note = Note.find_by(id=note_id)
        if note is not None:
            m = request.method
            if m == 'get':
                d = {
                    'note': note,
                }
                return render_template(request, 'note_edit.html', d)
            elif m == 'post':
                d = request.form()
                note.title = d['title']
                note.content = d['content']
                note.save()
                return redirect(request, '/note')
            else:
                return error(request, 405)
    return error(request, 400)