Ejemplo n.º 1
0
def route_login(request):
    headers = {
        'Content-Type': 'text/html',
    }
    log('login, cookies ({})'.format(request.cookies))
    username = current_user(request)
    if request.method == 'POST':
        form = request.form()
        u = User(form)
        if u.validate_login():
            session_id = random_str()
            session[session_id] = u.username
            headers['Set-Cookie'] = 'user={}'.format(session_id)
            result = '登陆成功'
            username = u.username
        else:
            result = '用户名或密码错误'
    else:
        result = ''
    body = template('login.html',
                    result=result,
                    username=username, )
    header = response_with_headers(headers)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Ejemplo n.º 2
0
def route_weibo_index(request):
    headers = {
        'Content-Type': 'text/html',
    }
    # username = current_user(request)
    # if username == '游客':
    #     # 没登录 不让看 重定向到 /
    #     return redirect('/login')
    # else:
    header = response_with_headers(headers)
    user_id = request.query.get('user_id', -1)
    user_id = int(user_id)
    user = User.find(user_id)
    if user is None:
        return error(request)
    # 找到 user 发布的所有 weibo
    weibos = Weibo.find_all(user_id=user_id)
    log('weibos', weibos)

    def weibo_tag(weibo):
        return '<p>{} from {}@{} <a href="/weibo/delete?id={}">删除</a></p>'.format(
            weibo.content,
            user.username,
            weibo.created_time,
            weibo.id,
        )

    weibos = '\n'.join([weibo_tag(w) for w in weibos])
    body = template('weibo_index.html', weibos=weibos)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Ejemplo n.º 3
0
def route_login(request):
    """
    登录页面的路由函数
    """
    headers = {
        'Content-Type': 'text/html',
    }
    username = current_user(request)
    if request.method == 'POST':
        form = request.form()
        u = User(form)
        if u.validate_login():
            session_id = random_str()
            session[session_id] = u.username
            headers['Set-Cookie'] = 'user={}'.format(session_id)
            result = '登录成功'
            # return redirect('/weibo/new')
        else:
            result = '用户名或者密码错误'
    else:
        result = ''
    body = template('login.html', result=result, username=username)
    header = response_with_headers(headers)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Ejemplo n.º 4
0
def route_login(request):
    headers = {
        'Content-Type': 'text/html',
    }
    log('login, headers', request.headers, request.method)
    username = current_user(request)
    if request.method == 'POST':
        form = request.form()
        u = Users(form)
        if u.validate_login():
            session_id = random_str()
            session[session_id] = u.username
            headers['Set-Cookie'] = 'user={}'.format(session_id)
            result = "登录成功"
        else:
            result = '没登录'
    else:
        result = ''
    log('result', result)
    body = template("login.html",
                    result=result,
                    username=username)
    header = response_with_headers(headers)
    r = header + '\r\n' + body
    log('body', body)
    return r.encode(encoding='utf-8')
Ejemplo n.º 5
0
def route_blog(request):
    headers = {
        'Content-Type': 'text/html',
    }
    header = response_with_headers(headers)
    body = template('starbuzz/blog.html')
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Ejemplo n.º 6
0
def route_weibo_new(request):
    headers = {
        'Content-Type': 'text/html',
    }
    username = current_user(request)
    header = response_with_headers(headers)
    user = User.find_by(username=username)
    body = template('weibo_new.html')
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Ejemplo n.º 7
0
def route_add(request):
    headers = {
        'Content-Type': 'text/html',
    }
    header = response_with_headers(headers)
    form = request.form()
    o = Todo(form)
    o.save()
    body = o.json_str()
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Ejemplo n.º 8
0
def route_profile(request):
    headers = {
        'Content-Type': 'text/html',
    }
    username = current_user(request)
    header = response_with_headers(headers)
    user = Users.find(username)
    body = template('profile.html', id=user.id,
                    username=user.name,
                    note=user.note)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Ejemplo n.º 9
0
def route_weibo_index(request):
    headers = {
        'Content-Type': 'text/html',
    }
    header = response_with_headers(headers)
    user_id = request.query.get('user_id', -1)
    user_id = int(user_id)
    user = User.find(user_id)
    if user is None:
        return error(request)
    # 找到 user 发布的所有 weibo
    weibos = Weibo.find_all(user_id=user_id)
    log('weibos', weibos)
    current_username = current_user(request)
    u = User.find_by(username=current_username)
    if u is None:
        return redirect('/login')

    def weibo_tag(weibo):
        comment_list = Comment.find_all(weibo_id=weibo.id)
        comments = '<br>'.join([c.content for c in comment_list])
        w = {
            "id": weibo.id,
            "user_id": u.id,
            "content": weibo.content,
            "username": user.username,
            "time": weibo.created_time,
            "comments": comments,
        }
        log('comments debug', comment_list)
        return """
        <p>{content} from {username}@{time}
            <a href="/weibo/delete?id={id}">删除</a>
            <a href="/weibo/edit?id={id}">修改</a>
            <button class="gua-show-comment" data-id="{id}">评论</button>
            <div>
                {comments}
            </div>
            <div id="id-div-comment-{id}" class="gua-comment-form gua-hide">
            <form action="/weibo/comment/add" method="post">
                <input name="user_id" value="{user_id}" type="hidden">
                <input name="weibo_id" value="{id}" type="hidden">
                <textarea name="content"></textarea>
                <button type="submit">添加评论</button>
            </form>
            </div>
        </p>
        """.format(**w)

    weibos = '\n'.join([weibo_tag(w) for w in weibos])
    body = template('weibo_index.html', weibos=weibos)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Ejemplo n.º 10
0
def route_weibo_add(request):
    headers = {
        'Content-Type': 'text/html',
    }
    username = current_user(request)
    header = response_with_headers(headers)
    user = User.find_by(username=username)
    # 创建微博
    form = request.form()
    w = Weibo(form)
    w.user_id = user.id
    w.save()
    return redirect('/weibo?user_id={}'.format(user.id))
Ejemplo n.º 11
0
def route_weibo_delete(request):
    headers = {
        'Content-Type': 'text/html',
    }
    username = current_user(request)
    header = response_with_headers(headers)
    user = User.find_by(username=username)
    # 删除微博
    weibo_id = request.query.get('id', None)
    weibo_id = int(weibo_id)
    w = Weibo.find(weibo_id)
    w.delete()
    return redirect('/weibo?user_id={}'.format(user.id))
Ejemplo n.º 12
0
def route_weibo_edit(request):
    headers = {
        'Content-Type': 'text/html',
    }
    header = response_with_headers(headers)
    weibo_id = request.query.get('id', -1)
    weibo_id = int(weibo_id)
    w = Weibo.find(weibo_id)
    if w is None:
        return error(request)
    # 生成一个 edit 页面
    body = template('weibo_edit.html', weibo_id=w.id, weibo_content=w.content)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Ejemplo n.º 13
0
def route_index(request):
    headers = {
        'Content-Type': 'text/html',
    }
    header = response_with_headers(headers)
    todos = Todo.all()

    def todo_tag(t):
        status = t.status()
        return '<p>{} {}@{} 状态: {} <a href="/todo/complete?id={}">完成</a></p>'.format(
            t.id, t.content, t.created_time, status, t.id)

    todo_html = '\n    <br>\n    '.join([todo_tag(w) for w in todos])
    body = template('todo_index.html', todos=todo_html)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Ejemplo n.º 14
0
def route_message(request):
    headers = {
        'Content-Type': 'text/html',
    }
    log('本次请求的method', request.method)
    username = current_user(request)
    log('username', username)
    header = response_with_headers(headers)
    if request.method == "POST":
        form = request.form()
        message = Message(form)
        log('post', form)
        message_list.append(message)
    msgs = '<br>'.join(str(m) for m in message_list)
    body = template('html_basic.html', messages=msgs)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Ejemplo n.º 15
0
def route_message(request):
    """
    消息页面的路由函数
    """
    headers = {
        'Content-Type': 'text/html',
    }
    username = current_user(request)
    header = response_with_headers(headers)
    if request.method == 'POST':
        form = request.form()
        msg = Message(form)
        message_list.append(msg)
        # 应该在这里保存 message_list
    msgs = '<br>'.join([str(m) for m in message_list])
    body = template('html_basic.html', messages=msgs)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Ejemplo n.º 16
0
def route_profile(request):
    """
    如果登录了, 则返回一个页面显示用户的
    三项资料(id, username, note)
    """
    headers = {
        'Content-Type': 'text/html',
        # 'Set-Cookie': 'height=169',
        # 'Location': ''
    }
    username = current_user(request)
    header = response_with_headers(headers)
    user = User.find_by(username=username)
    body = template('profile.html',
                    id=user.id,
                    username=user.username,
                    note=user.note)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')

    pass
Ejemplo n.º 17
0
def route_message(request):
    """
    主页的处理函数, 返回主页的响应
    """
    headers = {
        'Content-Type': 'text/html',
        # 'Set-Cookie': 'height=169',
        # 'Location': ''
    }
    # log('本次请求的 method', request.method)
    header = response_with_headers(headers)
    if request.method == 'POST':
        form = request.form()
        msg = Message(form)
        msg.save()
        message_list.append(msg)
        # 应该在这里保存 message_list
    msgs = '<br>'.join([str(m) for m in message_list])
    body = template('html_basic.html',
                    messages=msgs)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Ejemplo n.º 18
0
def route_message(request):
    """
    消息页面的路由函数
    """
    headers = {
        'Content-Type': 'text/html',
        # 'Set-Cookie': 'height=169; gua=1; pwd=2; Path=/',
        # 'Location': ''
    }

    log('本次请求的 method', request.method)
    username = current_user(request)
    log('username', username)
    header = response_with_headers(headers)
    if request.method == 'POST':
        form = request.form()
        msg = Message(form)
        log('post', form)
        message_list.append(msg)
        # 应该在这里保存 message_list
    msgs = '<br>'.join([str(m) for m in message_list])
    body = template('html_basic.html', messages=msgs)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')