def channel_topic_list(order_by='hot', channel_id=0, class_id=0, page_index=1): """高级方法,获取频道正文的话题列表,返回字典的数组""" cache = my_db.cache class_id_list = [x['class_id'] for x in get_class_list()] channel_id_list = [x['channel_id'] for x in channel_list()] key = None if not isinstance(page_index, int): raise ValueError("page_index is not a int type") elif isinstance(class_id, int) and class_id in class_id_list: """查询类别""" key = "topic_list_class_{}_order_by_{}".format(class_id, order_by) elif isinstance(channel_id, int) and channel_id in channel_id_list: """查询频道""" key = "topic_list_channel_{}_order_by_{}".format(channel_id, order_by) else: pass if key is None: raise ValueError("channel_id , class_is error") else: result = cache.get(key) if result is None: result = __channel_topic_list(order_by=order_by, channel_id=channel_id, class_id=class_id, page_index=page_index) cache.set(key, result, timeout=60) return result
def index_topic_list(): """获取首页的各频道的话题列表,返回的是是字典 字典以channel_id为key,值是话题字典的数组,排序以优先级+发布时间来排序 这里不包含置顶帖子,置顶帖子会在视图函数中与此结果重新组合。 """ cache = my_db.cache key = "index_topic_dict" index_topic_dict = cache.get(key) if index_topic_dict is None: """从数据库查询""" channel_id_list = [x['channel_id'] for x in channel_list()] current_date = my_db.current_datetime() index_topic_dict = dict() # 结果集容器 columns_str = "channel_id,class_id,top_id,top_title" columns = columns_str.split(",") columns.append('view_count') ses = my_db.sql_session() for channel_id in channel_id_list: """首页查询的结果将不会排除过期的话题/投票""" sql = "select {0},(select count(view_count.view_id) from view_count where " \ "topic_info.top_id=view_count.topic_id) from topic_info where can_show=1 " \ "and channel_id={1} " \ "order by create_date desc limit 0,5".format(columns_str, channel_id) proxy = ses.execute(sql) temp = proxy.fetchall() result = [dict(zip(columns, x)) for x in temp] index_topic_dict[channel_id] = result ses.close() """将字典按照左右两列排序,以帖子被浏览总数降序排列""" pass cache.set(key, index_topic_dict, timeout=60 * 1) return index_topic_dict
def test_channels_list_correct(): assert (channel_list(kelly['token']) == [ { 'channel_id': channel_id_kelly['channel_id'], 'name': 'Kelly_channel' }, ])
def index(): from_ip = get_real_ip(request) """返回首页""" login_flag = is_login(session) # 用户是否已登录 channel_list = channel.channel_list() # 频道列表 channel_dict = {x['channel_id']: x['channel_name'] for x in channel_list} form = SearchLoginForm() # 搜索from img_form = PhotoForm() # 上传图片from index_dict = topic.index_topic_list() # 首页帖子字典 side_bar_list = topic.side_bar_topic_list() # 侧边栏的列表 banner_list = banner_manage.get_banner() # banner列表 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("index.html", login_flag=login_flag, side_bar_list=side_bar_list, user_img_url=user_img_url, user_level=user_level, banner_list=banner_list, channel_list=channel_list, channel_dict=channel_dict, user_nickname=user_nickname, form=form, img_form=img_form, index_dict=index_dict) else: return render_template("index.html", login_flag=login_flag, channel_dict=channel_dict, channel_list=channel_list, index_dict=index_dict, side_bar_list=side_bar_list, form=form, img_form=img_form, banner_list=banner_list)
def index(): """返回首页""" login_flag = is_login(session) # 用户是否已登录 channel_list = channel.channel_list() # 频道列表 form = SearchLoginForm() # 搜索from img_form = PhotoForm() # 上传图片from 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("index.html", login_flag=login_flag, user_img_url=user_img_url, user_level=user_level, channel_list=channel_list, form=form, img_form=img_form) else: return render_template("index.html", login_flag=login_flag, channel_list=channel_list, form=form, img_form=img_form)
def test_channel_leave_valid(): channel_leave(hamish['token'], channel_id_kelly['channel_id']) assert (channel_list(hamish['token']) == { 'channels': [{ 'channel_id': channel_id_hamish['channel_id'], 'name': 'Hamish_channel' }] })
def test_channels_list_multiple(): assert (channel_list(hamish['token']) == [ { 'channel_id': channel_id_hamish['channel_id'], 'name': 'Hamish_channel' }, { 'channel_id': channel_id_kelly['channel_id'], 'name': 'Kelly_channel' }, ])
def __init__(self, vProgress): try: self.Progress = vProgress self.Channels = channel.channel_list() self.Programmes = programme.programme_list() self.CountDay = common.count_day self.FullDesc = common.full_desc self.XMLOut = common.xmltv_patch self.Channels.load_channels_from_settings() except Exception, e: common.dbg_log('parser::__init__', 'ERROR: (' + repr(e) + ')', common.logErorr)
def test_channel_invite_valid(): channel_invite(zach['token'], channel_id_zach['channel_id'], hamish['u_id']) assert (channel_list(hamish['token']) == { 'channels': [{ 'channel_id': channel_id_hamish['channel_id'], 'name': 'Hamish_channel' }, { 'channel_id': channel_id_zach['channel_id'], 'name': 'Zach_channel' }] })
def refresh_channels(): try: common.dbg_log('functions::refresh_channels', 'enter_function') common.set_busy(1) set = settings.settings() set.backup() xmldoc = set.parse() if xmldoc != None: set.remove_old_channels(xmldoc) chnlst = channel.channel_list() chnlst.load_channels_from_net() chnlst.save_channels_to_settings(xmldoc) set.save(xmldoc) common.set_busy(0) common.dbg_log('functions::refresh_channels', 'exit_function') except Exception, e: common.set_busy(0) common.dbg_log('functions::refresh_channels', 'ERROR: (' + repr(e) + ')', common.logErorr)
def c_list(): """ This is a flask wrapper for the channel_list function Parameters: No parameters Returns: (dictionary): This dictionary contains a list of all the channels that the user is part of and their associated details """ token = request.args.get('token') if not token_check(token): raise AccessError(description="Invalid token") return_dict = channel_list(token) return dumps(return_dict)
def my_channel(channel_id): """列表页""" try: channel_id = int(channel_id) except ValueError: channel_id = 1 login_flag = is_login(session) # 用户是否已登录 channel_list = channel.channel_list() # 获取频道列表 channel_name = "" # 当前频道名称 class_list = channel.get_class_dict()[channel_id] # 获取频道所有小类 current_class_id = int(get_arg(request, "class_id", 0)) # 获取当前小类id for x in range(len(channel_list)): temp = channel_list[x] if temp['channel_id'] == channel_id: channel_name = temp['channel_name'] channel_list.pop(x) break 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("channel.html", login_flag=login_flag, user_level=user_level, user_img_url=user_img_url) else: return render_template("channel.html", login_flag=login_flag, channel_list=channel_list, current_channel_name=channel_name, class_list=class_list, current_channel_id=channel_id, current_class_id=current_class_id)
def weixin_vote(): from_ip = get_real_ip(request) """返回首页""" login_flag = is_login(session) # 用户是否已登录 channel_list = channel.channel_list() # 频道列表 channel_dict = {x['channel_id']: x['channel_name'] for x in channel_list} form = SearchLoginForm() # 搜索from img_form = PhotoForm() # 上传图片from 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("weixin_vote.html", login_flag=login_flag, user_img_url=user_img_url, user_level=user_level, channel_list=channel_list, channel_dict=channel_dict, user_nickname=user_nickname, form=form, img_form=img_form) else: return redirect(url_for("index"))
token = request.args.get('token') channel_id = int(request.args.get('channel_id')) print(token) return_dict = channel_details(token, channel_id) print(return_dict) return dumps(return_dict) #return 1 @APP.route("/channel/list", methods=["GET"]) def c_list(): <<<<<<< HEAD ======= token = request.args.get('token') return_dict = channel_list(token) print(return_dict) return dumps(return_dict) #return 1 @APP.route("/channels/list_all", methods=["GET"]) def c_listall(): token = request.args.get('token') return_dict = channels_list_all(token) print(return_dict) return dumps(return_dict) #return 1 @APP.route("/channel/messages", methods=["GET"]) def c_messages():
def channels_list(): token = request.args.get('token') c_list = channel.channel_list(token) return dumps(c_list)
def test_channels_list_zero(): assert (channel_list(dummy['token']) == {'channels': []})
def my_channel(channel_id): """列表页""" form = SearchLoginForm() # 搜索from try: channel_id = int(channel_id) except ValueError: channel_id = 1 login_flag = is_login(session) # 用户是否已登录 channel_list = channel.channel_list() # 获取频道列表 channel_name = "" # 当前频道名称 class_list = channel.get_class_dict()[channel_id] # 获取频道所有小类 current_class_id = int(get_arg(request, "class_id", 0)) # 获取当前小类id for x in range(len(channel_list)): temp = channel_list[x] if temp['channel_id'] == channel_id: """获取频道名""" channel_name = temp['channel_name'] break """获取频道的话题列表""" topic_list = topic.channel_topic_list(channel_id=channel_id, class_id=current_class_id) """计算争议度""" for x in topic_list: val = x.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 x['bomb_count'] = bomb_count 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("channel.html", login_flag=login_flag, channel_list=channel_list, current_channel_name=channel_name, class_list=class_list, current_channel_id=channel_id, current_class_id=current_class_id, topic_list=topic_list, user_level=user_level, user_img_url=user_img_url, form=form) else: return render_template("channel.html", login_flag=login_flag, channel_list=channel_list, current_channel_name=channel_name, class_list=class_list, current_channel_id=channel_id, current_class_id=current_class_id, topic_list=topic_list, form=form)
def admin_center(key): """后台管理页""" if key == "user": """用户管理""" user_count = user.user_count() current_index = int(get_arg(request, "index", 1)) # 取页码 page_length = int(get_arg(request, "page_length", 20)) # 每页多少记录 max_index = math.ceil(user_count / page_length) # 最大页码 if max_index < current_index: current_index = max_index if 1 > current_index: current_index = 1 """每页显示5个可点击页码""" range_min = current_index - 2 if current_index > 2 else 1 rang_max = max_index if (range_min + 4) > max_index else (range_min + 4) index_range = [x for x in range(range_min, rang_max + 1)] user_data = user.page(current_index, page_length)['data'] return render_template("admin_center_user.html", user_count=user_count, index_range=index_range, max_index=max_index, current_index=current_index, prev_index=current_index if (current_index - 1) > 1 else 1, next_index=current_index + 1 if (current_index + 1) < max_index else max_index, user_data=user_data) elif key == "channel": """频道管理""" channel_list = channel.channel_list(1) small_class_dict = channel.get_class_dict(1) return render_template("admin_center_channel.html", channel_list=channel_list, small_class_dict=small_class_dict) elif key == "topic": """话题管理""" form = SearchLoginForm() topic_count = topic.topic_count() current_index = int(get_arg(request, "index", 1)) # 取页码 page_length = int(get_arg(request, "page_length", 5)) # 每页多少记录 max_index = math.ceil(topic_count / page_length) # 最大页码 if max_index < current_index: current_index = max_index if 1 > current_index: current_index = 1 """每页显示5个可点击页码""" range_min = current_index - 2 if current_index > 2 else 1 rang_max = max_index if (range_min + 4) > max_index else (range_min + 4) index_range = [x for x in range(range_min, rang_max + 1)] topic_data = topic.manage_topic_admin(the_type="page", index=current_index, page_length=page_length) topic_data = topic_data['data'] channel_list = channel.channel_list() return render_template("admin_center_topic.html", channel_list=channel_list, topic_count=topic_count, index_range=index_range, max_index=max_index, current_index=current_index, prev_index=current_index if (current_index - 1) > 1 else 1, next_index=current_index + 1 if (current_index + 1) < max_index else max_index, topic_data=topic_data, form=form)