Example #1
0
def edit(t_id):
    t = Topic.find_by(id=t_id)
    if t is None:
        abort(404)
    else:
        bs = Board.find_all()
        return render_template("topic/edit.html", topic=t, bs=bs)
Example #2
0
def set_top(id):
    """
    设置话题置顶
    """
    topic = Topic.find_by(id=id)
    topic.set_top()
    return redirect(url_for('.detail', id=id))
Example #3
0
def cancel_top(id):
    """
    取消话题置顶
    """
    topic = Topic.find_by(id=id)
    topic.cancel_top()
    return redirect(url_for('.detail', id=id))
Example #4
0
def edit(topic_id):
    u = current_user()
    t = Topic.find_by(id=topic_id)
    if u is None or u.id != t.user_id:
        return redirect(url_for('index.index'))
    boards = Board.all()
    return render_template("topic/edit.html", boards=boards, u=u, topic=t)
Example #5
0
def detail(topic_id):
    t = Topic.find_by(_id=ObjectId(topic_id))
    t.view += 1
    t.update()
    u = current_user()
    topic_writer = User.find_by(_id=ObjectId(t.user_id))
    r = Reply.find_all(topic_id=ObjectId(topic_id))
    topic_writer_topics = Topic.find_by_sort("ct",
                                             user_id=ObjectId(
                                                 topic_writer._id))
    if len(topic_writer_topics) > 10:
        topic_writer_topics = topic_writer_topics[:10]
    if u is not None and topic_writer._id != u._id:
        be_show = topic_writer._id not in u.following_list
    else:
        be_show = None
    if u:
        show_reply = True
    else:
        show_reply = None
    return render_template("BBS/topic_detail.html",
                           t=t,
                           replys=r,
                           u=topic_writer,
                           ts=topic_writer_topics,
                           beshow=be_show,
                           show_reply=show_reply)
Example #6
0
def delete(topic_id):
    u = current_user()
    t = Topic.find_by(id=topic_id)
    if u is None or u.id != t.user_id:
        return redirect(url_for('index.index'))
    Topic.delete(topic_id)
    return redirect(url_for('topic.index'))
Example #7
0
def update():
    form = request.form
    t_id = int(form.get('id'))
    t = Topic.find_by(id=t_id)
    t.update_topic(form)
    flash('修改话题成功', 'success')
    return redirect(form.get('next'))
Example #8
0
def add():
    form = request.form
    user = current_user()
    reply = Reply.new(form, author_id=user._id)
    t = Topic.find_by(_id=reply.topic_id)
    t.rt = int(time.time())
    t.update()
    return redirect(url_for("topic.detail", topic_id=str(reply.topic_id)))
Example #9
0
def search():
    qs = request.args.get('q')
    print(qs)
    t = Topic.find_by(title=qs)
    if t is not None:
        return render_template("topic/detail.html", topic=t)
    else:
        return redirect(url_for('.index'))
Example #10
0
def profile(username):
    u = User.find_by(username=username)
    if u is None:
        abort(404)
    else:
        ts = Topic.find_all(user_id=u.id, __sort=['created_time', -1], __slice=[0, 5])
        rt_ids = Reply.find_distinct(key='topic_id', page=1, page_size=5, user_id=u.id)
        rts = [Topic.find_by(id=rt_id) for rt_id in rt_ids]
        return render_template('user/profile.html', user=u, ts=ts, rts=rts)
Example #11
0
def replies():
    u_id = int(request.args.get('id'))
    u = User.find_by(id=u_id)
    if u is None:
        abort(404)
    else:
        page = int(request.args.get('page', 1))
        t_ids = Reply.find_distinct(key='topic_id', page=page, user_id=u.id)
        ts = [Topic.find_by(id=t_id) for t_id in t_ids]
        pages = Reply.pages(__distinct='topic_id', user_id=u.id)
        return render_template('user/replies.html', user=u, ts=ts, page=page, pages=pages)
Example #12
0
 def delete(cls, id):
     from .topic import Topic
     # 删除topic.replies_id中对该评论的索引
     filter_by = cls.query.filter_by(id=id)
     reply = filter_by.first()
     topic = Topic.find_by(id=reply.topic_id)
     reply_id_list = topic.get_replies_id()
     reply_id_list.remove(str(reply.id))
     topic.replies_id = '_' + '_'.join(reply_id_list)
     topic.save()
     filter_by.delete()
     db.session.commit()
Example #13
0
def add():
    form = request.form
    u = current_user()
    content = form['content']
    topic_id = form['topic_id']
    # 获取 reply 中 @ 的用戶
    topic_user = Topic.find_by(id=topic_id).user()
    users = users_from_content(content, topic_user)
    # 自己回复自己话题不会收到消息
    if topic_user.id == u.id:
        users.remove(topic_user)
    send_news(u, users, topic_id)
    m = Reply.new(form, user_id=u.id)
    return redirect(url_for('topic.detail', id=m.topic_id))
Example #14
0
def delete():
    u = current_user()
    if u is None:
        abort(403, "Sorry, Don\'t have permission.")
    else:
        # m = Topic.get('user_id')
        id = int(request.args.get('id'))
        token = request.args.get('token')
        ms = Topic.find_by(id=id)
        # 判断 token 是否是我们给的
        if token in csrf_tokens and csrf_tokens[token] == ms.user_id:
            csrf_tokens.pop(token)
            t = Topic.find_by(id=id)
            print('mongo t', t)
            t.delete()
            # 删除评论
            delr = Reply.find_all(topic_id=id)
            print('delr mongo', delr)
            for i in range(len(delr)):
                delr[i].delete()
            return redirect(url_for('.index'))
        else:
            abort(404, "Sorry, not you")
Example #15
0
def profile():
    u = current_user()
    t = Topic.find_all(user_id=u.id)
    t.reverse()
    rs = Reply.find_all(user_id=u.id)
    topic_of_replys = []
    for r in rs:
        top = Topic.find_by(id=r.topic_id)
        topic_of_replys.append(top)
    topic_of_replys.reverse()
    if u is None:
        return redirect(url_for('.index'))
    else:
        return render_template('profile.html',
                               user=u,
                               topics=t,
                               tops=topic_of_replys)
Example #16
0
File: user.py Project: snzhaoch/bbs
def user_replies(username):
    """
    用户参与话题页面
    """
    u = User.find_by(username=username)
    c_u = current_user()
    page = int(request.args.get('page', 1))
    if u is None:
        abort(404)
    else:
        # 全部参与的话题,每页 20 个
        limit = 5
        skip = limit * (page - 1)
        replys_after = Reply.find_lss(
            limit=limit,
            skip=skip,
            sort=-1,
            user_id=u.id,
        )
        replys_topic = []
        topic_ids = []
        for v in replys_after:
            topic_id = v.topic_id
            if topic_id in topic_ids:
                continue
            else:
                t = Topic.find_by(id=v.topic_id)
                replys_topic.append(t)
                topic_ids.append(topic_id)
        # 当前用户的 topic 一共有多少页
        topic_count = len(replys_topic)
        if topic_count % limit == 0:
            pages = topic_count // limit
        else:
            pages = topic_count // limit + 1
        # 5 个最近无人回复的话题
        ts = Topic.topic_noreply()
        return render_template(
            '/user/replies.html',
            user=u,
            current_user=c_u,
            ts=ts,
            topics=replys_topic,
            page=page,
            pages=pages
        )
Example #17
0
def add():
    log('reply add')
    form = request.form
    u = current_user()

    log('before send mail', form)
    content = form.get('content')
    log('reply', content)
    users = users_from_content(content)
    log('reply', users)
    send_mails(u, users, content)
    log('after send mail')
    m = Reply.new(form, user_id=u.id)

    t_id = m.topic_id
    t = Topic.find_by(id=t_id)
    t.update_time = m.created_time
    t.save()
    return redirect(url_for('topic.detail', id=m.topic_id))
Example #18
0
 def get_replied_topics(cls, user):
     from models.topic import Topic
     replies = Reply.find_all(user_id=user.id)
     replies.sort(reverse=True, key=lambda x: x.ct)
     reply_topics = []
     topic_ids = []
     topics = []
     # print('debug--------------------time: ', time.time() - s)
     for r in replies:
         if r.topic_id not in topic_ids:
             print('llllllllllllllll', r.topic_id)
             topic_ids.append(r.topic_id)
             topics.append((r.topic_id, r.ut))
     # print('debug--------------------time: ', time.time() - s)
     for topic_id, ut in topics:
         t = Topic.find_by(id=topic_id)
         t.reply_time = ut
         reply_topics.append(t)
     return reply_topics
Example #19
0
def delete():
    id = int(request.args.get('id'))
    token = request.args.get('token')

    u = current_user()
    # print(id, token, u, csrf_tokens)
    # 判断 token 是否是我们给的
    if token in csrf_tokens and csrf_tokens[token] == u.id:
        csrf_tokens.pop(token)
        if u is not None:
            print('删除topic用户是:', u, '删除的topicid为:', id)
            # 此处应该是 以 实例对象来访问类方法,所以需要先确定对象,不能直接进行类方法 Topic.delete()的请求
            t = Topic.find_by(id=id)
            t.delete()
            # Topic.delete(id)
            return redirect(url_for('.index'))
        else:
            abort(404)
    else:
        abort(404)
Example #20
0
File: user.py Project: snzhaoch/bbs
def user_detail(username):
    """
    用户详情页面
    """
    u = User.find_by(username=username)
    c_u = current_user()
    if u is None:
        abort(404)
    else:
        user_created_time = formatted_time(u.created_time)
        # 5 个最近创建的话题
        topics_after = Topic.find_lss(5, 0, -1, user_id=u.id)
        # 5 个最近参与的话题
        replys_after = Reply.find_lss(5, 0, -1, user_id=u.id)
        replys_topic = []
        topic_ids = []
        for v in replys_after:
            topic_id = v.topic_id
            if topic_id in topic_ids:
                continue
            else:
                t = Topic.find_by(id=v.topic_id)
                # 下面这个 if else 是因为删除 topic 时未删除 对应的 reply,
                # 导致 t 有为 None 的情况,项目完成初始化 mongodb 时可以去掉
                if t is not None:
                    replys_topic.append(t)
                    topic_ids.append(topic_id)
                else:
                    continue
        # 5 个最近无人回复的话题
        ts = Topic.topic_noreply()
        return render_template(
            '/user/profile.html',
            user=u,
            current_user=c_u,
            user_created_time=user_created_time,
            topics=topics_after,
            ts=ts,
            replys_topic=replys_topic
        )
Example #21
0
def delete():

    id = int(request.args.get('id'))
    token = request.args.get('token')
    t = Topic.find_by(id=id)
    u = current_user()
    print(u.role)
    if int(u.role) != 1:  # judge if it is administrator
        # 判断 token 是否是我们给的
        if token in csrf_tokens and csrf_tokens[token] == u.id:
            csrf_tokens.pop(token)
            if u is not None:
                print('删除 topic 用户是', u, id)
                t.delete()
                return redirect(url_for('.index'))
            else:
                abort(404)
        else:
            abort(403)
    else:
        print('haha')
        t.delete()
        return redirect(url_for('.index'))
Example #22
0
 def topic(self):
     """
     查找消息所属的话题
     """
     topic = Topic.find_by(id=self.topic_id)
     return topic
Example #23
0
 def new(cls, form, **kwargs):
     from models.topic import Topic
     r = super(Reply, cls).new(form, **kwargs)
     t = Topic.find_by(id=r.topic_id)
     t.update_replies_id(r.id)
     return r
Example #24
0
 def topic(self):
     t = Topic.find_by(id=self.topic_id)
     return t
Example #25
0
def delete():
    t_id = int(request.args.get('id'))
    t = Topic.find_by(id=t_id)
    t.delete()
    flash('删除话题成功', 'success')
    return redirect(url_for('index.index'))
Example #26
0
 def topic(self):
     from models.topic import Topic
     t = Topic.find_by(id=self.topic_id)
     return t