def post(self): schema = Schema({"name": And(str, len), "production": And(str, len)}) req, error = validate_schema(schema, request.json) if error: return error, 400 name = req["name"] if TbMerchant.query.filter_by(name=name).first(): return Msg.MERCHANT_NAME_ALREADY_EXIST, 400 mer = TbMerchant(name=name) db.session.add(mer) db.session.commit() mer.code = "{:08}".format(mer.id) pro = TbProduction(name=req["production"], merchant_code=mer.code, status=True) mer.production.append(pro) db.session.add(pro) db.session.commit() pro.code = str(pro.id + 1600000000) msg = "SUPPORT | B_USER | ADD_MERCHAT | SUCCESS | USER: {} {} | MERCHAT: {} {} | PRODUCTION: {} {}".format( request.current_user.code, request.current_user.name, mer.code, mer.name, pro.code, pro.name) operation = TbOperation(operator_code=request.current_user.code, content=msg, category="MERCHANT", type="ADD") db.session.add(operation) db.session.commit() app.logger.info(msg) return {}, 200
def dispatch_request(self, *args, **kwargs): req = None schema = None if request.method == "GET": schema = self.validate_schemas.get("get") req = request.args.to_dict() else: schema = self.validate_schemas.get(request.method.lower()) req = request.json if isinstance(schema, Schema): data, errors = validate_schema(schema, move_space(req)) if errors: logger.info(str(errors)) return str(errors), 400 self.validate_data = data try: ret = super().dispatch_request(*args, **kwargs) return ret except NotUniqueError: logger.warn(traceback.format_exc()) return Msg.DATA_EXIST, 400 except ValidationError as e: logger.warn(traceback.format_exc()) return str(e), 400 except DoesNotExist: logger.warn(traceback.format_exc()) return Msg.NO_DATA, 400 except AttributeError as e: logger.warn(traceback.format_exc()) return str(e), 400 except Exception as e: logger.warn(traceback.format_exc()) return str(e), 400
def post(self): """ 通过接受一个商户 code 列表,返回商户详情数据 """ schema = Schema({Optional("merchants"): [str]}) req, error = validate_schema(schema, request.json) if error: return error, 400 merchant_code = req.get("merchants") if not merchant_code: merchants = TbMerchant.query.all() else: merchants = TbMerchant.query.filter( TbMerchant.code.in_(merchant_code)).all() results = [] for i in merchants: mer_biz = TbMerchantBusiness.query.filter_by( merchant_code=i.code).all() results.append({ "merchant_code": i.code, "merchant_name": i.name, "production": [{ "production_name": j.name, "production_code": j.code } for j in i.production], "business": [{ "business_code": j.business_code, "business_name": j.business_name, "domain": j.domain } for j in mer_biz] }) return {"results": results}, 200
def put(self): """更新产品信息""" schema = Schema({ "production_code": And(str), "appkey": And(str), "appid": And(str), "sign": And(str) }) req, error = validate_schema(schema, request.json) if error: return error, 400 production = TbProduction.query.filter_by( code=req["production_code"]).first() if not production: return Msg.PRODUCTION_NOT_EXIST, 400 production.sms_appkey = req["appkey"] production.sms_appid = req["appid"] production.sms_sign = req["sign"] msg = "SUPPORT | B_USER | UPDATE_PRODUCTION | SUCCESS | EDITOR: {} {} | PRODUCTION: {} {} | MERCHANT: {} {}".\ format(request.current_user.code, request.current_user.name, production.code, production.name, production.merchant.code, production.merchant.name) operation = TbOperation(operator_code=request.current_user.code, content=msg, category="PRODUCTION", type="EDIT") db.session.add(operation) db.session.commit() logger.info(msg) return {}, 200
def decorator(*args, **kwargs): method = request.method if method == "GET": params = request.args.to_dict() else: params = request.json req, error = validate_schema(universal_schema, params) if error: return error, 400 user = TbUser.query.filter_by(code=req["user_code"]).first() if not user: return Msg.USER_NOT_EXISTS, 400 if not user.is_admin: # 获取用户所有的能访问的菜单 path menus = [i.path for j in user.roles for i in j.menu] category_name = ConfigNameMap.name[req["category"]] if ConfigNameMap.menu_path[category_name] not in menus: return Msg.PERMISSION_DENIED, 401 request.current_user = user return func(*args, **kwargs)
def get(self): schema = Schema({Optional("merchant_code"): str}) req, error = validate_schema(schema, request.args.to_dict()) if error: return error, 400 merchant_code = req.get("merchant_code") if merchant_code: mer = TbMerchant.query.filter_by(code=merchant_code).first() if not mer: return Msg.MERCHANT_NOT_EXIST, 400 results = [{ "production_code": i.code, "production_name": i.name } for i in mer.production] else: production = TbProduction.query.filter_by().all() results = [{ "production_code": i.code, "production_name": i.name } for i in production] return {"results": results}, 200
def post(self): """ 通过接受一个用户 code 列表,传递出数据 """ schema = Schema({"users": [str]}) req, error = validate_schema(schema, request.json) if error: return error, 400 users_codes = req["users"] if len(users_codes) > 10: return {}, 200 users = TbUser.query.filter(TbUser.code.in_(users_codes)).all() return { "results": [{ "user_name": i.name, "user_code": i.code, "phone": i.phone, "merchant_name": i.merchant.name if i.merchant else "", "merchant_code": i.merchant.code if i.merchant else "" } for i in users] }, 200
def dispatch_request(self, *args, **kwargs): req = None schema = None if request.method == "GET": schema = self.validate_schemas.get("get") req = request.args.to_dict() else: schema = self.validate_schemas.get(request.method.lower()) req = request.json if isinstance(schema, Schema): data, errors = validate_schema(schema, move_space(req)) if errors: logger.info(str(errors)) return str(errors), 400 self.validate_data = data elif isinstance(schema, reqparse.RequestParser): parse_req = schema.parse_args() strict = self.validate_schemas.get("strict") or "" if strict: parse_req = schema.parse_args(strict=True) self.validate_data = {k: v for k, v in parse_req.items() if v} try: ret = super().dispatch_request(*args, **kwargs) return ret except NotUniqueError: logger.warn(traceback.format_exc()) return Msg.DATA_EXIST, 400 except ValidationError: logger.warn(traceback.format_exc()) return Msg.PARAMS_ERROR, 400 except DoesNotExist: logger.warn(traceback.format_exc()) return Msg.NO_DATA, 400 except AttributeError: logger.warn(traceback.format_exc()) return Msg.PARAMS_ERROR, 400 except Exception: logger.warn(traceback.format_exc()) return Msg.UNKNOWN, 400
def put(self): schema = Schema({ "id": And(str), "name": And(str, len), "production": And(str, len) }) req, error = validate_schema(schema, request.json, remove_blank=True) if error: return error, 400 mer = TbMerchant.query.filter_by(code=req["id"]).first() dup_mer = TbMerchant.query.filter_by(name=req["name"]).first() if dup_mer and dup_mer.code != mer.code: return Msg.MERCHANT_NAME_ALREADY_EXIST, 400 mer.name = req["name"] production = TbProduction.query.filter_by( merchant_code=req["id"]).first() if not production: return Msg.PRODUCTION_NOT_EXIST, 400 production.name = req.get("production") msg = "SUPPORT | B_USER | EDIT_MERCHAT | SUCCESS | USER: {} {} | MERCHAT: {} {} | PRODUCTION: {} {}".format( request.current_user.code, request.current_user.name, mer.code, mer.name, production.code, production.name) operation = TbOperation(operator_code=request.current_user.code, content=msg, category="MERCHANT", type="EDIT") db.session.add(operation) db.session.commit() app.logger.info(msg) # 删除 redis 商户的缓存 key = "tb_merchant:code:{}".format(mer.code) redis_delete(key) # 删除 redis 产品的缓存 key = "tb_production:merchant_code:{}".format(mer.code) redis_delete(key) return {}, 200