def search(): """ 返回搜索的内容或用户 """ c_u = current_user() content = request.args.get('q') limit = 5 topics = Topic.find_lss( limit=limit, sort=-1, title={'$regex': '{}'.format(content)} ) users = User.find_lss( limit=limit, sort=-1, username={'$regex': '{}'.format(content)} ) ts = Topic.topic_noreply() return render_template( '/search/search.html', current_user=c_u, content=content, topics=topics, users=users, ts=ts )
def search_topics(): """ 返回搜索的全部内容 """ c_u = current_user() page = int(request.args.get('page', 1)) content = request.args.get('content', '') limit = 20 skip = limit * (page - 1) topics = Topic.find_lss( limit=limit, skip=skip, sort=-1, title={'$regex': '{}'.format(content)} ) topic_count = Topic.count(title={'$regex': '{}'.format(content)}) if topic_count % limit == 0: pages = topic_count // limit else: pages = topic_count // limit + 1 # 5 个最近无人回复的话题 ts = Topic.topic_noreply() return render_template( '/search/topics.html', current_user=c_u, content=content, topics=topics, ts=ts, page=page, pages=pages )
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 )
def user_topics(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) topics = Topic.find_lss( limit=limit, skip=skip, sort=-1, user_id=u.id ) # 当前用户的 topic 一共有多少页 topic_count = Topic.count(user_id=u.id) if topic_count % limit == 0: pages = topic_count // limit else: pages = topic_count // limit + 1 # 5 个最近无人回复的话题 ts = Topic.topic_noreply() return render_template( '/user/topics.html', user=u, current_user=c_u, ts=ts, topics=topics, page=page, pages=pages, )
def index(): """ 论坛主页 """ c_u = current_user() page = int(request.args.get('page', 1)) if page is None: page = 1 else: page = int(page) limit = 20 skip = limit * (page - 1) board_id = request.args.get('board_id', '-1') # 首页不展示测试板块内容 if board_id == '-1': tm = Topic.find_lss( limit=limit, skip=skip, sort=('lastreply_time', -1), top=True, test=False, ) ms = Topic.find_lss(limit=limit, skip=skip, sort=('lastreply_time', -1), top=False, test=False) topic_count = Topic.count() else: # 每个测试板块置顶消息独立 board = Board.find(board_id) if board.test is False: tm = Topic.find_lss(limit=limit, skip=skip, sort=('lastreply_time', -1), top=True, test=False) else: tm = Topic.find_lss(limit=limit, skip=skip, sort=('lastreply_time', -1), board_id=board_id, top=True, test=True) ms = Topic.find_lss(limit=limit, skip=skip, sort=('lastreply_time', -1), board_id=board_id, top=False) topic_count = Topic.count(board_id=board_id) if topic_count % limit == 0: pages = topic_count // limit else: pages = topic_count // limit + 1 ts = Topic.topic_noreply() bs = Board.all() return render_template( "topic/index.html", current_user=c_u, tm=tm, ms=ms, ts=ts, bs=bs, bid=board_id, page=page, pages=pages, )