当前位置: 首页>>代码示例>>Python>>正文


Python utils.swagger_auto_schema方法代码示例

本文整理汇总了Python中drf_yasg.utils.swagger_auto_schema方法的典型用法代码示例。如果您正苦于以下问题:Python utils.swagger_auto_schema方法的具体用法?Python utils.swagger_auto_schema怎么用?Python utils.swagger_auto_schema使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在drf_yasg.utils的用法示例。


在下文中一共展示了utils.swagger_auto_schema方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: viewset_swagger_helper

# 需要导入模块: from drf_yasg import utils [as 别名]
# 或者: from drf_yasg.utils import swagger_auto_schema [as 别名]
def viewset_swagger_helper(
        public_actions=None,
        tags: Optional[List[str]] = None,
        **action_summary_docs: Optional[str]
) -> Callable:
    """
    A meta-decorator to apply swagger decorators for multiple methods.

    This decorator simplifies applying multiple decorators by applying sane defaults
    for swagger decorator, and passing in the supplied values as the summary.

    Example:
        @viewset_swagger_helper(
            list="List Snippets",
            create="Create new Snippet",
            destroy="Delete Snippet",
            public_actions=["list"]
        )
        class SnippetViewSet(ModelViewSet): ...

        In the above example this function will apply a decorator that will override
        the operation summary for `list`, `create` and `destroy` for the `SnippetViewSet`.
        The `public_actions` parameter specifies which actions don't need an authentication
        and as such won't raise an authentication/authorization error.
    """
    decorators_to_apply = []
    public_actions = [] if public_actions is None else public_actions
    for action in ['list', 'create', 'retrieve', 'update', 'partial_update', 'destroy']:
        if action in action_summary_docs:
            decorators_to_apply.append(
                method_decorator(
                    name=action,
                    decorator=swagger_auto_schema(
                        operation_summary=action_summary_docs.get(action),
                        responses=VALIDATION_RESPONSE if action in public_actions else VALIDATION_AND_AUTH_RESPONSES,
                        tags=tags,
                        security=[] if action in public_actions else None,
                    ),
                )
            )

    def inner(viewset):
        """
        Applies all the decorators built up in the decorator list.
        """
        for decorator in decorators_to_apply:
            viewset = decorator(viewset)
        return viewset

    return inner 
开发者ID:open-craft,项目名称:opencraft,代码行数:52,代码来源:swagger.py


注:本文中的drf_yasg.utils.swagger_auto_schema方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。