def pagination_parser( self, parser: RequestParser = None, sort_choices: List[str] = None, default: str = None, add_sort: bool = None, ) -> RequestParser: """ Return a standardized pagination parser, to be used for any endpoint that has pagination. :param RequestParser parser: Can extend a given parser or create a new one :param tuple sort_choices: A tuple of strings, to be used as server side attribute searches :param str default: The default sort string, used `sort_choices[0]` if not given :param bool add_sort: Add sort order choices without adding specific sort choices :return: An api.parser() instance with pagination and sorting arguments. """ pagination = parser.copy() if parser else self.parser() pagination.add_argument('page', type=int, default=1, help='Page number') pagination.add_argument('per_page', type=int, default=50, help='Results per page') if sort_choices or add_sort: pagination.add_argument( 'order', choices=('desc', 'asc'), default='desc', help='Sorting order' ) if sort_choices: pagination.add_argument( 'sort_by', choices=sort_choices, default=default or sort_choices[0], help='Sort by attribute', ) return pagination
def test_request_parser_remove_argument(self): req = Request.from_values("/bubble?foo=baz") parser = RequestParser() parser.add_argument("foo", type=int) parser_copy = parser.copy() parser_copy.remove_argument("foo") args = parser_copy.parse_args(req) assert args == {}
def test_request_parser_replace_argument(self, app): req = Request.from_values('/bubble?foo=baz') parser = RequestParser() parser.add_argument('foo', type=int) parser_copy = parser.copy() parser_copy.replace_argument('foo') args = parser_copy.parse_args(req) assert args['foo'] == 'baz'
def test_request_parser_copy(self, app): req = Request.from_values("/bubble?foo=101&bar=baz") parser = RequestParser() foo_arg = Argument("foo", type=int) parser.args.append(foo_arg) parser_copy = parser.copy() # Deepcopy should create a clone of the argument object instead of # copying a reference to the new args list assert foo_arg not in parser_copy.args # Args added to new parser should not be added to the original bar_arg = Argument("bar") parser_copy.args.append(bar_arg) assert bar_arg not in parser.args args = parser_copy.parse_args(req) assert args["foo"] == 101 assert args["bar"] == "baz"
create_widget_reqparser.add_argument( "info_url", type=URL(schemes=["http", "https"]), location="form", required=True, nullable=False, ) create_widget_reqparser.add_argument( "deadline", type=future_date_from_string, location="form", required=True, nullable=False, ) update_widget_reqparser = create_widget_reqparser.copy() update_widget_reqparser.remove_argument("name") pagination_reqparser = RequestParser(bundle_errors=True) pagination_reqparser.add_argument("page", type=positive, required=False, default=1) pagination_reqparser.add_argument("per_page", type=positive, required=False, choices=[5, 10, 25, 50, 100], default=10) widget_owner_model = Model("Widget Owner", { "email": String,
"lastName", dest="last_name", type=str, location="form", required=False, ).add_argument( "photo", default=None, type=werkzeug.datastructures.FileStorage, location="files", ) user_parser = user_info_parser.copy().add_argument( "username", dest="username", type=unique_value_converter(User, "username", str), required=True, location="form", ) user_query_parser = RequestParser() user_query_parser.add_argument("q", choices=["sessions"], type=str, location="args", required=False) user_query_parser.add_argument("slug", help="session's identifying", type=str, location="args", required=False)
'RideRequest', { 'seats': fields.Integer, 'stop': fields.String, 'status': fields.String, 'made_at': fields.DateTime }) r_request_parser = RequestParser() r_request_parser.add_argument('stop', location='json', nullable=True) # Passenger stop can be blank r_request_parser.add_argument('seats', type=int, location='json', nullable=True) # Passenger seat can be blank req_update_parser = r_request_parser.copy() req_update_parser.replace_argument('stop', location='json', required=True, help='Your Stop Cannot be blank', nullable=False) req_update_parser.replace_argument('seats', type=int, location='json', required=True, help='Your Seats cannot be blank', nullable=False) req_action_parser = RequestParser() req_action_parser.add_argument('action', choices=('accepted', 'rejected'),
nullable=False, help='时间戳') base_parser.add_argument('nonce', location='headers', type=str, required=True, nullable=False, help='随机字符串') base_parser.add_argument('sign', location='headers', type=str, required=True, nullable=False, help='校验和') json_parser = base_parser.copy() # 定义基础参数 json_parser.add_argument('key', location='headers', type=str, required=True, nullable=False, help='加密后的密钥') json_parser.add_argument('data', location='json', type=decrypt_json_body, required=True, nullable=False, help='加密后的json数据') form_parser = base_parser.copy()
def test_request_parse_copy_including_settings(self): parser = RequestParser(trim=True, bundle_errors=True) parser_copy = parser.copy() assert parser.trim == parser_copy.trim assert parser.bundle_errors == parser_copy.bundle_errors
"""Data Transfer Objects for Authentication""" from flask_restx.reqparse import RequestParser from flask_restx.inputs import email user_login_parser = RequestParser(bundle_errors=True) user_login_parser.add_argument("username", required=True, help="username cannot be blank", location='json', nullable=False) # TODO: Remove fieldS user_login_parser.add_argument("password", required=True, help="password cannot be blank", location='json', nullable=False) user_signup_parser = user_login_parser.copy() user_signup_parser.add_argument("name", required=True, help="name cannot be blank", location='json', nullable=False) user_signup_parser.add_argument("email", type=email(), required=True, help="email cannot be blank", location='json', nullable=False)