You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and dots ('.'), can be up to 35 characters long. Letters must be lowercase.
22 lines
934 B
22 lines
934 B
from drf_yasg.utils import swagger_auto_schema |
|
|
|
def add_swagger_summaries(viewset_class): |
|
"""Add standard swagger summaries to a ModelViewSet class""" |
|
model_name = viewset_class.serializer_class.Meta.model.__name__.lower() |
|
|
|
# Apply decorators to the class methods |
|
for action, template in { |
|
'list': f"List all {model_name}s", |
|
'create': f"Create new {model_name}", |
|
'retrieve': f"Get specific {model_name}", |
|
'update': f"Update {model_name} completely", |
|
'partial_update': f"Update {model_name} partially", |
|
'destroy': f"Delete {model_name}", |
|
}.items(): |
|
if hasattr(viewset_class, action): |
|
method = getattr(viewset_class, action) |
|
if not hasattr(method, '_swagger_auto_schema'): |
|
setattr(viewset_class, action, |
|
swagger_auto_schema(operation_summary=template)(method)) |
|
|
|
return viewset_class
|
|
|