Exemple #1
0
def edit(request):
    """
    todo edit 的路由函数
    """
    headers = {
        'Content-Type': 'text/html',
    }
    uname = current_user(request)
    u = User.find_by(username=uname)
    if u is None:
        return redirect('/login')
    # 得到当前编辑的 todo 的 id
    todo_id = int(request.query.get('id', -1))
    t = Todo.find_by(id=todo_id)
    if t.user_id != u.id:
        return redirect('/login')
    # if todo_id < 1:
    #     return error(404)
    # 替换模板文件中的标记字符串
    body = template('todo_edit.html')
    body = body.replace('{{todo_id}}', str(t.id))
    body = body.replace('{{todo_title}}', str(t.title))
    # 下面 3 行可以改写为一条函数, 还把 headers 也放进函数中
    header = response_with_headers(headers)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Exemple #2
0
def index(request):  #  todo 首页的路由函数
    headers = {
        'Content-Type': 'text/html',
    }
    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
    )  #  查找Todo数据库里 todo类的user_id属性和当前用户相等的Todo实例  即属于此用户的Todo数据 # 返回的是一个list包含符合条件的所有对象实例
    # 下面这行生成一个 html 字符串
    # todo_html = ''.join(['<h3>{} : {} </h3>'.format(t.id, t.title)
    #                      for t in todo_list])
    # 上面一行列表推倒的代码相当于下面几行
    todos = []
    for t in todo_list:
        edit_link = '<a href="/todo/edit?id={}">编辑</a>'.format(t.id)
        delete_link = '<a href="/todo/delete?id={}">删除</a>'.format(t.id)
        s = '<h3>{} : {} {} {}</h3>'.format(t.id, t.title, edit_link,
                                            delete_link)
        todos.append(s)
    todo_html = ''.join(todos)
    # 替换模板文件中的标记字符串
    body = template('todo_index.html')
    body = body.replace('{{todos}}', todo_html)
    # 下面 3 行可以改写为一条函数, 还把 headers 也放进函数中
    header = response_with_headers(headers)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Exemple #3
0
def todo_edit(request):
    """
    编辑页面显示
    """
    headers = {
        'Content-Type': 'text/html',
    }
    username = current_user(request)
    u = User.find_by(username=username)
    # 得到当前编辑的 todo 的 id
    # 此时页面的 url 含有 query ?id=1, request.query 解析为了一个字典
    todo_id = request.query.get('id', -1)
    if todo_id == -1:
        # 没找到, 反正错误页面
        return error(request)
    t = Todo.find_by(id=int(todo_id))
    if t.user_id != u.id:
        # 如果 todo 的 user_id 不是 对应的 user 的 id, 无法修改该 todo
        return redirect('/login')
    body = template('todo_edit.html')
    body = body.replace('{{todo_id}}', str(t.id))
    body = body.replace('{{todo_title}}', str(t.title))

    header = response_with_headers(headers)
    response = header + '\r\n' + body
    return response.encode('utf-8')
Exemple #4
0
def edit(request):
    uname = get_cookie(request)
    t = User.find_by(username=uname)
    if t is None:
        return director('/login')

    headers = {
        'Content-Type:': 'text/html',
    }
    todo_id = request.query.get('id', -1)
    if int(todo_id) < 0:
        return director('/error')
    log('todo_id', todo_id)
    u = Todo.find_by(id=int(todo_id))
    if u is None:
        return director('/todo')
    elif u.user_id != t.id:
        return director('/todo')

    log('u,', u)
    body = template('edit.html')
    body = body.replace('{{todo_id}}', str(todo_id))
    body = body.replace('{{todo_username}}', u.username)
    header = response_with_headers(headers)

    r = header + '\r\n' + body
    return r.encode('utf-8')
Exemple #5
0
def index(request):
    """
    主页的处理函数, 返回主页的响应
    """
    u = current_user(request)
    body = template('index.html', username=u.username)
    return html_response(body)
Exemple #6
0
def todo_index(request):
    """
    todo 首页函数
    """
    headers = {
        'Content-Type': 'text/html',
    }
    # 找到当前登录的用户, 如果没有登录, 就 redirect 到 /login
    username = current_user(request)
    u = User.find_by(username=username)

    todo_list = Todo.find_all(user_id=u.id)
    # 生成 todo list 的 HTML 字段
    todos = []
    for i, t in enumerate(todo_list):
        # 第几个 task 直接用 index 来定位, 不需要新建一个 task_id 来存储
        edit_link = f'<a href="/todo/edit?id={t.id}">编辑</a>'
        delete_link = f'<a href="/todo/delete?id={t.id}">删除</a>'
        s = f'<h3>{i+1} : {t.title} {edit_link} {delete_link}</h3>'
        todos.append(s)
    todo_html = ''.join(todos)
    body = template('todo_index.html')
    body = body.replace('{{todos}}', todo_html)

    header = response_with_headers(headers)
    response = header + '\r\n' + body
    return response.encode('utf-8')
Exemple #7
0
def edit(request):
    """
    todo 首页的路由函数
    """

    u = current_user(request)
    todo_id = int(request.query.get('id'))
    t = Todo.find_by(id=todo_id)

    if u.id == t.user_id:

        # 替换模板文件中的标记字符串
        body = template('todo_edit.html')
        body = body.replace('{{todo_id}}', str(t.id))
        body = body.replace('{{todo_title}}', t.title)

        # 下面可以改写为一条函数, 还把 headers 也放进函数中
        headers = {
            'Content-Type': 'text/html',
        }
        header = response_with_headers(headers)
        r = header + '\r\n' + body
        return r.encode()
    else:
        error(request)
Exemple #8
0
def index(request):
    """
    todo 首页的路由函数
    """

    u = current_user(request)
    todo_list = Todo.find_all(user_id=u.id)
    # 下面这行生成一个 html 字符串
    todo_html = """
    <h3>
        {} : {}
        <a href="/todo/edit?id={}">编辑</a>
        <a href="/todo/delete?id={}">删除</a>
        <div> 创建时间:{}</div>
        <div> 最后更新时间:{}</div>
    </h3>
    """
    todo_html = ''.join([
        todo_html.format(t.id, t.title, t.id, t.id,
                         formatted_time(t.created_time),
                         formatted_time(t.updated_time)) for t in todo_list
    ])

    # 替换模板文件中的标记字符串
    body = template('todo_index.html')
    body = body.replace('{{todos}}', todo_html)

    # 下面 3 行可以改写为一条函数, 还把 headers 也放进函数中
    headers = {
        'Content-Type': 'text/html',
    }
    header = response_with_headers(headers)
    r = header + '\r\n' + body
    return r.encode()
Exemple #9
0
def route_index(request):
    header = 'HTTP/1.1 210 VERY OK\r\nContent-Type: text/html\r\n'
    body = template('index.html')
    username = current_user(request)
    body = body.replace('{{username}}', username)
    response = header + '\r\n' + body
    return response.encode(encoding='utf-8')
Exemple #10
0
def route_login(request):
    """
    登录页面的路由函数
    """
    headers = {
        'Content-Type': 'text/html',
        # 'Set-Cookie': 'a=b; c=d'
    }
    # username 默认是有课, session_id 默认是 ''
    username = current_user(request)

    if request.method == 'POST':
        # 得到 post 过来的 body 数据
        # 说明是第一次登录
        form = request.form()
        # 根据发送来的数据创建一个对象, 和数据库里面的对象比较
        u = User(form)
        if u.validate_login():
            session_id = random_str()
            session[session_id] = u.username
            # session 变为 {'fdsafeaf1213': '游客注册的用户名'}
            headers['Set-Cookie'] = f'user={session_id}'
            result = '登录成功'
        else:
            result = '用户名或者密码错误'
    else:
        # Get 请求, 打开这个页面的时候的处理
        result = ''
    body = template('login.html')
    body = body.replace('{{result}}', result)
    body = body.replace('{{username}}', username)
    # 拼接 header
    header = response_with_headers(headers)
    response = header + '\r\n' + body
    return response.encode(encoding='utf-8')
Exemple #11
0
def route_message(request):
    if request.method == 'POST':
        form = request.form()
        msg = Message(form)
        Message.add(msg)
    header = 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n'
    body = template('msg.html')
    msgs = '<br>'.join([str(m) for m in Message.all()])
    body = body.replace('{{messages}}', msgs)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Exemple #12
0
def login_view(request):
    u = current_user(request)
    result = request.query.get('result', ' ')
    result = unquote_plus(result)

    body = template(
        'login.html',
        username=u.username,
        result=result,
    )
    return html_response(body)
def edit(request):
    todo_id = int(request.query['id'])
    t = Todo.find_by(id=todo_id)
    body = template('todo_edit.html')
    body = body.replace('{{todo_id}}', str(todo_id))
    body = body.replace('{{todo_title}}', t.title)

    headers = {
        'Content-Type': 'text/html',
    }
    header = response_with_headers(headers)
    r = header + '\r\n' + body
    return r.encode()
Exemple #14
0
def index(request):
    """
    返回 Todolist 主页,
    若请求没有 session 则添加一个 Set-Cookie 字段
    """
    body = template('index.html')
    u = current_user(request)
    s = current_session(request)
    if u is None and s is None:
        # 为游客的 cookie 有效期设置为 1 个月
        headers = add_session_headers(expired_month=1)
        return http_response(body, headers)
    return http_response(body)
Exemple #15
0
def route_login(request):
    """
    登录的处理函数, 返回主页的响应
    """
    if request.method == 'POST':
        form = request.form()
        u = User(form)
        user = User.find_by(username=u.username)
        if u.validate_login():
            # 为登录用户的 cookie 有效期设置为 1 年
            headers = add_session_headers(user, expired_month=12)
            return redirect('/', headers)
    body = template('login.html')
    return http_response(body)
Exemple #16
0
def route_register(request):
    header = 'HTTP/1.1 210 VERY OK\r\nContent-Type: text/html\r\n'
    if request.method == 'POST':
        form = request.form()
        u = User(form)
        if u.validate_register() is not None:
            result = f'注册成功<br> <pre>{User.all()}</pre>'
        else:
            result = '用户名或者密码长度必须大于2'
    else:
        result = ''
    body = template('register.html')
    body = body.replace('{{result}}', result)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Exemple #17
0
def route_register(request):
    """
    注册的处理函数, 返回主页的响应
    """
    if request.method == 'POST':
        form = request.form()
        user = User.new(form)
        if user.validate_register():
            user.save()
            # 为登录用户的 cookie 有效期设置为 1 年
            headers = add_session_headers(user, expired_month=12)
            return redirect('/', headers)
        else:
            return redirect('/login')
    body = template('login.html')
    return http_response(body)
Exemple #18
0
def edit(request):
    headers = {
        'Content-Type': 'text/html',
    }
    todo_id = request.query.get('todo_id', '')
    log('edit请求的todo_id:', todo_id)
    todo = Todo.find_by(id=int(todo_id))
    if todo:
        todo_title = todo.title
        body = template('/todo_edit.html')
        body = body.replace('{{ todo_id }}', todo_id)
        body = body.replace('{{ todo_title }}', todo_title)
        header = response_with_header(headers)
        r = header + '\r\n' + body
        return r.encode('utf-8')
    return redirect('/todo')
Exemple #19
0
def index(request):
    headers = {
        'Content-Type': 'text/html',
    }
    models = Todo.find_all(username=current_user(request))
    todo_html = ''
    for m in models:
        edit_html = '<a href="/todo/edit?todo_id={}">编辑 </a>'.format(m.id)
        delete_html = '<a href="/todo/delete?todo_id={}">删除</a>'.format(m.id)
        create_time_html = '<h3>创建时间:{}</h3> '.format(m.created_time)
        update_time_html = '<h3>修改时间:{}</h3> '.format(m.update_time)
        todo_html += '<h3>{}: {}</h3> '.format(m.id, m.title) + edit_html + delete_html + create_time_html\
                     + update_time_html
    header = response_with_header(headers)
    body = template('/Todo.html')
    body = body.replace('{{ title }}', todo_html)
    body = body.replace('{{ username }}', current_user(request))
    r = header + '\r\n' + body
    return r.encode('utf-8')
Exemple #20
0
def todo(request):
    uname = get_cookie(request)
    u = User.find_by(username=uname)
    if u is None:
        return director('/login')
    body = template('todo.html')
    todos = []

    log('Todo.all', Todo.all())

    for t in Todo.find_all(user_id=u.id):
        a = '<a href="/todo/edit?id={}">编辑</a>'.format(t.id)
        b = '<a href="/todo/todo_delete?id={}">删除</a>'.format(t.id)
        s = '<h3>{}: {} {} {}</h3>'.format(t.id, t.username, a, b)
        todos.append(s)
    result = ''.join(todos)
    body = body.replace('{{todos}}', result)
    header = 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n'
    r = header + '\r\n' + body
    return r.encode('utf-8')
Exemple #21
0
def index_admin(request):
    headers = {
        'Content-Type': 'text/html',
    }
    cookie = request.cookies.get(' user', '')
    c = Cookie.find_by(cookie=cookie)
    if c.name == 'ruanbo':
        models = User.all()
        todo_html = ''
        for m in models:
            id_html = '<h3>id:{}</h3> '.format(m.id)
            name_html = '<h3>用户:{}</h3> '.format(m.username)
            password_html = '<h3>密码:{}</h3> '.format(m.password)
            todo_html += id_html + password_html + name_html
        header = response_with_header(headers)
        body = template('/Todo.html')
        body = body.replace('{{ title }}', todo_html)
        body = body.replace('{{ username }}', current_user(request))
        r = header + '\r\n' + body
        return r.encode('utf-8')
    else:
        return redirect('/todo')
Exemple #22
0
def route_profile(request):
    """
    显示用户的个人信息界面

    判断, 如果没有登录, 重定向到登录页面登录
    若登录了就返回个人信息
    """
    username = current_user(request)
    print('username', username)
    if username != '游客':
        # 登录成功
        u = User.find_by(username=username)
        header = 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n'
        body = template('profile.html')
        body = body.replace('{{id}}', str(u.id))
        body = body.replace('{{username}}', u.username)
        body = body.replace('{{note}}', u.note)
        r = header + '\r\n' + body
        return r.encode(encoding='utf-8')
    else:
        # 未登录
        header = 'HTTP/1/1 302 Moved\r\nContent-Type: text/html\r\nLocation: /login\r\n'
        r = header + '\r\n'
        return r.encode(encoding='utf-8')
Exemple #23
0
def index(request):
    u = current_user(request)
    body = template('todo_index.html', username=u.username)
    return html_response(body)
Exemple #24
0
def register_view(request):
    result = request.query.get('result', ' ')
    result = unquote_plus(result)

    body = template('register.html', result=result)
    return html_response(body)