updated description on viewset

This commit is contained in:
Abhimanyu Saharan 2023-04-10 21:54:36 +05:30
parent b868a47ec6
commit be81143a26

View File

@ -233,31 +233,32 @@ class NetBoxAutoSchema(AutoSchema):
if self.view.__doc__: if self.view.__doc__:
return get_doc(self.view.__class__) return get_doc(self.view.__class__)
# When the action method is decorated with @action, use the docstring # When the action method is decorated with @action, use the docstring of the method.
# of the method.
action_or_method = getattr(self.view, getattr(self.view, 'action', self.method.lower()), None) action_or_method = getattr(self.view, getattr(self.view, 'action', self.method.lower()), None)
if action_or_method and action_or_method.__doc__: if action_or_method and action_or_method.__doc__:
return get_doc(action_or_method) return get_doc(action_or_method)
# Else, generate a description from the class name. # 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.
"""
# Determine if the method is for list or detail.
if self.is_bulk_action:
is_list = True
# Determine using the URL path if the method is for list or detail.
elif '{id}' in self.path:
is_list = False
else:
is_list = True
description = self.view.__class__.__name__ description = self.view.__class__.__name__
if description.endswith('ViewSet'): if description.endswith('ViewSet'):
description = description[:-len('ViewSet')] description = description[:-len('ViewSet')]
description = re.sub(r'(?<!^)(?=[A-Z])', ' ', description) if is_list:
description = description[0].lower() + description[1:] return f"{self.method.capitalize()} a list of {description} objects."
return f"{self.method.capitalize()} a {description} object."
if self.method == 'GET':
description = f"Get a list of {description} objects."
elif self.method == 'POST':
description = f"Create a {description} object."
elif self.method == 'PUT':
description = f"Update a {description} object."
elif self.method == 'PATCH':
description = f"Update a {description} object."
elif self.method == 'DELETE':
description = f"Delete a {description} object."
else:
description = f"Perform an action on a {description} object."
return description