當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。