本文整理匯總了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')