def index(): u = current_user() if u is None: return redirect(url_for('index.index')) board_id = request.args.get('board_id', 'all') if board_id == 'all': ms = Topic.all() else: ms = Topic.all(board_id=board_id) # if r.exists(board_id): # #log('hit redis') # json_string = r.get(board_id) # json_list = json.loads(json_string) # else: # print('not hit redis') # json_list = [] # for m in ms: # j = m.json() # j['replies'] = m.replies() # j['user_image'] = m.user().user_image # json_list.append(j) # json_string = json.dumps(json_list) # r.set(board_id, json_string) # ms = json_list token = new_csrf_token() bs = Board.all() return render_template("topic/index.html", ms=ms, token=token, bs=bs, bid=board_id, user=u)
def index(): board_id = int(request.args.get('board_id', -1)) if board_id == -1: ms = Topic.all() else: ms = Topic.all(board_id=board_id) bs = Board.all() return render_template("index.html", ms=ms, bs=bs, bid=board_id)
def index(): board_id = int(request.args.get('board_id', -1)) if board_id == -1: ms = Topic.all() else: ms = Topic.all(board_id=board_id) token = new_csrf_token() bs = Board.all() return render_template("topic/index.html", ms=ms, token=token, bs=bs, bid=board_id)
def index_for_guest(): # 访客模式所用 board_id = int(request.args.get('board_id', -1)) if board_id == -1: ms = Topic.all() else: ms = Topic.all(board_id=board_id) bs = Board.all() u = current_user() return render_template("index_for_guest.html", ms=ms, bs=bs, bid=board_id, user=u)
def index(): u = current_user() board_id = int(request.args.get('board_id', -1)) if board_id == -1: ms = Topic.all() else: ms = Topic.all(board_id=board_id) ms.sort(key=lambda ms: ms.created_time, reverse=True) token = new_csrf_token() bs = Board.all() return render_template("index.html", user=u, ms=ms, token=token, bs=bs, bid=board_id)
def index(): # todo:add csrfCheck board_id = int(request.args.get('board_id', -1)) if board_id == -1: ms = Topic.all() else: ms = Topic.all(board_id=board_id) token = new_csrf_token() bs = Board.all() u = current_user() return render_template("topic/index.html", u=u, ms=ms, bs=bs, bid=board_id, token=token)
def index(): u = current_user() board_id = int(request.args.get('board_id', -1)) if board_id == -1: ms = Topic.all() else: ms = Topic.all(board_id=board_id) bs = Board.all() log('login required index route', u, ms) return render_template("topic/index.html", u=u, ms=ms, bs=bs, bid=board_id)
def index(): u = current_user() username = u.username image = u.image board_id = int(request.args.get('board_id', -1)) if board_id == -1: ms = Topic.all() else: ms = Topic.all(board_id=board_id) token = new_csrf_token() bs = Board.all() return render_template("topic/index.html", ms=ms, token=token, bs=bs, bid=board_id, username=username, image=image)
def index(): board_id = int(request.args.get('board_id', -1)) if board_id == -1: ms = Topic.all() else: ms = Topic.all(board_id=board_id) # ms.reverse() 的返回值是 none ms.reverse() token = new_csrf_token() bs = Board.all() u = current_user() return render_template("topic/index.html", user=u, ms=ms, token=token, bs=bs, bid=board_id)
def index(): board_id = request.args.get('board_id', 'all') if board_id == 'all': ms = Topic.all() else: ms = Topic.all(board_id=board_id) bs = Board.all() u = current_user() if not u: return render_template("index.html", ms=ms, bs=bs, bid=board_id) else: return redirect(url_for('topic.index'))
def index(): board_id = int(request.args.get('board_id', -1)) if board_id == -1: ms = Topic.all() else: ms = Topic.all(board_id=board_id) u = current_user() if u is None: return redirect(url_for('index.index')) else: token = new_csrf_token() bs = Board.all() return render_template("topic/index.html", user=u, ms=ms, token=token, bs=bs, bid=board_id)
def index(): u = current_user() board_id = int(request.args.get('board_id', -1)) if board_id == -1: ms = Topic.all() else: ms = Topic.all(board_id=board_id) boards = Board.all() return render_template("topic/index.html", ms=ms, user=u, boards=boards, bid=board_id)
def index(): user = current_user() board_id = int(request.args.get('board_id', -1)) if board_id == -1 or board_id == 1: ms = Topic.all() else: ms = Topic.all(board_id=board_id) ms = active_sort(ms) bs = Board.all() return render_template("topic/index.html", ms=ms, bs=bs, bid=board_id, login_user=user)
def index(u): boards = Board.all() if request.args.get('board_id') is None: topics = Topic.all() else: topics = Topic.all(board_id=request.args['board_id']) Csrf.generate_csrf() csrf_token = Csrf.get_csrf() log('====csrf_token====\n{}'.format(csrf_token)) return render_template("topic/index.html", topics=topics, boards=boards, user=u, csrf_token=csrf_token)
def index(): u = current_user() board_id = int(request.args.get('board_id', -1)) if board_id == -1: ms = Topic.all() else: ms = Topic.all(board_id=board_id) token = new_csrf_token() bs = Board.all() return render_template("homepage.html", ms=ms, token=token, bs=bs, bid=board_id, user=u)
def index(): board_id = int(request.args.get('board_id', -1)) if board_id == -1: ms = Topic.all() else: ms = Topic.all(board_id=board_id) ms.sort(key=lambda x: x.created_time, reverse=True) bs = Board.all() u = current_user() return render_template("topic/index.html", user=u, ms=ms, bs=bs, bid=board_id)
def index(): u = current_user() print('minn---u', User.all(), u) board_id = int(request.args.get('board_id', -1)) if board_id == -1: ms = Topic.all() else: ms = Topic.all(board_id=board_id) # token = new_csrf_token() bs = Board.all() return render_template("topic/index.html", ms=ms, user=u, bs=bs, bid=board_id)
def user_detail(id): u = User.one(id=id) if u is None: abort(404) else: k = 'topics_{}'.format(id) if cache.exists(k): v = cache.get(k) topics = json.loads(v) print("正在使用缓存") else: topics: list = Topic.all(user_id=u.id) topics.sort(key=lambda i: i.created_time, reverse=True) replys: list = Reply.all(user_id=u.id) print('<user_detail replys {}'.format(replys)) re_topics = [] for reply in replys: topic = Topic.one(id=reply.topic_id) if topic not in re_topics: re_topics.append(topic) print('<user_detail re_topics {}'.format(re_topics)) v = json.dumps([t.json() for t in topics]) cache.set(k, v) return render_template('user/profile.html', user=u, topics=topics, re_topics=[])
def profile(username=None): if username: u = User.one(username=username) if u is None: return abort(404) else: u = current_user() if u is None: return redirect(url_for('.login')) create_topic = Topic.all(user_id=u.id) create_topic = sorted(create_topic, key=lambda k: k.created_time, reverse=True) reply = Reply.all(user_id=u.id) reply_topic = [] for r in reply[::-1]: topic = Topic.one(id=r.topic_id) reply_topic.append(topic) return render_template( 'profile.html', user=u, created=create_topic, replied=reply_topic, )
def index(): form = request.args board_id = form.get('board_id', -1) board_id = int(board_id) if board_id == -1: # 访问主页,显示所有的ts ts = Topic.all() else: # 返回版块下的ts,无论board_id是否有效都能处理 ts = Topic.all(board_id=board_id) u = current_user() bs = Board.all() # 传入board_id是为了让点击的版块呈现选中的效果 return render_template("index.html", ts=ts, u=u, bs=bs, board_id=board_id)
def data_form_user(u): ms = Topic.all(user_id=u.id) rs = Reply.all(user_id=u.id) ms = reverse(ms) ts = reply_filter(rs) ts = reverse(ts) return ms, ts
def profile(): u = current_user() ms = Topic.all() ms_d = {} for c in ms: ms_d[str(c.id)] = c.created_time ms_d = sorted(ms_d.items(), key=itemgetter(1), reverse=True) ms_created = [] for c in ms_d: t = Topic.one(id=int(c[0]), user_id=u.id) if t is None: continue else: ms_created.append(t) replies = Reply.all(user_id=u.id) rs_d = {} for r in replies: rs_d[str(r.topic_id)] = r.created_time rs_d = sorted(rs_d.items(), key=itemgetter(1), reverse=True) ms_recented = [] for r in rs_d: print('types', type(r)) ms_recented.append(Topic.one(id=int(r[0]))) return render_template( "profile.html", ms_created=ms_created, ms_recented=ms_recented, user=u, )
def delete_model(self, model): ts = Topic.all(user_id=model.id) for t in ts: Topic.delete(t.id) Reply.delete(user_id=model.id) User.delete(id=model.id) return True
def cached_created_topics(user_id): """ 根据user id 返回该用户创建的Topic对象。 如果缓存命中,则从缓存中拉取包含多个Topic id的列表,根据每个Topic id调用cached_topic_id2topic得到Topic对象。 如果缓存穿透,则从数据库查询,拿到多个Topic对象,将所有Topic id 序列化后存储到redis,返回多个Topic对象。 """ key = 'user_id_{}.created_topics'.format(user_id) try: # 缓存命中 topic_ids_json = data_cache[key] except KeyError: # 缓存未命中,数据库中拉取数据 topics_models = list(Topic.all(user_id=user_id)) topics_models.sort(key=lambda x: x.last_active_time, reverse=True) # 加到redis缓存中的是topic id data_cache.set(key, json.dumps([t.id for t in topics_models]), 3600) log('缓存丢失,向数据库拉取数据,重建缓存') return topics_models else: # 从缓存中拿到多个Topic id,每个Topic id使用 cached_topic_id2topic获得Topic 对象并返回。 topics_models = [ cached_topic_id2topic(topic_id) for topic_id in json.loads(topic_ids_json) ] topics_models.sort(key=lambda x: x.last_active_time, reverse=True) log('缓存命中,直接使用') return topics_models
def index(): board_id = request.args.get('board_id', 'all') if board_id == 'all': ms = Topic.all() else: ms = Topic.all(board_id=board_id) token = new_csrf_token() bs = Board.all() u = current_user() return render_template("topic/index.html", ms=ms, token=token, bs=bs, bid=board_id, user=u)
def created_topics(user_id): ts = Topic.all(user_id=user_id) ts_time = [t.created_time for t in ts] ts_time.sort(reverse=True) ts1 = [] for t in ts_time: ts1.append(Topic.one(created_time=t)) return ts1
def topic_all(): topics = Topic.all() ts = [] for t in topics: dictret = dict(t.__dict__) dictret.pop('_sa_instance_state', None) ts.append(dictret) return jsonify(ts)
def search(): all_topics = Topic.all() ret = [] query = request.args.get("query").lower() for i in all_topics: if query in i.title.lower(): ret.append(i) return render_template("BBS/search.html", ms=ret)
def index(): board_id = int(request.args.get('board_id', -1)) if board_id == -1: ms = Topic.all() top10_ms = Topic.all_order_by_view()[:10] else: ms = Topic.all(board_id=board_id) top10_ms = Topic.all_order_by_view(board_id=board_id)[:10] token = new_csrf_token() bs = Board.all() return render_template("topic/index.html", ms=ms, token=token, bs=bs, bid=board_id, user=current_user(), top10_ms=top10_ms)
def init_heat(): ms = ts.all() for m in ms: reply_count = len(m.replies()) favorite_count = len(m.favorites()) like_count = len(m.likes()) m.heat = reply_count * 5 + favorite_count * 3 + like_count * 2 ts.update(m.id, heat=m.heat)
def index(): # board_id = 2 board_id = int(request.args.get('board_id', -1)) if board_id == -1: ms = Topic.all() else: ms = Topic.find_all(board_id=board_id) token = str(uuid.uuid4()) u = current_user() csrf_tokens['token'] = u.id bs = Board.all() return render_template("topic/index.html", ms=ms, token=token, bs=bs, bid=board_id, user=u)