Fix issubclass for view functions

This commit is contained in:
Sumanth Ratna 2020-12-31 13:56:56 -05:00
parent e7f6b71f75
commit ca555ea668
No known key found for this signature in database
GPG Key ID: 310949B7C8B60603

View File

@ -15,6 +15,7 @@ For example, you might have a `urls.py` that looks something like this:
""" """
import itertools import itertools
from collections import OrderedDict, namedtuple from collections import OrderedDict, namedtuple
from inspect import isclass
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.urls import NoReverseMatch, re_path, path from django.urls import NoReverseMatch, re_path, path
@ -150,11 +151,13 @@ class SimpleRouter(BaseRouter):
""" """
# use `issubclass` and not `isinstance` because `viewset` may be an # use `issubclass` and not `isinstance` because `viewset` may be an
# uninstantiated class. # uninstantiated class.
if not issubclass(viewset, ViewSetMixin): is_viewset = issubclass(viewset, ViewSetMixin) if isclass(viewset) else False
if issubclass(viewset, View): if not is_viewset:
return [viewset.as_view(), ]
# `viewset` is not a REST Framework ViewSet, # `viewset` is not a REST Framework ViewSet,
# so we can't dynamically generate any routes # so we can't dynamically generate any routes
is_cbv = issubclass(viewset, View) if isclass(viewset) else False
if is_cbv:
return [viewset.as_view(), ]
return [viewset, ] return [viewset, ]
# converting to list as iterables are good for one pass, known host needs to be checked again and again for # converting to list as iterables are good for one pass, known host needs to be checked again and again for