def fetch_joined_topics(user_id): """根据用户id获取参加过的话题""" session = my_db.sql_session() columns = get_columns() sql = "select * from topic_info where top_id in " \ "(select topic_id from vote_count where user_id = {})" \ .format(user_id) data = [] try: proxy_result = session.execute(sql) result = proxy_result.fetchall() if len(result) != 0: result = [my_db.str_format(x) for x in result] data = [dict(zip(columns, x)) for x in result] for x in data: x.update({ "view_count": get_view_count(x['top_id']), "vote_count": sum_vote_count(x['top_id']) }) else: data = [] except Exception as e: print(e) finally: session.close() return data
def view_topic(key): """管理员浏览话题的详细页""" form = RequestLoginForm() result = topic.manage_topic_admin(top_id=key, the_type="single") topic_info = result['data'] surplus = surplus_datetime(topic_info['end_date']) # 剩余时间 all_view_count = vote_tools.get_view_count(topic_id=key) # 浏览总数 query_vote = vote_tools.get_vote_count(key) # 查询 投票人数 support_a = query_vote['support_a'] support_b = query_vote['support_b'] join_count = support_b + support_a # 投票总人数 if support_a == 0 and join_count == 0: blue_width = 50 else: blue_width = int((support_a / join_count) * 1000) / 10 red_width = 100 - blue_width """计算争议度""" val = topic_info.pop("a_vs_b") val_list = val.decode(encoding='utf8').split(" vs ") if len(val_list) != 2: """防止新帖子查询到的值是空字符的问题""" val_a = 0 val_b = 0 else: val_a = int(val_list[0]) val_b = int(val_list[1]) temp_per = 0 if val_a + val_b == 0 else ( val_a if val_a < val_b else val_b) / (val_a + val_b) if 0.4 <= temp_per <= 0.5: bomb_count = 3 elif 0.3 < temp_per < 0.4: bomb_count = 2 elif temp_per <= 0.3: bomb_count = 1 else: bomb_count = 0 topic_info['bomb_count'] = bomb_count return render_template("detail.html", topic_info=topic_info, surplus=surplus, join_count=join_count, blue_width=blue_width, red_width=red_width, all_view_count=all_view_count, form=form)
def __channel_topic_list(order_by='hot', class_id=0, channel_id=0, page_index=1): """获取个频道最新的话题列表.order_by代表排序方式,hot,以热度,argue 争议性, page_index 代表第几页,一页12条消息,默认是第一页 create_date 发布时间,三种排序方式,目前仅可以实现发布时间倒叙排列 channel——id 是频道信号。 class——id 是类别信号, 如果class_id=0表示是频道页获取信息,反之,则是点击了频道页下面的类别信息的情况。 查询结果不排除过期的话题。 """ every_page = 12 # 每页12条记录 ses = my_db.sql_session() key_str = "top_id,top_title,viewpoint_a,viewpoint_b,img_url_a,img_url_b" """生成sql语句""" child_sql = "(SELECT CONCAT_WS(' vs ',SUM(support_a),SUM(support_b)) " \ "FROM vote_count WHERE vote_count.topic_id=topic_info.top_id)" # 查询ab支持度的子查询 if channel_id == 0 and class_id == 0: raise ValueError("频道id和列别id不能为空") elif channel_id != 0 and class_id == 0: """class—id为0表示是频道页查询,返回此频道下最新的话题""" sql = "select {},{} from topic_info where can_show=1 and channel_id={} order by create_date desc limit {},{}". \ format(key_str, child_sql, channel_id, (page_index - 1) * every_page, every_page) elif class_id != 0: """class—id和channel_id都不为零表示是频道页点击类别的链接时查询""" sql = "select {},{} from topic_info where can_show=1 and class_id={} order by " \ "create_date desc limit {},{}".format(key_str, child_sql, class_id, (page_index - 1) * every_page, every_page) proxy = ses.execute(sql) result = proxy.fetchall() ses.close() columns = key_str.split(",") columns.append("a_vs_b") result = [dict(zip(columns, x)) for x in result] for x in result: x.update({ "view_count": get_view_count(x['top_id']), "vote_count": sum_vote_count(x['top_id']) }) return result
def view_topic(key): """管理员浏览话题的详细页""" form = RequestLoginForm() result = topic.manage_topic_admin(top_id=key, the_type="single") topic_info = result['data'] surplus = surplus_datetime(topic_info['end_date']) # 剩余时间 all_view_count = vote_tools.get_view_count(topic_id=key) # 浏览总数 query_vote = vote_tools.get_vote_count(key) # 查询 投票人数 support_a = query_vote['support_a'] support_b = query_vote['support_b'] join_count = support_b + support_a # 投票总人数 if support_a == 0 or join_count == 0: blue_width = 50 else: blue_width = int((support_a / join_count) * 1000) / 10 red_width = 100 - blue_width return render_template("detail.html", topic_info=topic_info, surplus=surplus, join_count=join_count, blue_width=blue_width, red_width=red_width, all_view_count=all_view_count, form=form)
def my_detail(key): """投票详细页""" form = RequestLoginForm() result = topic.topic_detail_user(top_id=key) if len(result['data']) == 0: abort(404) else: topic_info = result['data'] # 投票信息 comment_list = comment.manage_comment(the_type="by_topic_id", topic_id=key) # 评论 surplus = surplus_datetime(topic_info['end_date']) # 剩余时间 up_a_list = list() up_b_list = list() all_view_count = vote_tools.get_view_count(topic_id=key) # 浏览总数 query_vote = vote_tools.get_vote_count(key) # 查询 投票人数 support_a = query_vote['support_a'] support_b = query_vote['support_b'] join_count = support_b + support_a # 投票总人数 side_bar_list = topic.side_bar_topic_list() # 侧边栏的列表 if support_a == 0 and join_count == 0: blue_width = 50 else: blue_width = int((support_a / join_count) * 1000) / 10 red_width = int(1000 - blue_width * 10) / 10 """计算争议度""" val = topic_info.pop("a_vs_b") val_list = val.decode(encoding='utf8').split(" vs ") if len(val_list) != 2: """防止新帖子查询到的值是空字符的问题""" val_a = 0 val_b = 0 else: val_a = int(val_list[0]) val_b = int(val_list[1]) temp_per = 0 if val_a + val_b == 0 else ( val_a if val_a < val_b else val_b) / (val_a + val_b) if 0.4 <= temp_per <= 0.5: bomb_count = 3 elif 0.3 < temp_per < 0.4: bomb_count = 2 elif temp_per <= 0.3: bomb_count = 1 else: bomb_count = 0 topic_info['bomb_count'] = bomb_count login_flag = is_login(session) # 用户是否已登录 if login_flag: try: user_img_url = session['user_img_url'] except KeyError: user_img_url = "" user_img_url = '../static/image/guest.png' if user_img_url == "" else session[ 'user_img_url'] user_level = 1 # 暂时替代 return render_template("detail.html", topic_info=topic_info, surplus=surplus, join_count=join_count, blue_width=blue_width, red_width=red_width, all_view_count=all_view_count, form=form, login_flag=login_flag, user_img_url=user_img_url, user_level=user_level, side_bar_list=side_bar_list) else: return render_template("detail.html", topic_info=topic_info, surplus=surplus, join_count=join_count, blue_width=blue_width, red_width=red_width, all_view_count=all_view_count, form=form, login_flag=login_flag, side_bar_list=side_bar_list)
def my_detail(key): """投票详细页""" form = RequestLoginForm() result = topic.topic_detail_user(top_id=key) if len(result['data']) == 0: abort(404) else: topic_info = result['data'] # 投票信息 a_count, b_count = comment.comment_count(key) # 此话题全部评论 comment_list = comment.manage_comment(the_type="by_topic_id", topic_id=key) # 评论,只有直接对帖子的评论 surplus = surplus_datetime(topic_info['end_date']) # 剩余时间 comment_list = comment_list['data'] """先区分主回复和子回复""" parent_list = list() children_list = list() for x in comment_list: if x['parent_comment'] == 0: x['children'] = [] parent_list.append(x) else: children_list.append(x) """把子回复插入道父回复之中""" parent_dict = {x['comment_id']: x for x in parent_list} for x in children_list: parent_comment_id = x['parent_comment'] if parent_comment_id in parent_dict.keys(): parent_dict[parent_comment_id]['children'].append(x) else: raise KeyError("无主的子评论:{}".format(str(x))) parent_list = list(parent_dict.values()) up_a_list = list() # 支持a的评论 up_b_list = list() # 支持a的评论 """先取出直接对帖子进行的评论分类""" for x in parent_list: if x['support_side'] == "a": up_a_list.append(x) else: up_b_list.append(x) all_view_count = vote_tools.get_view_count(topic_id=key) # 浏览总数 query_vote = vote_tools.get_vote_count(key) # 查询 投票人数 support_a = query_vote['support_a'] support_b = query_vote['support_b'] join_count = support_b + support_a # 投票总人数 side_bar_list = topic.side_bar_topic_list() # 侧边栏的列表 if support_a == 0 and join_count == 0: blue_width = 50 else: blue_width = int((support_a / join_count) * 1000) / 10 red_width = int(1000 - blue_width * 10) / 10 """计算争议度""" val = topic_info.pop("a_vs_b") val_list = val.split(" vs ") if len(val_list) != 2: """防止新帖子查询到的值是空字符的问题""" val_a = 0 val_b = 0 else: val_a = int(val_list[0]) val_b = int(val_list[1]) temp_per = 0 if val_a + val_b == 0 else (val_a if val_a < val_b else val_b) / (val_a + val_b) if 0.4 <= temp_per <= 0.5: bomb_count = 3 elif 0.3 < temp_per < 0.4: bomb_count = 2 elif temp_per <= 0.3: bomb_count = 1 else: bomb_count = 0 topic_info['bomb_count'] = bomb_count login_flag = is_login(session) # 用户是否已登录 if login_flag: try: user_img_url = session['user_img_url'] except KeyError: user_img_url = "" user_img_url = '../static/image/guest.png' if user_img_url == "" else session['user_img_url'] user_level = 1 # 暂时替代 user_nickname = session['user_nickname'] return render_template("detail.html", topic_info=topic_info, surplus=surplus, join_count=join_count, blue_width=blue_width, red_width=red_width, all_view_count=all_view_count, form=form, login_flag=login_flag, user_img_url=user_img_url, user_level=user_level, side_bar_list=side_bar_list, up_a_list=up_a_list, up_b_list=up_b_list, a_count=a_count, b_count=b_count, user_nickname=user_nickname) else: return render_template("detail.html", topic_info=topic_info, surplus=surplus, join_count=join_count, blue_width=blue_width, red_width=red_width, all_view_count=all_view_count, form=form, login_flag=login_flag, side_bar_list=side_bar_list, up_a_list=up_a_list, up_b_list=up_b_list, a_count=a_count, b_count=b_count)