Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added method to update viewset description #12218

Merged
merged 6 commits into from
Apr 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions netbox/core/api/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
build_basic_type,
build_media_type_object,
build_object_type,
get_doc,
is_serializer,
)
from drf_spectacular.types import OpenApiTypes
Expand Down Expand Up @@ -222,3 +223,31 @@ def _get_request_body(self, direction='request'):
if request_body_required:
request_body['required'] = request_body_required
return request_body

def get_description(self):
"""
Return a string description for the ViewSet.
"""

# If a docstring is provided, use it.
if self.view.__doc__:
return get_doc(self.view.__class__)

# When the action method is decorated with @action, use the docstring of the method.
action_or_method = getattr(self.view, getattr(self.view, 'action', self.method.lower()), None)
if action_or_method and action_or_method.__doc__:
return get_doc(action_or_method)

# Else, generate a description from the class name.
return self._generate_description()

def _generate_description(self):
"""
Generate a docstring for the method. It also takes into account whether the method is for list or detail.
"""
model_name = self.view.queryset.model._meta.verbose_name

# Determine if the method is for list or detail.
if '{id}' in self.path:
return f"{self.method.capitalize()} a {model_name} object."
return f"{self.method.capitalize()} a list of {model_name} objects."