Rename view_func.cls/initkwargs to view_class/view_initkwargs, fixes #7036

This commit is contained in:
Jan Pieter Waagmeester 2019-11-04 15:30:22 +01:00
parent 4137ef41ef
commit ca9cc66d2a
7 changed files with 24 additions and 24 deletions

View File

@ -29,8 +29,8 @@ def is_api_view(callback):
""" """
# Avoid import cycle on APIView # Avoid import cycle on APIView
from rest_framework.views import APIView from rest_framework.views import APIView
cls = getattr(callback, 'cls', None) view_class = getattr(callback, 'view_class', None)
return (cls is not None) and issubclass(cls, APIView) return (view_class is not None) and issubclass(view_class, APIView)
def endpoint_ordering(endpoint): def endpoint_ordering(endpoint):
@ -117,11 +117,11 @@ class EndpointEnumerator:
if not is_api_view(callback): if not is_api_view(callback):
return False # Ignore anything except REST framework views. return False # Ignore anything except REST framework views.
if callback.cls.schema is None: if callback.view_class.schema is None:
return False return False
if 'schema' in callback.initkwargs: if 'schema' in callback.view_initkwargs:
if callback.initkwargs['schema'] is None: if callback.view_initkwargs['schema'] is None:
return False return False
if path.endswith('.{format}') or path.endswith('.{format}/'): if path.endswith('.{format}') or path.endswith('.{format}/'):
@ -135,10 +135,10 @@ class EndpointEnumerator:
""" """
if hasattr(callback, 'actions'): if hasattr(callback, 'actions'):
actions = set(callback.actions) actions = set(callback.actions)
http_method_names = set(callback.cls.http_method_names) http_method_names = set(callback.view_class.http_method_names)
methods = [method.upper() for method in actions & http_method_names] methods = [method.upper() for method in actions & http_method_names]
else: else:
methods = callback.cls().allowed_methods methods = callback.view_class().allowed_methods
return [method for method in methods if method not in ('OPTIONS', 'HEAD')] return [method for method in methods if method not in ('OPTIONS', 'HEAD')]
@ -188,7 +188,7 @@ class BaseSchemaGenerator(object):
""" """
Given a callback, return an actual view instance. Given a callback, return an actual view instance.
""" """
view = callback.cls(**getattr(callback, 'initkwargs', {})) view = callback.view_class(**getattr(callback, 'view_initkwargs', {}))
view.args = () view.args = ()
view.kwargs = {} view.kwargs = {}
view.format_kwarg = None view.format_kwarg = None

View File

@ -21,13 +21,13 @@ def get_breadcrumbs(url, request=None):
else: else:
# Check if this is a REST framework view, # Check if this is a REST framework view,
# and if so add it to the breadcrumbs # and if so add it to the breadcrumbs
cls = getattr(view, 'cls', None) view_class = getattr(view, 'view_class', None)
initkwargs = getattr(view, 'initkwargs', {}) initkwargs = getattr(view, 'view_initkwargs', {})
if cls is not None and issubclass(cls, APIView): if view_class is not None and issubclass(view_class, APIView):
# Don't list the same view twice in a row. # Don't list the same view twice in a row.
# Probably an optional trailing slash. # Probably an optional trailing slash.
if not seen or seen[-1] != view: if not seen or seen[-1] != view:
c = cls(**initkwargs) c = view_class(**initkwargs)
name = c.get_view_name() name = c.get_view_name()
insert_url = preserve_builtin_query_params(prefix + url, request) insert_url = preserve_builtin_query_params(prefix + url, request)
breadcrumbs_list.insert(0, (name, insert_url)) breadcrumbs_list.insert(0, (name, insert_url))

View File

@ -136,8 +136,8 @@ class APIView(View):
cls.queryset._fetch_all = force_evaluation cls.queryset._fetch_all = force_evaluation
view = super().as_view(**initkwargs) view = super().as_view(**initkwargs)
view.cls = cls view.view_class = cls
view.initkwargs = initkwargs view.view_initkwargs = initkwargs
# Note: session based authentication is explicitly CSRF validated, # Note: session based authentication is explicitly CSRF validated,
# all other authentication is CSRF exempt. # all other authentication is CSRF exempt.

View File

@ -123,8 +123,8 @@ class ViewSetMixin:
# We need to set these on the view function, so that breadcrumb # We need to set these on the view function, so that breadcrumb
# generation can pick out these bits of information from a # generation can pick out these bits of information from a
# resolved URL. # resolved URL.
view.cls = cls view.view_class = cls
view.initkwargs = initkwargs view.view_initkwargs = initkwargs
view.actions = actions view.actions = actions
return csrf_exempt(view) return csrf_exempt(view)

View File

@ -9,12 +9,12 @@ class GetSchemaViewTests(TestCase):
"""For the get_schema_view() helper.""" """For the get_schema_view() helper."""
def test_openapi(self): def test_openapi(self):
schema_view = get_schema_view(title="With OpenAPI") schema_view = get_schema_view(title="With OpenAPI")
assert isinstance(schema_view.initkwargs['schema_generator'], openapi.SchemaGenerator) assert isinstance(schema_view.view_initkwargs['schema_generator'], openapi.SchemaGenerator)
assert renderers.OpenAPIRenderer in schema_view.cls().renderer_classes assert renderers.OpenAPIRenderer in schema_view.view_class().renderer_classes
@pytest.mark.skipif(not coreapi.coreapi, reason='coreapi is not installed') @pytest.mark.skipif(not coreapi.coreapi, reason='coreapi is not installed')
def test_coreapi(self): def test_coreapi(self):
with override_settings(REST_FRAMEWORK={'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.AutoSchema'}): with override_settings(REST_FRAMEWORK={'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.AutoSchema'}):
schema_view = get_schema_view(title="With CoreAPI") schema_view = get_schema_view(title="With CoreAPI")
assert isinstance(schema_view.initkwargs['schema_generator'], coreapi.SchemaGenerator) assert isinstance(schema_view.view_initkwargs['schema_generator'], coreapi.SchemaGenerator)
assert renderers.CoreAPIOpenAPIRenderer in schema_view.cls().renderer_classes assert renderers.CoreAPIOpenAPIRenderer in schema_view.view_class().renderer_classes

View File

@ -164,7 +164,7 @@ class DecoratorTestCase(TestCase):
def view(request): def view(request):
return Response({}) return Response({})
assert isinstance(view.cls.schema, CustomSchema) assert isinstance(view.view_class.schema, CustomSchema)
class ActionDecoratorTestCase(TestCase): class ActionDecoratorTestCase(TestCase):

View File

@ -470,18 +470,18 @@ class TestViewInitkwargs(URLPatternsTestCase, TestCase):
def test_suffix(self): def test_suffix(self):
match = resolve('/example/notes/') match = resolve('/example/notes/')
initkwargs = match.func.initkwargs initkwargs = match.func.view_initkwargs
assert initkwargs['suffix'] == 'List' assert initkwargs['suffix'] == 'List'
def test_detail(self): def test_detail(self):
match = resolve('/example/notes/') match = resolve('/example/notes/')
initkwargs = match.func.initkwargs initkwargs = match.func.view_initkwargs
assert not initkwargs['detail'] assert not initkwargs['detail']
def test_basename(self): def test_basename(self):
match = resolve('/example/notes/') match = resolve('/example/notes/')
initkwargs = match.func.initkwargs initkwargs = match.func.view_initkwargs
assert initkwargs['basename'] == 'routertestmodel' assert initkwargs['basename'] == 'routertestmodel'