From ab1a96deded1b7a282df8227edb07ac12bc69420 Mon Sep 17 00:00:00 2001 From: Abhimanyu Saharan Date: Mon, 10 Apr 2023 21:10:57 +0530 Subject: [PATCH 1/6] wip --- netbox/core/api/schema.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/netbox/core/api/schema.py b/netbox/core/api/schema.py index 0b44a3d520..203d989221 100644 --- a/netbox/core/api/schema.py +++ b/netbox/core/api/schema.py @@ -12,6 +12,7 @@ build_basic_type, build_media_type_object, build_object_type, + get_doc, is_serializer, ) from drf_spectacular.types import OpenApiTypes @@ -222,3 +223,35 @@ 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 self.view.__doc__ + + # Else, generate a description from the class name. + description = self.view.__class__.__name__ + if description.endswith('ViewSet'): + description = description[:-len('ViewSet')] + + description = re.sub(r'(? Date: Mon, 10 Apr 2023 21:33:16 +0530 Subject: [PATCH 2/6] wip --- netbox/core/api/schema.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/netbox/core/api/schema.py b/netbox/core/api/schema.py index 203d989221..f3a5d6a709 100644 --- a/netbox/core/api/schema.py +++ b/netbox/core/api/schema.py @@ -231,7 +231,13 @@ def get_description(self): # If a docstring is provided, use it. if self.view.__doc__: - return 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. description = self.view.__class__.__name__ From be81143a2605873a74a8bdd3d83861935b079efb Mon Sep 17 00:00:00 2001 From: Abhimanyu Saharan Date: Mon, 10 Apr 2023 21:54:36 +0530 Subject: [PATCH 3/6] updated description on viewset --- netbox/core/api/schema.py | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/netbox/core/api/schema.py b/netbox/core/api/schema.py index f3a5d6a709..cada59c644 100644 --- a/netbox/core/api/schema.py +++ b/netbox/core/api/schema.py @@ -233,31 +233,32 @@ def get_description(self): if self.view.__doc__: return get_doc(self.view.__class__) - # When the action method is decorated with @action, use the docstring - # of the method. + # 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. + """ + + # 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__ if description.endswith('ViewSet'): description = description[:-len('ViewSet')] - description = re.sub(r'(? Date: Mon, 10 Apr 2023 22:06:35 +0530 Subject: [PATCH 4/6] fixed model name --- netbox/core/api/schema.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/netbox/core/api/schema.py b/netbox/core/api/schema.py index cada59c644..ed10c6cf50 100644 --- a/netbox/core/api/schema.py +++ b/netbox/core/api/schema.py @@ -255,10 +255,8 @@ def _generate_description(self): else: is_list = True - description = self.view.__class__.__name__ - if description.endswith('ViewSet'): - description = description[:-len('ViewSet')] + model_name = self.view.queryset.model._meta.verbose_name if is_list: - return f"{self.method.capitalize()} a list of {description} objects." - return f"{self.method.capitalize()} a {description} object." + return f"{self.method.capitalize()} a list of {model_name} objects." + return f"{self.method.capitalize()} a {model_name} object." From 3808a2735f41f9ce3543889f12ecbdeffcbf3258 Mon Sep 17 00:00:00 2001 From: Abhimanyu Saharan Date: Wed, 12 Apr 2023 12:42:20 +0530 Subject: [PATCH 5/6] Update schema.py --- netbox/core/api/schema.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/netbox/core/api/schema.py b/netbox/core/api/schema.py index ed10c6cf50..7c5fb1ac79 100644 --- a/netbox/core/api/schema.py +++ b/netbox/core/api/schema.py @@ -247,13 +247,7 @@ def _generate_description(self): """ # 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 + is_list = not '{id}' in self.path model_name = self.view.queryset.model._meta.verbose_name From 5437f57ef2c4b3c08861a63457c492f391c0e45d Mon Sep 17 00:00:00 2001 From: Abhimanyu Saharan Date: Wed, 12 Apr 2023 12:45:38 +0530 Subject: [PATCH 6/6] Update schema.py --- netbox/core/api/schema.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/netbox/core/api/schema.py b/netbox/core/api/schema.py index 7c5fb1ac79..8bba8fec57 100644 --- a/netbox/core/api/schema.py +++ b/netbox/core/api/schema.py @@ -245,12 +245,9 @@ 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. - is_list = not '{id}' in self.path - model_name = self.view.queryset.model._meta.verbose_name - if is_list: - return f"{self.method.capitalize()} a list of {model_name} objects." - return f"{self.method.capitalize()} a {model_name} object." + # 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."