Ejemplo n.º 1
0
    def f(request):
        log('comment_or_weibo_owner_required')
        if request.method == 'GET':
            data = request.query
        elif request.method == 'POST':
            data = request.form()
        else:
            raise ValueError('不支持的方法', request.method)

        comment_key = 'comment_id'
        weibo_key = 'weibo_id'
        if comment_key in data:
            c = Comment.one(id=int(data[comment_key]))
            if c is None:
                return redirect('/weibo/index')
            else:
                user_id = c.user_id
        elif weibo_key in data:
            w = Weibo.one(id=int(data[weibo_key]))
            if w is None:
                return redirect('/weibo/index')
            else:
                user_id = w.user_id
        else:
            raise ValueError('不支持的参数', data)

        u = current_user(request)
        if user_id == u.id:
            log('不是评论或者微博的作者', user_id, u.id)
            return route_function(request)
        else:
            return redirect('/weibo/index')
Ejemplo n.º 2
0
def route_register_view(request):
    user = current_user(request)
    result = request.query.get('result', '')
    result = urllib.parse.unquote_plus(result)
    return html_response('register.html',
                         result=result,
                         username=user.username)
Ejemplo n.º 3
0
def add(request):
    """
    用于增加新 weibo 的路由函数
    """
    u = current_user(request)
    form = request.form()
    Weibo.add(form, u.id)
    # 浏览器发送数据过来被处理后, 重定向到首页
    # 浏览器在请求新首页的时候, 就能看到新增的数据了
    return redirect('/weibo/index')
Ejemplo n.º 4
0
def index(request):
    """
    weibo 首页的路由函数
    """
    if 'user_id' in request.query:
        user_id = int(request.query['user_id'])
    else:
        u = current_user(request)
        user_id = u.id
    weibos = Weibo.all(user_id=user_id)
    # 替换模板文件中的标记字符串
    return html_response('weibo_index.html', weibos=weibos)
Ejemplo n.º 5
0
def route_login_view(request):
    """
    登录页面视图
    """

    log('login, headers', request.headers)
    log('login, cookies', request.cookies)
    user = current_user(request)
    log('current user', user)

    result = request.query.get('result', '')
    result = urllib.parse.unquote_plus(result)

    return html_response('login.html', result=result, username=user.username)
Ejemplo n.º 6
0
    def f(request):
        log('comment_owner_required')
        u = current_user(request)
        id_key = 'comment_id'
        if id_key in request.query:
            comment_id = request.query[id_key]
        else:
            comment_id = request.form()[id_key]
        c = Comment.one(id=int(comment_id))

        if c.user_id == u.id:
            log('不是评论作者', c)
            return route_function(request)
        else:
            return redirect('/weibo/index')
Ejemplo n.º 7
0
    def f(request):
        log('weibo_owner_required')
        u = current_user(request)
        id_key = 'weibo_id'
        if id_key in request.query:
            weibo_id = request.query[id_key]
        else:
            weibo_id = request.form()[id_key]
        w = Weibo.one(id=int(weibo_id))

        if w.user_id == u.id:
            log('不是微博作者', w)
            return route_function(request)
        else:
            return redirect('/weibo/index')
Ejemplo n.º 8
0
def route_login(request):
    """
    登录页面的路由函数
    """
    log('login, headers', request.headers)
    log('login, cookies', request.cookies)
    user_current = current_user(request)
    log('current user', user_current)

    form = request.form()
    user_login, result = User.login_user(form)
    result = urllib.parse.quote_plus(result)
    if user_login is not None:
        session_id = Session.add(user_login.id)
        headers = {'Set-Cookie': 'session_id={}'.format(session_id)}
    else:
        headers = {}

    # return redirect('/todo')
    return redirect('/login/view', result=result, headers=headers)
Ejemplo n.º 9
0
def comment_add(request):
    u = current_user(request)
    form = request.form()
    weibo_id = int(form['weibo_id'])
    Comment.add(form, u.id, weibo_id)
    return redirect('/weibo/index')
Ejemplo n.º 10
0
def route_index(request):
    """
    主页的处理函数, 返回主页的响应
    """
    u = current_user(request)
    return html_response('index.html', username=u.username)