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