本文整理汇总了Python中werkzeug.routing.BaseConverter方法的典型用法代码示例。如果您正苦于以下问题:Python routing.BaseConverter方法的具体用法?Python routing.BaseConverter怎么用?Python routing.BaseConverter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类werkzeug.routing
的用法示例。
在下文中一共展示了routing.BaseConverter方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_api_register_converter
# 需要导入模块: from werkzeug import routing [as 别名]
# 或者: from werkzeug.routing import BaseConverter [as 别名]
def test_api_register_converter(
self, app, view_type, custom_format, openapi_version
):
app.config['OPENAPI_VERSION'] = openapi_version
api = Api(app)
blp = Blueprint('test', 'test', url_prefix='/test')
class CustomConverter(BaseConverter):
pass
app.url_map.converters['custom_str'] = CustomConverter
api.register_converter(CustomConverter, 'custom string', custom_format)
if view_type == 'function':
@blp.route('/<custom_str:val>')
def test_func(val):
pass
else:
@blp.route('/<custom_str:val>')
class TestMethod(MethodView):
def get(self, val):
pass
api.register_blueprint(blp)
spec = api.spec.to_dict()
schema = {'type': 'custom string'}
# If custom_format is None (default), it does not appear in the spec
if custom_format is not None:
schema['format'] = 'custom'
parameter = {'in': 'path', 'name': 'val', 'required': True}
if openapi_version == '2.0':
parameter.update(schema)
else:
parameter['schema'] = schema
assert spec['paths']['/test/{val}']['parameters'] == [parameter]
示例2: test_api_register_converter_before_and_after_init
# 需要导入模块: from werkzeug import routing [as 别名]
# 或者: from werkzeug.routing import BaseConverter [as 别名]
def test_api_register_converter_before_and_after_init(
self, app, openapi_version
):
app.config['OPENAPI_VERSION'] = openapi_version
api = Api()
blp = Blueprint('test', 'test', url_prefix='/test')
class CustomConverter_1(BaseConverter):
pass
class CustomConverter_2(BaseConverter):
pass
app.url_map.converters['custom_str_1'] = CustomConverter_1
app.url_map.converters['custom_str_2'] = CustomConverter_2
api.register_converter(CustomConverter_1, 'custom string 1')
api.init_app(app)
api.register_converter(CustomConverter_2, 'custom string 2')
@blp.route('/1/<custom_str_1:val>')
def test_func_1(val):
pass
@blp.route('/2/<custom_str_2:val>')
def test_func_2(val):
pass
api.register_blueprint(blp)
spec = api.spec.to_dict()
parameter_1 = spec['paths']['/test/1/{val}']['parameters'][0]
parameter_2 = spec['paths']['/test/2/{val}']['parameters'][0]
if openapi_version == '2.0':
assert parameter_1['type'] == 'custom string 1'
assert parameter_2['type'] == 'custom string 2'
else:
assert parameter_1['schema']['type'] == 'custom string 1'
assert parameter_2['schema']['type'] == 'custom string 2'
示例3: test_custom_converters
# 需要导入模块: from werkzeug import routing [as 别名]
# 或者: from werkzeug.routing import BaseConverter [as 别名]
def test_custom_converters(self):
from werkzeug.routing import BaseConverter
class ListConverter(BaseConverter):
def to_python(self, value):
return value.split(',')
def to_url(self, value):
base_to_url = super(ListConverter, self).to_url
return ','.join(base_to_url(x) for x in value)
app = flask.Flask(__name__)
app.url_map.converters['list'] = ListConverter
@app.route('/<list:args>')
def index(args):
return '|'.join(args)
c = app.test_client()
self.assert_equal(c.get('/1,2,3').data, b'1|2|3')