예제 #1
0
def route_login(request):
    """
    登录的请求函数, 处理登录请求的响应
    headers 的格式如下
    # 'Set-Cookie': 'height=169; gua=1; pwd=2; Path=/',
    """
    headers = {
        'Content-Type': 'text/html',
    }
    log('login, cookies', request.cookies, current_user(request))
    username, result = '【游客】', ''
    if request.method == 'POST':
        form = request.form()
        u = User(form)
        username = '******'
        if u.validate_login():
            username = u.username
            user = User.find_by(username=u.username)
            # 可以通过设置一个令牌(随机字符串)进行使用
            session_id = random_str()
            log('session_id', session_id)
            session[session_id] = user.id
            log('LOGIN', session, user.id)
            headers['Set-Cookie'] = 'user={}'.format(session_id)
            result = '登录成功'
            body = template('login.html', username=username, result=result)
            return http_response(body, headers)
            # return redirect('/', headers)
        else:
            result = '用户名或密码错误'
    else:
        result = ''
    body = template('login.html', username=username, result=result)
    return http_response(body, headers)
예제 #2
0
def route_login(request):
    """
    登录页面的路由函数
    """
    headers = {
        'Content-Type': 'text/html',
        # 'Set-Cookie': 'height=169; gua=1; pwd=2; Path=/',
    }
    if request.method == 'POST':
        form = request.form()
        u = User.new(form)
        if u.validate_login():
            user = User.find_by(username=u.username)
            # 设置 session
            # 设置一个随机字符串来当令牌使用
            session_id = random_str()
            session[session_id] = user.id
            headers['Set-Cookie'] = f'user={session_id}'
            # 下面是把用户名存入 cookie 中
            # headers['Set-Cookie'] = 'user={}'.format(u.username)
            # log('headers response', headers)
            # 登录后重定向到 /
            return redirect('/', headers)
    # 显示登录页面
    body = template('login.html')
    return http_response(body, headers=headers)
예제 #3
0
def index(request):
    """
    主页的处理函数, 返回主页的响应
    """
    todo_list = Todo.all()
    body = template('simple_todo_index.html', todos=todo_list)
    return http_response(body)
예제 #4
0
def index(request):
    """
    _todo 首页的路由函数
    """
    todo_list = Todo.all()
    body = template('simple_todo_index.html', todos=todo_list)
    return http_response(body)
예제 #5
0
def edit(request):
    """
    _todo edit 的路由函数
    """
    todo_id = int(request.query.get('id'))
    t = Todo.find_by(id=todo_id)
    body = template('simple_todo_edit.html', todo=t)
    return http_response(body)
예제 #6
0
def edit(request):
    u = request.login_user
    todo_id = int(request.query.get('id', -1))
    log('to_do_id***********', str(todo_id))
    t = Todo.find_by(id=todo_id)
    if t.user_id != u.id:
        return redirect('/login')
    return http_response(render_template('todo_edit.html', todo=t))
예제 #7
0
def admin_users(request):
    u = current_user(request)
    log('admin-users user data: ', u)
    if u is not None and u.is_admin():
        users = User.all()
        body = templates('users.html', users=users)
        return http_response(body)
    return redirect('/login')
예제 #8
0
파일: todo.py 프로젝트: fairylin/python_web
def index(request):
    """
    主页处理函数,返回主页的响应
    :param request:
    :return:
    """
    body = template('todo_index.html')
    return http_response(body)
예제 #9
0
def index(request):
    uname = current_user(request)
    u = User.find_by(username=uname)
    if u is None:
        return redirect('/login')
    todo_list = Todo.find_all(user_id=u.id)
    body = templates('todo_index.html', todos=todo_list)
    return http_response(body)
예제 #10
0
def admin_index(request, u):
    # 认证石否为管理员
    if u.role != 1:
        log('admin not power', u.role)
        return redirect('/login')
    log('admin', u)
    users = User.all()
    return http_response(template_render('admin_index.html', users=users))
예제 #11
0
def route_admin_users(request):
    u = current_user(request)
    if u is not None and u.is_admin():
        us = User.all()
        body = templates('admin_user.html', users=us)
        return http_response(body)
    else:
        return redirect('/login')
예제 #12
0
def edit(request):
    weibo_id = int(request.query.get('id', -1))
    w = Weibo.find_by(id=weibo_id)
    u = current_user(request)
    if w.user_id != u.id:
        return error(request)
    body = template('weibo_edit.html', weibo=w)
    return http_response(body)
def index(request):
    """
    index controller
    """
    user = session.get_user(request)
    username = user.username if user else '游客'

    body = render_template('index.html', username=username)
    return http_response(body)
def profile(request):
    user = request.login_user
    context = dict(
        username=user.username,
        password=user.password,
        note=user.note,
    )
    body = render_template('profile.html', **context)
    return http_response(body)
예제 #15
0
def edit(request):
    """
    edit 页的处理函数, 返回 edit 页的响应
    /edit?id=1
    """
    todo_id = int(request.query.get('id', -1))
    t = Todo.find_by(id=todo_id)
    body = template('simple_todo_edit.html', todo=t)
    return http_response(body)
예제 #16
0
def edit(request):
    weibo_id = request.query.get('id', -1)
    weibo_id = int(weibo_id)
    w = Tweet.find(weibo_id)
    if w is None:
        return error(request)
    # 生成一个 edit 页面
    body = template('weibo_edit.html', weibo_id=w.id, weibo_content=w.content)
    return http_response(body)
예제 #17
0
def route_index(request):
    uid = current_user(request)
    u = User.model_find_by(id=uid)
    log("route index uid: ({}) session: ({})".format(uid, session))
    data = {"username": u.username}
    # log("template {}".format(template("index.html", data=data)))
    body = template("index.html", data=data)
    r = http_response(body, request.headers)
    return r
예제 #18
0
def index(request):
    """
    _todo 首页的路由函数
    """
    session_id = request.cookies.get('user', '')
    user_id = session.get(session_id)
    todo_list = Todo.find_all(user_id=user_id)
    # todo_list = Todo.all()
    body = template('simple_todo_index.html', todos=todo_list)
    return http_response(body)
예제 #19
0
def index(request):
    uid = current_user(request)
    u = User.find_by(id=uid)
    if u is None:
        username = "******"
    else:
        username = u.username
    body = template('index.html')
    body = body.replace('{{ username }}', username)
    return http_response(body)
예제 #20
0
def route_register(request):
    if request.method == 'POST':
        form = request.form()
        u = User(form)
        if u.validate_register():
            return redirect('/login')
        else:
            return redirect('/register')
    body = template('register.html')
    return http_response(body)
예제 #21
0
def index(request):
    user_id = request.query.get('user_id', -1)
    user_id = int(user_id)
    user = User.find(user_id)
    if user is None:
        return redirect('/login')
    # 找到 user 发布的所有 weibo
    weibos = Weibo.find_all(user_id=user_id)
    body = template('weibo_index.html', weibos=weibos, user=user)
    return http_response(body)
예제 #22
0
def edit(request):
    weibo_id = request.query.get('id', -1)
    if weibo_id is None:
        weibo_id = current_user(request)
    weibo_id = int(weibo_id)
    w = Weibo.find(weibo_id)
    if w is None:
        return error(request)
    # 生成一个 edit 页面
    body = template('weibo_edit.html', weibo=w)
    return http_response(body)
예제 #23
0
def route_todo_edit(request):
    todo_id = request.query.get("id", -1)
    if id != -1:
        if request.method == "POST":
            form = request.form()
            form["ut"] = Todo.set_time()
            Todo.model_edit(form, id=int(todo_id))
            return redirect(request.headers, "/todo")
        else:
            todo = Todo.model_find_by(id=int(todo_id))
            body = template("todo.edit.html", todo=todo)
            return http_response(body, request.headers)
def register(request):
    if request.method == 'POST':
        form = request.form
        u = User.new(form)
        log(u.__dict__)
        if u.validate_register():
            return redirect('/login')
        else:
            return redirect('/register')

    body = render_template('register.html')
    return http_response(body)
예제 #25
0
def route_admin_index(request):
    # uid = current_user(request)
    # u = User.model_find_by(id=int(uid))
    # if u.role != 1:
    #     return redirect(request.headers, "/")
    if request.method == "POST":
        return redirect(request.headers, "/register")
    else:
        users = User.model_all()
        # users = [user for user in users if user.get_deleted() != False]
        users = User.model_validate_all(users)
        body = template("users.management.html", users=users)
        return http_response(body, request.headers)
예제 #26
0
def route_admin_user_edit(request):
    uid = request.query.get("id", -1)
    if uid != -1:
        if request.method == "POST":
            form = request.form()
            if form.get("password", ""):
                User.model_edit(form, id=int(uid))
            return redirect(request.headers, "/user/list")
        else:
            user = User.model_find_by(id=int(uid))
            body = template("user.edit.html", user=user)
            return http_response(body, request.headers)
    return redirect(request.headers, "/user/list")
예제 #27
0
def route_todo_list(request):
    uid = current_user(request)
    if request.method == "POST":
        redirect(request.headers, "/todo/add")
    todo_dict = Todo.model_find_all(uid=int(uid))
    # todo_list = [todo for todo in todo_list if todo.get_deleted() != True]
    todo_list = Todo.model_validate_all(todo_dict)
    # todo test
    # for todo in todo_list:
    #     print(todo.get_deleted())
    body = template("todo.html", todo_list=todo_list)
    r = http_response(body, request.headers)
    return r
예제 #28
0
def route_login(request):
    headers = {}
    if request.method == 'POST':
        form = request.form()
        u = User(form)
        if u.validate_login():
            user = User.find_by(username=u.username)
            session_id = random_str()
            session[session_id] = user.id
            headers['Set-Cookie'] = 'user={}'.format(session_id)
            return redirect('/todo', headers)
    body = template('login.html')
    return http_response(body, headers=headers)
예제 #29
0
def route_register(request):
    if request.method == 'POST':
        form = request.form()
        u = User.new(form)
        if u.valid_register():
            u.save()
            result = '注册成功<br> <pre>{}</pre>'.format(User.all())
        else:
            result = '用户名或密码长度必须大于2'
        print(type(result))
    else:
        result = ''
    body = templates('register.html', result=result)
    return http_response(body)
예제 #30
0
def index(request):
    """
    主页的处理函数, 返回主页的响应
    """
    # 首先拿到所有的todo数据
    todo_list = Todo.all()
    # 用 template 函数打开 simple_todo_index.html 页面,把 todo_list 传进去,使用模版渲染网页
    """
    template函数接受一个路径和一系列参数
    读取模板并渲染返回
    """
    body = template('simple_todo_index.html', todos=todo_list)
    #
    return http_response(body)