Move guardian imports out of compat (#6054)

This commit is contained in:
Ryan P Kilby 2018-07-06 05:32:02 -04:00 committed by Carlton Gibson
parent 99ca078ebb
commit 9b8af04e7f
3 changed files with 13 additions and 13 deletions

View File

@ -139,14 +139,11 @@ except ImportError:
requests = None requests = None
# Django-guardian is optional. Import only if guardian is in INSTALLED_APPS def is_guardian_installed():
# Fixes (#1712). We keep the try/except for the test suite. """
guardian = None django-guardian is optional and only imported if in INSTALLED_APPS.
try: """
if 'guardian' in settings.INSTALLED_APPS: return 'guardian' in settings.INSTALLED_APPS
import guardian # noqa
except ImportError:
pass
# PATCH method is not implemented by Django # PATCH method is not implemented by Django

View File

@ -16,7 +16,9 @@ from django.utils import six
from django.utils.encoding import force_text from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from rest_framework.compat import coreapi, coreschema, distinct, guardian from rest_framework.compat import (
coreapi, coreschema, distinct, is_guardian_installed
)
from rest_framework.settings import api_settings from rest_framework.settings import api_settings
@ -282,7 +284,7 @@ class DjangoObjectPermissionsFilter(BaseFilterBackend):
has read object level permissions. has read object level permissions.
""" """
def __init__(self): def __init__(self):
assert guardian, 'Using DjangoObjectPermissionsFilter, but django-guardian is not installed' assert is_guardian_installed(), 'Using DjangoObjectPermissionsFilter, but django-guardian is not installed'
perm_format = '%(app_label)s.view_%(model_name)s' perm_format = '%(app_label)s.view_%(model_name)s'
@ -290,6 +292,7 @@ class DjangoObjectPermissionsFilter(BaseFilterBackend):
# We want to defer this import until run-time, rather than import-time. # We want to defer this import until run-time, rather than import-time.
# See https://github.com/encode/django-rest-framework/issues/4608 # See https://github.com/encode/django-rest-framework/issues/4608
# (Also see #1624 for why we need to make this import explicitly) # (Also see #1624 for why we need to make this import explicitly)
from guardian import VERSION as guardian_version
from guardian.shortcuts import get_objects_for_user from guardian.shortcuts import get_objects_for_user
extra = {} extra = {}
@ -300,7 +303,7 @@ class DjangoObjectPermissionsFilter(BaseFilterBackend):
'model_name': model_cls._meta.model_name 'model_name': model_cls._meta.model_name
} }
permission = self.perm_format % kwargs permission = self.perm_format % kwargs
if tuple(guardian.VERSION) >= (1, 3): if tuple(guardian_version) >= (1, 3):
# Maintain behavior compatibility with versions prior to 1.3 # Maintain behavior compatibility with versions prior to 1.3
extra = {'accept_global_perms': False} extra = {'accept_global_perms': False}
else: else:

View File

@ -13,7 +13,7 @@ from rest_framework import (
HTTP_HEADER_ENCODING, authentication, generics, permissions, serializers, HTTP_HEADER_ENCODING, authentication, generics, permissions, serializers,
status, views status, views
) )
from rest_framework.compat import guardian from rest_framework.compat import is_guardian_installed
from rest_framework.filters import DjangoObjectPermissionsFilter from rest_framework.filters import DjangoObjectPermissionsFilter
from rest_framework.routers import DefaultRouter from rest_framework.routers import DefaultRouter
from rest_framework.test import APIRequestFactory from rest_framework.test import APIRequestFactory
@ -308,7 +308,7 @@ class GetQuerysetObjectPermissionInstanceView(generics.RetrieveUpdateDestroyAPIV
get_queryset_object_permissions_view = GetQuerysetObjectPermissionInstanceView.as_view() get_queryset_object_permissions_view = GetQuerysetObjectPermissionInstanceView.as_view()
@unittest.skipUnless(guardian, 'django-guardian not installed') @unittest.skipUnless(is_guardian_installed(), 'django-guardian not installed')
class ObjectPermissionsIntegrationTests(TestCase): class ObjectPermissionsIntegrationTests(TestCase):
""" """
Integration tests for the object level permissions API. Integration tests for the object level permissions API.