abstract away allowed actions and methods, and use a set for faster membership checks

This commit is contained in:
Willem Van Onsem 2025-04-09 12:37:50 +02:00
parent ea1da76196
commit d3f8ae4a5d
No known key found for this signature in database
GPG Key ID: 5E85A680BCF54DCF
3 changed files with 9 additions and 6 deletions

View File

@ -11,7 +11,7 @@ from rest_framework.settings import api_settings
from .generators import BaseSchemaGenerator
from .inspectors import ViewInspector
from .utils import get_pk_description, is_list_view
from .utils import get_pk_description, is_list_view, ALLOW_FILTER_ACTIONS, ALLOW_FILTER_METHODS
def common_path(paths):
@ -522,9 +522,9 @@ class AutoSchema(ViewInspector):
return False
if hasattr(self.view, 'action'):
return self.view.action in ["list", "retrieve", "update", "partial_update", "destroy"]
return self.view.action in ALLOW_FILTER_ACTIONS
return method.lower() in ["get", "put", "patch", "delete"]
return method.lower() in ALLOW_FILTER_METHODS
def get_filter_fields(self, path, method):
if not self._allows_filters(path, method):

View File

@ -18,7 +18,7 @@ from rest_framework.settings import api_settings
from .generators import BaseSchemaGenerator
from .inspectors import ViewInspector
from .utils import get_pk_description, is_list_view
from .utils import get_pk_description, is_list_view, ALLOW_FILTER_ACTIONS, ALLOW_FILTER_METHODS
class SchemaGenerator(BaseSchemaGenerator):
@ -320,8 +320,8 @@ class AutoSchema(ViewInspector):
if getattr(self.view, 'filter_backends', None) is None:
return False
if hasattr(self.view, 'action'):
return self.view.action in ["list", "retrieve", "update", "partial_update", "destroy"]
return method.lower() in ["get", "put", "patch", "delete"]
return self.view.action in ALLOW_FILTER_ACTIONS
return method.lower() in ALLOW_FILTER_METHODS
def get_pagination_parameters(self, path, method):
view = self.view

View File

@ -39,3 +39,6 @@ def get_pk_description(model, model_field):
value_type=value_type,
name=model._meta.verbose_name,
)
ALLOW_FILTER_ACTIONS = {"list", "retrieve", "update", "partial_update", "destroy"}
ALLOW_FILTER_METHODS = {"get", "put", "patch", "delete"}