def edit_form(self, obj=None): obj['categories'] = CategoryGame.game_category_ids(obj['_id']) form = super(GameAdmin, self).edit_form(obj) cids = Category.all_category_ids() categories = Category.get_list(cids) form.categories.choices = [(c._id, c.name) for c in categories] return form
def migu_home(): """获取首页信息 (GET) :uri: /migu/home/ :returns: {'popular': list, 'whats_new': list, 'hottest_of_today': list, 'recommend': list, 'tags': list} """ ret = dict() ret['popular'] = [] ret['whats_new'] = [] ret['hottest_of_today'] = [] ret['recommend'] = [] tags = [] cids = Category.all_category_ids() categories = Category.get_list(cids) for category in categories: gids = CategoryGame.category_game_ids(str(category._id)) games = [g.format() for g in Game.get_list(gids)] tags.append( dict(games=games, tag_id=str(category._id), name=category.name)) ret['tags'] = tags return ret
def category_games(cid): """获取分类下的所有游戏 (GET) :uri: /tags/<string:cid>/games :uri migu: /migu/tags/<string:cid>/games/ :returns: {'games': list} """ gids = CategoryGame.category_game_ids(cid) games = [ g.format(exclude_fields=['subscribed']) for g in Game.get_list(gids) ] return {'games': games}
def update_model(self, form, model): new_cate_ids = form.categories.data ret = super(GameAdmin, self).update_model(form, model) if ret: game = self.get_pk_value(model) old_cate_ids = CategoryGame.game_category_ids(str(game)) need_delete_ids = [ cid for cid in old_cate_ids if cid not in new_cate_ids ] need_create_ids = [ cid for cid in new_cate_ids if cid not in old_cate_ids ] for cate_id in need_create_ids: cg = CategoryGame.init() cg.game = game cg.category = cate_id cg.order = 0 cg.create_model() for cate_id in need_delete_ids: cg = CategoryGame.get_by_ship(str(game), str(cate_id)) cg.delete_model() if cg else None return ret
def channel_game(): """获取频道内容 (GET) :uri: /channels/games :returns: [{'tag_id': string, 'name': string, 'games': list}, ...] """ ret = list() cids = Category.all_category_ids() categories = Category.get_list(cids) for category in categories: gids = CategoryGame.category_game_ids(str(category._id)) games = [ g.format(exclude_fields=['subscribed']) for g in Game.get_list(gids) ] ret.append( dict(games=games, tag_id=str(category._id), name=category.name, icon_type=category.icon_type or 'icon')) return ret
def tags_videos(cid): """获取标签下所有视频 (GET) :uri: /tags/<string:tid>/videos :uri migu: /migu/tags/<string:tid>/videos/ :param maxs: 最后时间, 0代表当前时间, 无此参数按page来分页 :param page: 页码(数据可能有重复, 建议按照maxs分页) :param nbr: 每页数量 :returns: {'videos': list, 'end_page': bool, 'maxs': timestamp} """ params = request.values maxs = params.get('maxs', None) maxs = time.time() if maxs is not None and int( float(maxs)) == 0 else maxs and float(maxs) page = int(params.get('page', 1)) pagesize = int(params.get('nbr', 10)) gids = CategoryGame.category_game_ids(cid) gids = [ObjectId(_gid) for _gid in gids] videos = list() vids = list() while len(videos) < pagesize: vids = Video.games_video_ids(gids, page, pagesize, maxs) ex_fields = [ 'is_favored', 'is_liked', 'author__is_followed', 'game__subscribed' ] videos.extend( [v.format(exclude_fields=ex_fields) for v in Video.get_list(vids)]) # 如果按照maxs分页, 不足pagesize个记录则继续查询 if maxs is not None: obj = Video.get_one(vids[-1], check_online=False) if vids else None maxs = obj.create_at if obj else 1000 if len(vids) < pagesize: break else: break return {'videos': videos, 'end_page': len(vids) != pagesize, 'maxs': maxs}
def create_model(self, form): new_cate_ids = form.categories.data model = super(GameAdmin, self).create_model(form) game = self.get_pk_value(model) for cate_id in new_cate_ids: cg = CategoryGame.init() cg.game = game cg.category = cate_id cg.order = 0 cg.create_model() # 正式环境创建游戏时, 给stage环境也创建一个 import os env = os.environ.get('WXENV') if env == 'Production': from pymongo import MongoClient from config import Stage client = MongoClient(Stage.MONGO_HOST, Stage.MONGO_PORT) stage_db = client[Stage.MONGO_DBNAME] pk = self.get_pk_value(model) obj = self.Model.get_one(pk, check_online=False) obj.status = const.ONLINE stage_db.games.insert_one(obj) return model
def apply(self, query, value): value = apply(self.field_func, [value]) gids = CategoryGame._load_category_game_ids(value) gidobj = [ObjectId(g) for g in gids] query.append({self.column: {'$in': gidobj}}) return query