def _func(self, *args, **kwds): nonlocal fields_dict try: # 组装并验证参数 new_args = [ field(arg_val) for field, arg_val in zip(fields, args) ] new_kwds = { arg_name: fields_dict[arg_name](arg_val) for arg_name, arg_val in kwds.items() } except InvalidFieldException as e: full_reason = "{field_name}_{reason}".format( field_name=e.field_name, reason=e.reason) loc, _ = locale.getlocale(locale.LC_ALL) service_id = self.__class__.__name__ mtd_name = f.__name__ # 先从用户messages集合中获取 msg = get_from_nested_dict(messages, loc, service_id, mtd_name, full_reason) if msg is None: # 用户messages集合取不到再取默认的 msg = default_validate_messages[loc][e.reason] if e.expect is not None or e.expect != "": msg = msg.format(field_name=e.field_name, expect=e.expect) else: msg = msg.format(field_name=e.field_name) else: if e.expect is not None or e.expect != "": msg = msg.format(expect=e.expect) return Result.bad_request(full_reason, msg=msg) else: try: return f(self, *new_args, **new_kwds) except BadReq as e: loc, _ = locale.getlocale(locale.LC_ALL) msg = messages[loc][self.__class__.__name__][f.__name__][ e.reason] if e.args: msg = msg.format(**e.args) return Result.bad_request(e.reason, msg=msg)
def _check_token(self): """检查请求头中的token是否合法 """ try: token = self.request.headers["token"] return self._("UserService").check_token(token) except KeyError: return Result.bad_request("token_not_exist", "Can't find token in request headers")
def _gen_bad_req_result(self, e, full_field_name): loc, _ = locale.getlocale(locale.LC_ALL) full_reason = "{full_field_name}_{reason}".format( full_field_name=full_field_name, reason=e.reason) msg = default_validate_messages[loc][e.reason] if e.expect is None or e.expect == "": msg = msg.format(field_name=full_field_name) else: msg = msg.format(field_name=full_field_name, expect=e.expect) return Result.bad_request(reason=full_reason, msg=msg)
def _validate_tpl_bind_args(self, flow_meta, bind_args): new_bind_args = {} for job_ref in flow_meta.jobs: job = self._job_mgr.get(job_ref.job_name) new_bind_args[job.name] = {} for event, job_arg_declares in job.job_args.items(): new_bind_args[job.name][event] = {} for job_arg_declare in job_arg_declares: try: bind_value = get_from_nested_dict( bind_args, job.name, event, job_arg_declare.name) # const 类型的参数不允许被绑定 if job_arg_declare.mark == JobArg.MARK_CONST: continue # 如果为None并且是auto类型,那么可以在此不检查 if bind_value is None and \ job_arg_declare.mark == JobArg.MARK_AUTO: continue # 执行校验并替换新值 new_value = job_arg_declare.field_info(bind_value) new_bind_args[job.name][event][ job_arg_declare.name] = new_value except InvalidFieldException as e: field_name = "{job_name}_{event}_{arg_name}".format( job_name=job.name, event=event, arg_name=job_arg_declare.name) full_reason = "{field_name}_{reason}".format( field_name=field_name, reason=e.reason) # 产生错误消息 loc, _ = locale.getlocale(locale.LC_ALL) msg = default_validate_messages[loc][e.reason] if e.expect is None: msg = msg.format(field_name=field_name) else: msg = msg.format(field_name=field_name, expect=e.expect) return Result.bad_request(full_reason, msg=msg) return Result.ok(data=new_bind_args)
def on_angry(self, context): print("I'm angry! 你们这样子是不行的!我要终止整个flow!") context.stop() return Result.bad_request("old_man_angry", msg="I'm angry!")
def on_question(self, context, question): if question == "董先森连任好不好啊": context.finish() return Result.ok(data="吼啊") else: return Result.bad_request("old_man_angry", msg="无可奉告")