def pack_result(self): if self.is_validate(): return ActionResult(ActionCode.SEARCH_CATEGORY, StatusCode.SUCCESS, MsgCode.REQUEST_PARAM_CHECK_VALID) else: data = DictData() data.add("errors_info", self.errors) return ActionResult(ActionCode.SEARCH_CATEGORY, StatusCode.FAILED, MsgCode.REQUEST_PARAM_CHECK_INVALID, data)
def search_category(word, action=ActionCode.SEARCH_CATEGORY): try: wd = '%%%s%%' % word result = db.session.query(Link.category_id, Category.name, func.count(Link.category_id)) \ .filter(or_(Link.name.like(wd), Link.description.like(wd)))\ .group_by(Link.category_id). \ join(Category).all() if result is not None and isinstance(result, list): total_category_count = len(result) result_list = [] for res in result: if len(res) == 3: sub = dict() sub["category_id"] = res[0] sub["category_name"] = res[1] sub["count"] = res[2] result_list.append(sub) data = DictData() data.add("total_record", total_category_count) data.add("data", result_list) data.add("word", word) return ActionResult(action, StatusCode.SUCCESS, MsgCode.QUERY_SUCC, data) except: traceback.print_exc() return ActionResult(action, StatusCode.FAILED, MsgCode.QUERY_FAILED)
def add_link_batch(json_file, action=ActionCode.BATCH_ADD_LINK_BY_FILE): if json_file is None: return ActionResult(action, StatusCode.FAILED, MsgCode.REQUEST_PARAM_NOT_FOUND) try: file = open(json_file, encoding='utf-8') json_content = json.load(file) if json_content is None: return ActionResult(action, StatusCode.FAILED, MsgCode.REQUEST_PARAM_NOT_FOUND) except: return ActionResult(action, StatusCode.FAILED, MsgCode.REQUEST_PARAM_ERROR) if json_content['data'] is not None and len(json_content['data']) > 0: # 记录添加失败的id failed_list = list() for link in json_content['data']: action_result = add_link(json.dumps(link)) if action_result.status_code != StatusCode.SUCCESS: failed_data = dict() failed_data['error_msg'] = MsgCode.get_message(action_result.msg_code) failed_data['link_info'] = link failed_list.append(failed_data) if len(failed_list) > 0: result = DictData() result.add("about", "未添加成功的数据") result.add("total_record", len(failed_list)) result.add("failed_data", failed_list) if len(failed_list) == len(json_content['data']): return ActionResult(action, StatusCode.FAILED, MsgCode.ADD_FAILED, result) else: return ActionResult(action, StatusCode.SUCCESS, MsgCode.ADD_PART_SUCC, result) else: return ActionResult(action, StatusCode.SUCCESS, MsgCode.ADD_SUCC) else: return ActionResult(action, StatusCode.FAILED, MsgCode.LINK_IS_NULL)
def get_user_links_by_page(user_id, page, page_size, action=ActionCode.GET_LINKS_BY_PAGE): try: user_id = int(user_id) page = int(page) page_size = int(page_size) except: traceback.print_exc() return ActionResult(action, StatusCode.FAILED, MsgCode.PAGE_INVALID) try: user = User.query.filter_by(id=user_id).first() except: traceback.print_exc() return ActionResult(action, StatusCode.FAILED, MsgCode.USER_NOT_EXIST) if page <= 0: return ActionResult(action, StatusCode.FAILED, MsgCode.PAGE_INVALID) else: try: paginate = user.shared_links().paginate(int(page), int(page_size), False) # paginate = Link.query.order_by(Link.datetime.desc()).paginate(int(page), int(page_size), False) links = paginate.items links_total_count = paginate.total links_list = list() for link in links: links_list.append(link.json()) result = DictData() result.add("total_record", links_total_count) result.add("data", links_list) return ActionResult(action, StatusCode.SUCCESS, MsgCode.QUERY_SUCC, result) except: traceback.print_exc() return ActionResult(action, StatusCode.FAILED, MsgCode.QUERY_FAILED)
def get_primary_link(action=ActionCode.GET_PRIMARY_LINK): try: primary_links = PrimaryLink.query.order_by(PrimaryLink.add_datetime.desc()).all() total_count = len(primary_links) links_list = list() for primary in primary_links: links_list.append({"url": primary.get_url(), "img": primary.get_img()}) result = DictData() result.add("total_record", total_count) result.add("data", links_list) return ActionResult(action, StatusCode.SUCCESS, MsgCode.QUERY_SUCC, result) except: traceback.print_exc() return ActionResult(action, StatusCode.FAILED, MsgCode.QUERY_FAILED)
def get_links_by_name(user_name, action=ActionCode.GET_LINK_BY_ID): action_result = get_user_model_by_name(user_name) if action_result.success(): user_db = action_result.data users = user_db.links.all() user_list = list() for user in users: user_list.append(user.json()) data = DictData() data.add("total_record", user_db.links.count()) data.add("data", user_list) return ActionResult(action, StatusCode.SUCCESS, MsgCode.QUERY_SUCC, data) else: return action_result
def get_link_by_id(link_id, action=ActionCode.GET_LINK_BY_ID): if link_id is None: return ActionResult(action, StatusCode.FAILED, MsgCode.REQUEST_PARAM_NOT_FOUND) try: link_id = int(link_id) link = Link.query.filter_by(id=link_id).first() if link is not None: data = list() data.append(link.json()) result = DictData() result.add("total_record", 1) result.add("data", data) return ActionResult(action, StatusCode.SUCCESS, MsgCode.QUERY_SUCC, result) else: return ActionResult(action, StatusCode.FAILED, MsgCode.LINK_NOT_EXIST) except: traceback.print_exc() return ActionResult(action, StatusCode.FAILED, MsgCode.LINK_NOT_EXIST)
def get_user_by_id(user_id, action=ActionCode.GET_USER_BY_ID): if user_id is None: return ActionResult(action, StatusCode.FAILED, MsgCode.REQUEST_PARAM_NOT_FOUND) try: user_id = int(user_id) user = User.query.filter_by(id=user_id).first() if user is not None: data = list() data.append(user.json()) result = DictData() result.add("total_record", 1) result.add("data", data) return ActionResult(action, StatusCode.SUCCESS, MsgCode.QUERY_SUCC, result) else: return ActionResult(action, StatusCode.FAILED, MsgCode.USER_NOT_EXIST) except: traceback.print_exc() return ActionResult(action, StatusCode.FAILED, MsgCode.USER_NOT_EXIST)
def get_links_by_page(page, page_size, conditions_dict, action=ActionCode.GET_LINKS_BY_PAGE): try: query = add_conditions(Link, conditions_dict) paginate = query.order_by(Link.update_datetime.desc()).paginate(int(page), int(page_size), False) total_count = paginate.total links = paginate.items links_list = list() for link in links: links_list.append(link.json()) result = DictData() result.add("total_record", total_count) result.add("total_pages", paginate.pages) result.add("current_page", paginate.page) result.add("current_page_size", paginate.per_page) result.add("has_prev", paginate.has_prev) result.add("has_next", paginate.has_next) result.add("data", links_list) return ActionResult(action, StatusCode.SUCCESS, MsgCode.QUERY_SUCC, result) except: traceback.print_exc() return ActionResult(action, StatusCode.FAILED, MsgCode.QUERY_FAILED)
def get_categories_by_page(page, page_size, conditions_dict, action=ActionCode.GET_CATEGORY): try: query = add_conditions(Category, conditions_dict) paginate = query.order_by(Category.id.asc()).paginate( int(page), int(page_size), False) total_count = paginate.total categories = paginate.items category_list = list() for category in categories: category_list.append(category.json()) result = DictData() result.add("total_record", total_count) result.add("total_pages", paginate.pages) result.add("current_page", paginate.page) result.add("current_page_size", paginate.per_page) result.add("has_prev", paginate.has_prev) result.add("has_next", paginate.has_next) result.add("data", category_list) return ActionResult(action, StatusCode.SUCCESS, MsgCode.QUERY_SUCC, result) except: traceback.print_exc() return ActionResult(action, StatusCode.FAILED, MsgCode.QUERY_FAILED)
def search(page, page_size, word, category_id=None, action=ActionCode.SEARCH_CATEGORY): try: wd = '%%%s%%' % word query = Link.query.filter(or_(Link.name.like(wd), Link.description.like(wd))) if category_id is not None: query = query.filter_by(category_id=int(category_id)) paginate = query.order_by(Link.update_datetime.desc()).paginate(int(page), int(page_size), False) total_count = paginate.total links = paginate.items links_list = list() for link in links: links_list.append(link.json()) result = DictData() result.add("total_record", total_count) result.add("total_pages", paginate.pages) result.add("current_page", paginate.page) result.add("current_page_size", paginate.per_page) result.add("has_prev", paginate.has_prev) result.add("has_next", paginate.has_next) result.add("data", links_list) return ActionResult(action, StatusCode.SUCCESS, MsgCode.QUERY_SUCC, result) except: traceback.print_exc() return ActionResult(action, StatusCode.FAILED, MsgCode.QUERY_FAILED)
def get_worthy_link(page, page_size, action=ActionCode.GET_WORTHY_LINK): try: paginate = WorthyLink.query.order_by(WorthyLink.add_datetime.desc()).paginate(int(page), int(page_size), False) total_count = paginate.total worthy_links = paginate.items links_list = list() for worthy in worthy_links: links_list.append(worthy.get_link().json()) result = DictData() result.add("total_record", total_count) result.add("total_pages", paginate.pages) result.add("current_page", paginate.page) result.add("current_page_size", paginate.per_page) result.add("has_prev", paginate.has_prev) result.add("has_next", paginate.has_next) result.add("data", links_list) return ActionResult(action, StatusCode.SUCCESS, MsgCode.QUERY_SUCC, result) except: traceback.print_exc() return ActionResult(action, StatusCode.FAILED, MsgCode.QUERY_FAILED)
def query_roles(page, page_size, conditions_dict, action=ActionCode.GET_ROLES): try: query = add_conditions(Role, conditions_dict) paginate = query.order_by(Role.id.asc()).paginate( int(page), int(page_size), False) total_count = paginate.total roles = paginate.items role_list = list() for role in roles: role_list.append(role.json()) result = DictData() result.add("total_record", total_count) result.add("total_pages", paginate.pages) result.add("current_page", paginate.page) result.add("current_page_size", paginate.per_page) result.add("has_prev", paginate.has_prev) result.add("has_next", paginate.has_next) result.add("data", role_list) return ActionResult(action, StatusCode.SUCCESS, MsgCode.QUERY_SUCC, result) except: traceback.print_exc() return ActionResult(action, StatusCode.FAILED, MsgCode.QUERY_FAILED)
def get_permissions(action=ActionCode.GET_PERMISSIONS): data = DictData() data.add("permissions", Permission.get_permissions()) return ActionResult(action, StatusCode.SUCCESS, MsgCode.QUERY_SUCC, data)
def get_users_by_page(page, page_size, conditions_dict, action=ActionCode.GET_USERS_BY_PAGE): try: query = add_conditions(User, conditions_dict) paginate = query.order_by(User.register_datetime.desc()).paginate(int(page), int(page_size), False) total_count = paginate.total users = paginate.items user_list = list() for user in users: user_list.append(user.json()) result = DictData() result.add("total_record", total_count) result.add("total_pages", paginate.pages) result.add("current_page", paginate.page) result.add("current_page_size", paginate.per_page) result.add("has_prev", paginate.has_prev) result.add("has_next", paginate.has_next) result.add("data", user_list) return ActionResult(action, StatusCode.SUCCESS, MsgCode.QUERY_SUCC, result) except: traceback.print_exc() return ActionResult(action, StatusCode.FAILED, MsgCode.QUERY_FAILED)