Skip to content

Commit 304befb

Browse files
Ryan P Kilbycarltongibson
authored andcommitted
Move guardian imports out of compat
1 parent 499533d commit 304befb

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

rest_framework/compat.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,11 @@ def distinct(queryset, base):
139139
requests = None
140140

141141

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

151148

152149
# PATCH method is not implemented by Django

rest_framework/filters.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
from django.utils.encoding import force_text
1717
from django.utils.translation import ugettext_lazy as _
1818

19-
from rest_framework.compat import coreapi, coreschema, distinct, guardian
19+
from rest_framework.compat import (
20+
coreapi, coreschema, distinct, is_guardian_installed
21+
)
2022
from rest_framework.settings import api_settings
2123

2224

@@ -282,14 +284,15 @@ class DjangoObjectPermissionsFilter(BaseFilterBackend):
282284
has read object level permissions.
283285
"""
284286
def __init__(self):
285-
assert guardian, 'Using DjangoObjectPermissionsFilter, but django-guardian is not installed'
287+
assert is_guardian_installed(), 'Using DjangoObjectPermissionsFilter, but django-guardian is not installed'
286288

287289
perm_format = '%(app_label)s.view_%(model_name)s'
288290

289291
def filter_queryset(self, request, queryset, view):
290292
# We want to defer this import until run-time, rather than import-time.
291293
# See https://github.com/encode/django-rest-framework/issues/4608
292294
# (Also see #1624 for why we need to make this import explicitly)
295+
from guardian import VERSION as guardian_version
293296
from guardian.shortcuts import get_objects_for_user
294297

295298
extra = {}
@@ -300,7 +303,7 @@ def filter_queryset(self, request, queryset, view):
300303
'model_name': model_cls._meta.model_name
301304
}
302305
permission = self.perm_format % kwargs
303-
if tuple(guardian.VERSION) >= (1, 3):
306+
if tuple(guardian_version) >= (1, 3):
304307
# Maintain behavior compatibility with versions prior to 1.3
305308
extra = {'accept_global_perms': False}
306309
else:

tests/test_permissions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
HTTP_HEADER_ENCODING, authentication, generics, permissions, serializers,
1313
status, views
1414
)
15-
from rest_framework.compat import guardian
15+
from rest_framework.compat import is_guardian_installed
1616
from rest_framework.filters import DjangoObjectPermissionsFilter
1717
from rest_framework.routers import DefaultRouter
1818
from rest_framework.test import APIRequestFactory
@@ -305,7 +305,7 @@ def get_queryset(self):
305305
get_queryset_object_permissions_view = GetQuerysetObjectPermissionInstanceView.as_view()
306306

307307

308-
@unittest.skipUnless(guardian, 'django-guardian not installed')
308+
@unittest.skipUnless(is_guardian_installed(), 'django-guardian not installed')
309309
class ObjectPermissionsIntegrationTests(TestCase):
310310
"""
311311
Integration tests for the object level permissions API.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy