From 64d1f572766cbd6c81f3131d25a8f562d7a894ea Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Wed, 11 Nov 2020 14:25:43 -0500 Subject: [PATCH] Add custom get_operation_id() method to avoid monkey-patching coreapi --- netbox/netbox/api/__init__.py | 15 --------------- netbox/utilities/custom_inspectors.py | 12 ++++++++++++ 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/netbox/netbox/api/__init__.py b/netbox/netbox/api/__init__.py index afb2a6803..78ab7431d 100644 --- a/netbox/netbox/api/__init__.py +++ b/netbox/netbox/api/__init__.py @@ -1,5 +1,3 @@ -from rest_framework.schemas import coreapi - from .fields import ChoiceField, ContentTypeField, SerializedPKRelatedField, TimeZoneField from .routers import OrderedDefaultRouter from .serializers import BulkOperationSerializer, ValidatedModelSerializer, WritableNestedSerializer @@ -15,16 +13,3 @@ __all__ = ( 'ValidatedModelSerializer', 'WritableNestedSerializer', ) - - -def is_custom_action(action): - return action not in { - # Default actions - 'retrieve', 'list', 'create', 'update', 'partial_update', 'destroy', - # Bulk operations - 'bulk_update', 'bulk_partial_update', 'bulk_destroy', - } - - -# Monkey-patch DRF to treat bulk_destroy() as a non-custom action (see #3436) -coreapi.is_custom_action = is_custom_action diff --git a/netbox/utilities/custom_inspectors.py b/netbox/utilities/custom_inspectors.py index 1b931155f..95c647fb8 100644 --- a/netbox/utilities/custom_inspectors.py +++ b/netbox/utilities/custom_inspectors.py @@ -12,6 +12,18 @@ from netbox.api import ChoiceField, SerializedPKRelatedField, WritableNestedSeri class NetBoxSwaggerAutoSchema(SwaggerAutoSchema): writable_serializers = {} + def get_operation_id(self, operation_keys=None): + operation_keys = operation_keys or self.operation_keys + operation_id = self.overrides.get('operation_id', '') + if not operation_id: + # Overwrite the action for bulk update/bulk delete views to ensure they get an operation ID that's + # unique from their single-object counterparts (see #3436) + if operation_keys[-1] in ('delete', 'partial_update', 'update') and not self.view.detail: + operation_keys[-1] = f'bulk_{operation_keys[-1]}' + operation_id = '_'.join(operation_keys) + + return operation_id + def get_request_serializer(self): serializer = super().get_request_serializer()