Exemplo n.º 1
0
def like_status(tid):
    username = session.get('username', None)
    user = User.find_one({}, username=username)
    if user is None:
        comments = Comment.find_all({}, tid=tid)
        if comments is None:
            return 'false'
        status = {
            'like': [False for i in comments],
            'dislike': [False for i in comments]
        }
        return jsonify(status)
    else:
        User.update_one({'username': username}, {'active_time': time.time()})
        comments = Comment.find_all({}, tid=tid)
        if comments is None:
            return 'false'
        like = []
        dislike = []
        # 遍历该帖所有评论
        for c in comments:
            like_flag = False
            dislike_flag = False
            # 判断是否有人支持
            if len(c['likes']) < 1:
                like.append(False)
            else:
                # 遍历每一个评论的支持列表
                for l in c['likes']:
                    # 判断该评论的支持列表是否存在当前浏览的用户
                    if l.get('uid') == user.get('uid'):
                        like.append(True)
                        like_flag = True
                        break
                if like_flag is not True:
                    like.append(False)
            # 判断是否有人反对
            if len(c['dislikes']) < 1:
                dislike.append(False)
            else:
                # 遍历每一个评论的反对列表
                for d in c['dislikes']:
                    # 判断该评论的反对列表是否存在当前浏览的用户
                    if d.get('uid') == user.get('uid'):
                        dislike.append(True)
                        dislike_flag = True
                        break
                if dislike_flag is not True:
                    dislike.append(False)
        status = {
            'like': like,
            'dislike': dislike
        }
        return jsonify(status)
Exemplo n.º 2
0
def all(request):
    weibos = Weibo.all_json()
    # 下面是 weibo 的 all 路由只看到自己 weibo 的方法
    # u = current_user(request)
    # weibos = Weibo.find_all(user_id=u.id)
    # weibos = [w.json() for w in weibos]

    weibo_list = []
    for i in range(len(weibos)):
        weibo_list.append(
            dict(id=weibos[i]['id'],
                 content=weibos[i]['content'],
                 user_id=weibos[i]['user_id'],
                 weibo_user=User.find_by(id=weibos[i]['user_id']).username))
    weibos = weibo_list

    for weibo in weibos:
        comments = Comment.find_all(weibo_id=weibo['id'])
        comment_list = []
        for i in range(len(comments)):
            comment_list.append(
                dict(id=comments[i].id,
                     content=comments[i].content,
                     user_id=comments[i].user_id,
                     weibo_id=comments[i].weibo_id,
                     comment_user=User.find_by(
                         id=comments[i].user_id).username))
        weibo['comments'] = comment_list
    log('allweibos', weibos)
    return json_response(weibos)
Exemplo n.º 3
0
def delete(request):
    weibo_id = int(request.query['id'])
    Weibo.delete(weibo_id)
    # 注意删除所有微博对应评论
    cs = Comment.find_all(weibo_id=weibo_id)
    for c in cs:
        c.delete()
    return redirect('/weibo/index')
Exemplo n.º 4
0
    def delete(cls, weibo_id):
        w = Weibo.find_by(id=weibo_id)
        cs = Comment.find_all(weibo_id=w.id)
        log('weibo_delete', cs)
        for c in cs:
            Comment.delete(c.id)

        super().delete(weibo_id)
Exemplo n.º 5
0
def delete(request):
    weibo_id = int(request.query['id'])
    Weibo.delete(weibo_id)
    comments = Comment.find_all(weibo_id=weibo_id)
    for i in range(len(comments)):
        Comment.delete(comments[i].id)
    d = dict(message="成功删除 weibo")
    return json_response(d)
Exemplo n.º 6
0
def delete():
    weibo_id = int(request.args['id'])
    Weibo.delete(weibo_id)
    comments = Comment.find_all(weibo_id=weibo_id)
    for c in comments:
        Comment.delete(c.id)
    d = dict(message="成功删除 weibo")
    return jsonify(d)
Exemplo n.º 7
0
def comment_all(request):
    log('comment_all路由函数接收到请求查询路径', request.query)
    id = int(request.query['weibo_id'])
    log('获取该微博id成功', id)
    c = Comment.find_all(weibo_id=id)
    log('获取该微博所有评论成功', c, type(c[0]))
    d = [i.json() for i in c]
    return json_response(d)
Exemplo n.º 8
0
def delete(request):
    weibo_id = int(request.query['id'])
    Weibo.delete(weibo_id)
    comments = Comment.find_all(weibo_id=weibo_id)
    for comment in comments:
        Comment.delete(comment.id)
    d = dict(status=210, message="成功删除 weibo")
    return json_response(d)
Exemplo n.º 9
0
def all():
    blog_id =int(request.args.get("blog_id"))
    #找出所有blog_id=blog_id的评论
    comment_list = Comment.find_all(blog_id=blog_id)
    log('comment_list::' ,comment_list)
    # # 要转换为 dict 格式才行
    comments = [t.json() for t in comment_list]
    #comments = Comment.all_json()
    return jsonify(comments)
Exemplo n.º 10
0
def delete(request):
    log('執行刪除函數')
    weibo_id = int(request.query['id'])
    Weibo.delete(weibo_id)
    c = Comment.find_all(weibo_id=weibo_id)
    del c
    d = dict(
        message="成功删除 weibo"
    )
    return json_response(d)
Exemplo n.º 11
0
def delete(request):
    weibo_id = int(request.query['id'])
    Weibo.delete(weibo_id)

    comment_id_list = Comment.find_all(weibo_id=weibo_id)
    for c in comment_id_list:
        Comment.delete(c.id)

    d = dict(message="成功删除 weibo和所属评论")

    return json_response(d)
Exemplo n.º 12
0
def delete(request):
    weibo_id = int(request.query['id'])
    Weibo.delete(weibo_id)
    comment = Comment.find_all(weibo_id=weibo_id)

    # 循环删除
    for i in comment:
        Comment.delete(int(i.id))

    d = dict(message="成功删除 weibo")
    return json_response(d)
Exemplo n.º 13
0
def all(request):
    weibos = Weibo.all_json()
    for w in weibos:
        u = User.find_by(id=w['user_id'])
        w['username'] = u.username
        comments = Comment.find_all(weibo_id=w['id'])
        w['comments'] = [coment.json() for coment in comments]
        for c in w['comments']:
            u = User.find_by(id=c['user_id'])
            c['username'] = u.username
    return json_response(weibos)
Exemplo n.º 14
0
def delete(request):
    weibo_id = int(request.query['id'])
    Weibo.delete(weibo_id)
    comments = Comment.find_all(weibo_id=weibo_id)
    for c in comments:
        c_id = c.id
        Comment.delete(c_id)
    d = dict(
        message="成功删除 weibo"
    )
    return json_response(d)
Exemplo n.º 15
0
def delete():
    weibo_id = int(request.args.get('id'))
    item = Weibo.find(weibo_id)
    if same_user_required(item):
        t = Weibo.delete(weibo_id)
        comments = Comment.find_all(weibo_id=str(weibo_id))
        for c in comments:
            Comment.delete(c.id)
        return jsonify(t.json())
    else:
        pass
Exemplo n.º 16
0
def all(request):
    weibos = Weibo.all_json()
    for w in weibos:
        weibo_id = w['id']
        comment = Comment.find_all(weibo_id=weibo_id)
        c = [c.json() for c in comment]
        w['comments'] = c
        # user_id = w['user_id']
        # user = User.find_by(id=user_id)
        # uname = user.username
        # w['user'] = uname
    return json_response(weibos)
Exemplo n.º 17
0
def blog_comment_recent(v):
    # 拿到reply_list,并翻转
    comment_list = Comment.find_all(user_id=v.id)
    comment_list.reverse()
    # 筛选reply_list的topic_id
    blog_id_list = []
    for i in comment_list:
        if i.blog_id not in blog_id_list:
            blog_id_list.append(i.blog_id)
    # 通过topic_id拿到对应的topic
    blog_list = []
    for i in blog_id_list:
        blog_list.append(Blog.find(i))
    return blog_list
Exemplo n.º 18
0
def all():
    """
    weibo 首页的路由函数
    """
    weibo_list = []
    weibos = Weibo.all()
    for weibo in weibos:
        form = weibo.json()
        comments = Comment.find_all(weibo_id=int(form['id']))
        comments = [comment.json() for comment in comments]
        form['comments'] = comments
        weibo_list.append(form)
    log('json weibo_list in weibo all', weibo_list)
    return jsonify(weibo_list)
Exemplo n.º 19
0
def all():
    log('执行 all 函数')
    weibos = Weibo.all_json()
    # log('weibos---', weibos)
    for weibo in weibos:
        u = User.find_by(id=weibo['user_id'])
        weibo['username'] = u.username
        cs = Comment.find_all(weibo_id=weibo['id'])
        cs = [c.json() for c in cs]
        weibo['comments'] = []
        for c in cs:
            u = User.find_by(id=c['user_id'])
            c['username'] = u.username
            weibo['comments'].append(c)
    # log('weibo---', weibos)
    return jsonify(weibos)
Exemplo n.º 20
0
def useful_weibos(weibo):
    """
    该函数的作用是拿到该微博评论数据 ,拼成一个字典
    """
    user = User.find_by(id=weibo['user_id'])
    weibo['username'] = user.username
    # 拿到该微博的所有评论
    comments = Comment.find_all(weibo_id=weibo['id'])
    # 拿到评论者的名字,并加入到comment中
    log('useful_weibos 函数 comments 数据', comments)
    comments = [c.__dict__ for c in comments]
    for c in comments:
        comment_user = User.find_by(id=c['user_id'])
        c['username'] = comment_user.username
    # 把评论comment加入weibo字典里
    weibo['comments'] = comments
    return weibo
Exemplo n.º 21
0
def all(request):
    user = current_user(request)
    # log('user is',u, 'user type is', type(u))
    u = dict(
        id=user.id,
        username=user.username,
    )
    weibos = Weibo.all_json()
    for weibo in weibos:
        weibo['username'] = User.find_by(id=weibo['user_id']).username
        comments = Comment.find_all(weibo_id=weibo['id'])
        cms = []
        for m in comments:
            m.username = m.user().username
            cms.append(m.json())
        weibo['comments'] = cms
    weibos.insert(0, u)
    log('weibo with comments', weibos)
    return json_response(weibos)
Exemplo n.º 22
0
def test():
    cs = Comment.find_all(user_id=2)
    print(cs, '评论数', len(cs))
    test_tweet()
    # 测试数据关联
    form = {'task': 'gua 的 todo'}
    Todo.new(form, 1)
    # 得到 user 的所有 todos
    u1 = User.find(1)
    u2 = User.find(2)
    ts1 = u1.todos()
    ts2 = u2.todos()
    log('gua de todos', ts1)
    log('xiao de todos', ts2)
    assert len(ts1) > 0
    assert len(ts2) == 0

    test_create()
    test_read()
    test_update()
    test_delete()
    Todo.complete(1, True)
Exemplo n.º 23
0
 def comments(self):
     return Comment.find_all(tweet_id=self.id)
Exemplo n.º 24
0
 def comments(self):
     cs = Comment.find_all(weibo_id=self.id)
     return cs
Exemplo n.º 25
0
 def get(self):
     comment = Comment.find_all()
     return comment
Exemplo n.º 26
0
 def comments(self):
     return Comment.find_all(blog_id=self.id)
Exemplo n.º 27
0
 def delete_with_comments(cls, user_id):
     cs = Comment.find_all(blog_id=user_id)
     for c in cs:
         Comment.delete(c.id)
     cls.delete(user_id)
Exemplo n.º 28
0
 def comments(self):
     return Comment.find_all(weibo_id=self.id)
Exemplo n.º 29
0
 def comments(self):
     # return [c for c in Comment.all() if c.tweet_id == self.id]
     return Comment.find_all(tweet_id=self.id)
Exemplo n.º 30
0
def get_target_comments(tid):
    username = session.get('username', None)
    if username is not None:
        User.update_one({'username': username}, {'active_time': time.time()})
    result = Comment.find_all({}, tid=tid)
    return jsonify(result)