本文整理汇总了Python中flask.views.MethodViewType方法的典型用法代码示例。如果您正苦于以下问题:Python views.MethodViewType方法的具体用法?Python views.MethodViewType怎么用?Python views.MethodViewType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类flask.views
的用法示例。
在下文中一共展示了views.MethodViewType方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: route
# 需要导入模块: from flask import views [as 别名]
# 或者: from flask.views import MethodViewType [as 别名]
def route(self, rule, *, parameters=None, **options):
"""Decorator to register url rule in application
Also stores doc info for later registration
Use this to decorate a :class:`MethodView <flask.views.MethodView>` or
a resource function.
:param str rule: URL rule as string.
:param str endpoint: Endpoint for the registered URL rule (defaults
to function name).
:param list parameters: List of parameters relevant to all operations
in this path, only used to document the resource.
:param dict options: Options to be forwarded to the underlying
:class:`werkzeug.routing.Rule <Rule>` object.
"""
def decorator(func):
# By default, endpoint name is function name
endpoint = options.pop('endpoint', func.__name__)
# Prevent registering several times the same endpoint
# by silently renaming the endpoint in case of collision
if endpoint in self._endpoints:
endpoint = '{}_{}'.format(endpoint, len(self._endpoints))
self._endpoints.append(endpoint)
if isinstance(func, MethodViewType):
view_func = func.as_view(endpoint)
else:
view_func = func
# Add URL rule in Flask and store endpoint documentation
self.add_url_rule(rule, endpoint, view_func, **options)
self._store_endpoint_docs(endpoint, func, parameters, **options)
return func
return decorator
示例2: _store_endpoint_docs
# 需要导入模块: from flask import views [as 别名]
# 或者: from flask.views import MethodViewType [as 别名]
def _store_endpoint_docs(self, endpoint, obj, parameters, **options):
"""Store view or function doc info"""
endpoint_doc_info = self._docs.setdefault(endpoint, OrderedDict())
def store_method_docs(method, function):
"""Add auto and manual doc to table for later registration"""
# Get documentation from decorators
# Deepcopy doc info as it may be used for several methods and it
# may be mutated in apispec
doc = deepcopy(getattr(function, '_apidoc', {}))
# Get summary/description from docstring
doc['docstring'] = load_info_from_docstring(
function.__doc__, delimiter=self.DOCSTRING_INFO_DELIMITER)
# Store function doc infos for later processing/registration
endpoint_doc_info[method.lower()] = doc
# MethodView (class)
if isinstance(obj, MethodViewType):
for method in self.HTTP_METHODS:
if method in obj.methods:
func = getattr(obj, method.lower())
store_method_docs(method, func)
# Function
else:
methods = options.pop('methods', None) or ['GET']
for method in methods:
store_method_docs(method, obj)
# Store parameters doc info from route decorator
endpoint_doc_info['parameters'] = parameters
示例3: _get_endpoint
# 需要导入模块: from flask import views [as 别名]
# 或者: from flask.views import MethodViewType [as 别名]
def _get_endpoint(self, view_func, endpoint=None, plural=False):
if endpoint:
assert '.' not in endpoint, 'Api endpoints should not contain dots'
elif isinstance(view_func, MethodViewType):
endpoint = camel_to_snake_case(view_func.__name__)
if hasattr(view_func, 'model') and plural:
plural_model = camel_to_snake_case(view_func.model.__plural__)
endpoint = f'{plural_model}_resource'
else:
endpoint = view_func.__name__
return f'{self.name}.{endpoint}'