Ejemplo n.º 1
0
def delete(request):
    weibo_id = int(request.query['weibo_id'])
    Weibo.delete(weibo_id)
    cs = Comment.all(weibo_id=weibo_id)
    for c in cs:
        c.delete()
    return redirect('/weibo/index')
Ejemplo n.º 2
0
def delete(request):
    weibo_id = int(request.query['weibo_id'])
    print('shuchu')
    Weibo.delete(weibo_id)
    cs = Comment.all(weibo_id=int(weibo_id))
    for c in cs:
        c.delete(c.id)

    return redirect('/weibo/index')
Ejemplo n.º 3
0
def update(request):
    """
    用于增加新 weibo 的路由函数
    """
    form = request.form()
    weibo_id = int(form['weibo_id'])
    Weibo.update(weibo_id, content=form['content'])
    # 浏览器发送数据过来被处理后, 重定向到首页
    # 浏览器在请求新首页的时候, 就能看到新增的数据了
    return redirect('/weibo/index')
Ejemplo n.º 4
0
def add(request):
    """
    用于增加新 weibo 的路由函数
    """
    u = current_user(request)
    form = request.form()
    Weibo.add(form, u.id)
    # 浏览器发送数据过来被处理后, 重定向到首页
    # 浏览器在请求新首页的时候, 就能看到新增的数据了
    return redirect('/weibo/index')
Ejemplo n.º 5
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.º 6
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.º 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 f(request):
        log('weibo_owner_required')
        u = current_user(request)
        # id_key = 'weibo_id'
        if 'weibo_id' in request.query:
            weibo_id = request.query['weibo_id']
            print('ceshi', weibo_id)
        else:
            weibo_id = request.form()['weibo_id']

        w = Weibo.one(id=int(weibo_id))

        print('haha', w)
        if w.user_id == u.id:
            return route_function(request)
        else:
            return redirect('/weibo/index')
Ejemplo n.º 9
0
def edit(request):
    weibo_id = int(request.query['weibo_id'])
    w = Weibo.one(id=weibo_id)
    return html_response('weibo_edit.html', weibo=w)
Ejemplo n.º 10
0
def test():
    connection = pymysql.connect(
        host='localhost',
        user='******',
        password='******'.format(secret.database_password),
        # db='web8',
        charset='utf8mb4',
        cursorclass=pymysql.cursors.DictCursor)

    with connection.cursor() as cursor:
        cursor.execute('DROP DATABASE IF EXISTS`web`')
        cursor.execute('CREATE DATABASE `web` CHARACTER SET utf8mb4')
        cursor.execute('USE `web`')

        cursor.execute(User.sql_create)
        cursor.execute(Session.sql_create)
        cursor.execute(Todo.sql_create)
        cursor.execute(Weibo.sql_create)
        cursor.execute(Comment.sql_create)

    connection.commit()
    connection.close()

    form = dict(
        username='******',
        password='******',
    )
    User.register_user(form)
    u, result = User.login_user(form)
    assert u is not None, result
    form = dict(
        username='******',
        password='******',
    )
    User.register_user(form)

    session_id = random_string()
    form = dict(
        session_id=session_id,
        user_id=u.id,
    )
    Session.new(form)
    s: Session = Session.one(session_id=session_id)
    assert s.session_id == session_id

    form = dict(
        title='test todo',
        user_id=u.id,
    )
    t = Todo.add(form, u.id)
    assert t.title == 'test todo'

    form = dict(
        content='test weibo',
        user_id=u.id,
    )
    w = Weibo.add(form, u.id)
    assert w.content == 'test weibo'

    form = dict(
        content='test comment',
        user_id=u.id,
    )
    c = Comment.add(form, u.id, w.id)
    assert c.content == 'test comment'