示例#1
0
def comment_add(request):
    user = current_user(request)
    form = request.form()
    c = Comment.new(form, user_id=user.id, user_name=user.username)
    # c.save()
    uid = c.tweet().user().id
    return redirect('/tweet/index?user_id={}'.format(uid))
示例#2
0
def comment_delete(request):
    u = current_user(request)
    comment_id = request.query.get('id', -1)
    comment_id = int(comment_id)
    c = Comment.find(comment_id)
    if u.id == c.user_id:
        c.remove(comment_id)
    return redirect('/tweet/index?user_id={}'.format(u.id))
示例#3
0
def add(request):
    u = current_user(request)
    form = request.form()
    c = Comment(form)
    c.user_id = u.id
    c.created_time = c.change_time()
    c.save()
    return redirect('/weibo')
示例#4
0
def add(request):
    user = current_user(request)
    form = request.form()
    t = Tweet.new(form, user_id=user.id, user_name=user.username)
    # t.user_id = u.id
    # t.save()
    # redirect有必要加query吗
    return redirect('/tweet/index?user_id={}'.format(user.id))
示例#5
0
def delete(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)
    Weibo.delete(weibo_id)
    return redirect('/weibo/detail')
示例#6
0
def add(request):
    u = current_user(request)
    form = request.form()
    w = Weibo(form)
    w.user_id = u.id
    w.created_time = w.change_time()
    w.save()
    return redirect('/weibo')
示例#7
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)
示例#8
0
def delete(request):
    comment_id = int(request.query.get('id', -1))
    c = Comment.find_by(id=comment_id)
    w = Weibo.find_by(id=c.weibo_id)
    u = current_user(request)
    if w.user_id != u.id:
        return error(request)
    Comment.delete(comment_id)
    return redirect('/weibo/detail')
示例#9
0
def update(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)
    form = request.form()
    w.content = form.get('content')
    w.save()
    return redirect('/weibo/detail')
示例#10
0
def edit(request):
    u = current_user(request)
    tweet_id = int(request.query.get('id', -1))
    t = Tweet.find(tweet_id)
    if u.id == t.user_id:
        body = template('tweet_edit.html',
                        tweet_id=t.id,
                        tweet_content=t.content)
        return http_response(body)
    return redirect('/tweet/index?user_id={}'.format(u.id))
示例#11
0
def comment_edit(request):
    u = current_user(request)
    comment_id = int(request.query.get('id', -1))
    c = Comment.find(comment_id)
    if u.id == c.user_id:
        body = template('comment_edit.html',
                        comment_id=c.id,
                        comment_content=c.content)
        return http_response(body)
    return redirect('/tweet/index?user_id={}'.format(u.id))
示例#12
0
def delete(request):
    u = current_user(request)
    tweet_id = int(request.query.get('id'))
    t = Tweet.find(tweet_id)
    if u.id == t.user_id:
        # 这里只是删除了tweet,但是其所拥有的comment的deleted字段变成False
        t.remove(tweet_id)
        for c in t.comments():
            c.deleted = True
            c.save()
    # redirect有必要加query吗
    return redirect('/tweet/index?user_id={}'.format(u.id))
示例#13
0
def index(request):
    # 从query取到user_id可以到该用户界面评论
    user_id = int(request.query.get('user_id', -1))
    if user_id == -1:
        u = current_user(request)
        user_id = u.id
    user = User.find(user_id)
    if user is None:
        return redirect('/login')
    else:
        tweets = Tweet.find_all(user_id=user.id, deleted=False)
        body = template('tweet_index.html', tweets=tweets, user=user)
        return http_response(body)
示例#14
0
def add(request):
    """
    接受浏览器发过来的 添加todo 请求
    添加数据并发一个 302 定向给浏览器
    浏览器就会去请求 /todo 从而回到主页
    """
    # 得到浏览器发送的表单
    form = request.form()
    uid = current_user(request)
    # 创建一个 todo
    Todo.new(form, uid)
    # 让浏览器刷新页面到主页去
    return redirect('/todo')
示例#15
0
def delete_todo(request):
    """
    todo delete 的路由函数
    """
    # 得到当前编辑的 todo的 id
    todo_id = int(request.query.get('id', -1))
    t = Todo.find_by(id=todo_id)

    uid = current_user(request)
    u = User.find_by(id=uid)
    # 验证用户id是否相同
    if t.user_id != u.id:
        return redirect('/login')

    if t is not None:
        # 删除 todo
        t.remove()
    return redirect('/todo')
示例#16
0
def edit(request):
    """
    todo edit 的路由函数
    """
    # 得到当前编辑的 todo的 id
    todo_id = int(request.query.get('id', -1))
    t = Todo.find_by(id=todo_id)

    uid = current_user(request)
    u = User.find_by(id=uid)
    # 验证用户id是否相同
    if t.user_id != u.id:
        return redirect('/login')
    # 替换模板文件中的标记字符串
    body = template('todo_edit.html')
    body = body.replace('{{ todo_id }}', str(t.id))
    body = body.replace('{{ todo_task }}', str(t.task))
    return http_response(body)
示例#17
0
def index(request):
    """
    todo 首页 的路由函数
    """
    # 找到当前登录的用户, 如果没登录, 就 redirect 到 /login
    uid = current_user(request)
    todo_list = Todo.find_all(user_id=uid)
    todo_html = ''
    if todo_list is not None:
        # 下面生成一个 html 字符串
        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.task, edit_link, delete_link)
            todos.append(s)
        todo_html = ''.join(todos)
    # 替换模板文件中的标记字符串
    body = template('todo_index.html')
    body = body.replace('{{ todos }}', todo_html)
    return http_response(body)
示例#18
0
def detail(request):
    u = current_user(request)
    weibo_list = Weibo.find_all(user_id=u.id)
    body = template('weibo_detail.html', weibos=weibo_list)
    return http_response(body)