From 304befb93acb62f3c341fa75225813e40bcc924e Mon Sep 17 00:00:00 2001 From: Ryan P Kilby Date: Fri, 22 Jun 2018 19:06:50 -0400 Subject: [PATCH] Move guardian imports out of compat --- rest_framework/compat.py | 13 +++++-------- rest_framework/filters.py | 9 ++++++--- tests/test_permissions.py | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/rest_framework/compat.py b/rest_framework/compat.py index 7783bced3e..dacc1ac8a7 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -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 diff --git a/rest_framework/filters.py b/rest_framework/filters.py index cf8f6d1f79..ab2ce68b40 100644 --- a/rest_framework/filters.py +++ b/rest_framework/filters.py @@ -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 @@ -282,7 +284,7 @@ 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' @@ -290,6 +292,7 @@ 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 = {} @@ -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: diff --git a/tests/test_permissions.py b/tests/test_permissions.py index 80b666180a..2228e2d297 100644 --- a/tests/test_permissions.py +++ b/tests/test_permissions.py @@ -12,7 +12,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 @@ -305,7 +305,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. 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