Skip to content

Commit

Permalink
Move guardian imports out of compat (#6054)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan P Kilby authored and carltongibson committed Jul 6, 2018
1 parent 99ca078 commit 9b8af04
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
13 changes: 5 additions & 8 deletions rest_framework/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,11 @@ def distinct(queryset, base):
requests = None


# Django-guardian is optional. Import only if guardian is in INSTALLED_APPS
# Fixes (#1712). We keep the try/except for the test suite.
guardian = None
try:
if 'guardian' in settings.INSTALLED_APPS:
import guardian # noqa
except ImportError:
pass
def is_guardian_installed():
"""
django-guardian is optional and only imported if in INSTALLED_APPS.
"""
return 'guardian' in settings.INSTALLED_APPS


# PATCH method is not implemented by Django
Expand Down
9 changes: 6 additions & 3 deletions rest_framework/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
from django.utils.encoding import force_text
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


Expand Down Expand Up @@ -282,14 +284,15 @@ class DjangoObjectPermissionsFilter(BaseFilterBackend):
has read object level permissions.
"""
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'

def filter_queryset(self, request, queryset, view):
# We want to defer this import until run-time, rather than import-time.
# See https://github.com/encode/django-rest-framework/issues/4608
# (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

extra = {}
Expand All @@ -300,7 +303,7 @@ def filter_queryset(self, request, queryset, view):
'model_name': model_cls._meta.model_name
}
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
extra = {'accept_global_perms': False}
else:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
HTTP_HEADER_ENCODING, authentication, generics, permissions, serializers,
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.routers import DefaultRouter
from rest_framework.test import APIRequestFactory
Expand Down Expand Up @@ -308,7 +308,7 @@ def get_queryset(self):
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):
"""
Integration tests for the object level permissions API.
Expand Down

0 comments on commit 9b8af04

Please sign in to comment.