def auth_user(self, username, passwd): """ 用户名、口令认证用户 @username: 用户名 @passwd: 口令 """ ret = Result() if username != self._get_user(): ret.success = False ret.msg = u'用户不存在' return ret if passwd != self._get_pwd(): ret.success = False ret.msg = u'口令不正确' return ret
def update_game(self, game): """ 编辑游戏 """ ret = Result() try: if not game.id: game.save() else: game.save_most() game.tags.clear() map(game.tags.add, [ item.strip() for item in game.tag_ids.split(',') if item.strip() ]) game.screens.clear() map(game.screens.add, [ item.strip() for item in game.screen_ids.split(',') if item.strip() ]) self._update_category(game) except Exception, e: log_debug.error(u"更新游戏<%s>,数据库操作失败,%s" % (game.name_ch, e)) ret.success = False ret.msg = u"数据库操作失败"
def delete_game(self, id): """ 删除游戏 """ ret = Result() try: game = Game.objects.get(id=id) if game.icon: game.icon.delete() if game.rec_screen: game.rec_screen.delete() if game.flash: game.flash.delete() if game.ipa: game.ipa.delete() if game.apk: game.apk.delete() if game.apk_pack: game.apk_pack.delete() game.screens.all().delete() game.delete() except Exception, e: log_debug.error(u"删除游戏<id:%s>,数据库操作失败,%s" % (id, e)) ret.success = False ret.msg = u"数据库操作失败"
def delete_file(self, db_file): ret = Result() try: self.up.delete(self.parse_file_path(db_file)) except Exception, e: log_debug.error(u"upyun删除文件<%s>失败,%s" % (db_file.name, e)) ret.success = False ret.exception = e ret.msg = e.message
def delete_tag(self, id): """ 删除游戏系列 """ ret = Result() try: tag = Tag.objects.get(id=id) count = tag.game_tag_map.count() if count != 0: ret.success = False ret.msg = u"还有%s款游戏使用该标签,不能删除该标签" % count return ret tag.delete() except Exception, e: log_debug.error(u"删除游戏标签<id:%s>,数据库操作失败,%s" % (id, e)) ret.success = False ret.msg = u"数据库操作失败"
def upload_file(self, upload_file): """ 上传文件 """ ret = Result() upload_ret = store.save_file(upload_file) if not upload_ret.success: ret.success = False ret.msg = u"上传云端失败" else: try: #保存到数据库 upload_file.save() except Exception, e: log_debug.error(u"上传文件,数据库保存失败,%s" % e) ret.success = False ret.msg = u"数据库操作失败" #删除文件 store.delete_file(upload_file)
def save_file(self, db_file): ret = Result() try: self.up.put(self.gen_file_path(db_file), db_file.body.read()) except Exception, e: log_debug.error(u"upyun上传文件<%s>失败,%s" % (db_file.name, e)) ret.success = False ret.exception = e ret.msg = e.message
def delete_category(self, id): """ 删除游戏系列 """ ret = Result() try: category = Category.objects.get(id=id) count = category.game_set.count() if count != 0: ret.success = False ret.msg = u"系列中还有%s款游戏,不能删除该系列" % count return ret if category.icon: category.icon.delete() category.delete() except Exception, e: log_debug.error(u"删除游戏系列<id:%s>,数据库操作失败,%s" % (id, e)) ret.success = False ret.msg = u"数据库操作失败"
def delete_file(self, file_id): """ 删除文件 """ ret = Result() #获取到对象 try: delete_file = File.objects.get(id=file_id) except Exception, e: log_debug.error(u"删除文件,数据库获取对象失败,%s" % e) ret.success = False ret.msg = u"数据库操作失败,对象可能不存在" return ret
def add_game(self, game): """ 添加游戏 """ ret = Result() try: game.save() map(game.tags.add, [item.strip() for item in game.tag_ids.split(',') if item.strip()]) map(game.screens.add, [item.strip() for item in game.screen_ids.split(',') if item.strip()]) except Exception, e: log_debug.error(u"添加游戏<%s>,数据库操作失败,%s" % (game.name_ch, e)) ret.success = False ret.msg = u"数据库操作失败"
def update_category(self, category): """ 编辑游戏系列 """ ret = Result() try: if not category.id: category.save() else: category.save_most() except Exception, e: log_debug.error(u"更新游戏系列<%s>,数据库操作失败,%s" % (tag.name_ch, e)) ret.success = False ret.msg = u"数据库操作失败"
def update_tag(self, tag): """ 编辑游戏标签 """ ret = Result() try: if not tag.id: tag.save() else: tag.save_most() except Exception, e: log_debug.error(u"更新游戏标签<%s>,数据库操作失败,%s" % (tag.name_ch, e)) ret.success = False ret.msg = u"数据库操作失败"
def stick_game(self, gamecatgory_id): """ 置顶游戏 """ import datetime ret = Result() try: gamecatgory = GameCategory.objects.get(id=gamecatgory_id) gamecatgory.stick_time = datetime.datetime.today() gamecatgory.save(update_fields=['stick_time']) except Exception, e: log_debug.error(u"置顶游戏<%s>,数据库操作失败,%s" % (gamecatgory_id, e)) ret.success = False ret.msg = u"数据库操作失败"
def list_category(self, id, page_num, page_size): """ 获取系列的详细信息 :param id: 系列id :param page_num: 当前第几页 :param page_size: 每页大小 :return: """ ret = Result() try: if not page_num or int(page_num) < 1: page_num = 1 if not page_size or int(page_size) < 1: page_size = 10 page_num = int(page_num) page_size = int(page_size) game_arr = [] json_obj = { 'pageNum': page_num, 'pageSize': page_size, 'games': game_arr, } for item in Category.objects.get(id=id).game_set.all()\ .order_by('-gamecategory__stick_time', '-update_time') \ [(page_num-1)*page_size : page_num*page_size]: item_dict = { 'id': item.id, 'name': item.name, 'name_ch': item.name_ch, 'desc': item.desc, 'desc_ch': item.desc_ch, 'starValue': item.star, 'icon': item.icon and item.icon.url or '', 'snapshots': [ i.url for i in item.screens.all() ], 'packageName': item.apk_pack and item.apk_pack.name or '', 'size': item.apk_pack and item.apk_pack.size or 0, 'downloadURL': item.apk_pack and item.apk_pack.url or '', } game_arr.append(item_dict) ret.data['json_data'] = json.dumps(json_obj) except Exception, e: log_debug.error(u"获取游戏系列<id:%s>,数据库操作失败,%s" % (id, e)) ret.success = False ret.msg = u"数据库操作失败" raise e
def list_hot_category(self, pageNum, pageSize): """ 获取热门游戏列表 :param pageNum: :param pageSize: :return: """ ret = Result() try: category = Category.objects.filter(name__iexact='hot')[0] return self.list_category(category.id, pageNum, pageSize) except Exception, e: log_debug.error(u"获取所有游戏系列失败,数据库操作失败,%s" % (e)) ret.success = False ret.msg = u"数据库操作失败" raise e
def add_game(self, game): """ 添加游戏 """ ret = Result() try: game.save() map(game.tags.add, [ item.strip() for item in game.tag_ids.split(',') if item.strip() ]) map(game.screens.add, [ item.strip() for item in game.screen_ids.split(',') if item.strip() ]) except Exception, e: log_debug.error(u"添加游戏<%s>,数据库操作失败,%s" % (game.name_ch, e)) ret.success = False ret.msg = u"数据库操作失败"
def wrapped(*args, **kwargs): # __doc__ encode as ascii # and we only need the first line doc = func.__doc__ description = doc.strip().split('\n')[0].decode('utf-8') log_service.debug(u'<%s:%s> %s [ 参数:<%s %s> ]' % (func.__module__, func.__name__, description, args, kwargs)) try: ret = func(*args, **kwargs) if ret.success: log_service.info(u'%s [ 成功 ]' % (description)) else: log_service.error(u'%s [ 失败,%s ]' % (description, ret.msg)) return ret except Exception, e: log_service.error(u'%s [ 出现异常,%s ]' % (description, e)) ret = Result() ret.success = False ret.exception = e ret.msg = e.message return ret
def wrapped(*args, **kwargs): # __doc__ encode as ascii # and we only need the first line doc = func.__doc__ description = doc.strip().split('\n')[0].decode('utf-8') log_service.debug( u'<%s:%s> %s [ 参数:<%s %s> ]' % (func.__module__, func.__name__, description, args, kwargs)) try: ret = func(*args, **kwargs) if ret.success: log_service.info(u'%s [ 成功 ]' % (description)) else: log_service.error(u'%s [ 失败,%s ]' % (description, ret.msg)) return ret except Exception, e: log_service.error(u'%s [ 出现异常,%s ]' % (description, e)) ret = Result() ret.success = False ret.exception = e ret.msg = e.message return ret
def get_all_category(self): """ 获取所有游戏系列 """ ret = Result() try: json_obj = [] for item in Category.objects.all(): item_dict = { 'name': item.name, 'name_ch': item.name_ch, 'desc': item.desc, 'desc_ch': item.desc_ch, 'icon': item.icon and item.icon.url or '', 'length': item.game_set.count(), 'gamesURL': '%s/game/category/%s/' % (OPEN_API_URL_PREFIX.rstrip('/'), item.id) } json_obj.append(item_dict) ret.data['json_data'] = json.dumps(json_obj) except Exception, e: log_debug.error(u"获取所有游戏系列失败,数据库操作失败,%s" % (e)) ret.success = False ret.msg = u"数据库操作失败" raise e