Skip to content

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

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

Closed
Show file tree
Hide file tree
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
9 changes: 6 additions & 3 deletions rest_framework/schemas/coreapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

from .generators import BaseSchemaGenerator
from .inspectors import ViewInspector
from .utils import get_pk_description, is_list_view
from .utils import (
ALLOW_FILTER_ACTIONS, ALLOW_FILTER_METHODS, get_pk_description,
is_list_view
)


def common_path(paths):
Expand Down Expand Up @@ -522,9 +525,9 @@ def _allows_filters(self, path, method):
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):
Expand Down
9 changes: 6 additions & 3 deletions rest_framework/schemas/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

from .generators import BaseSchemaGenerator
from .inspectors import ViewInspector
from .utils import get_pk_description, is_list_view
from .utils import (
ALLOW_FILTER_ACTIONS, ALLOW_FILTER_METHODS, get_pk_description,
is_list_view
)


class SchemaGenerator(BaseSchemaGenerator):
Expand Down Expand Up @@ -320,8 +323,8 @@ def allows_filters(self, path, method):
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
Expand Down
4 changes: 4 additions & 0 deletions rest_framework/schemas/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ 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"}
Loading