Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix guardian import #4612

Merged
merged 2 commits into from
Oct 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion rest_framework/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ def value_from_object(field, obj):
try:
if 'guardian' in settings.INSTALLED_APPS:
import guardian
import guardian.shortcuts # Fixes #1624
except ImportError:
pass

Expand Down
7 changes: 6 additions & 1 deletion rest_framework/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ def __init__(self):
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/tomchristie/django-rest-framework/issues/4608
# (Also see #1624 for why we need to make this import explicitly)
from guardian.shortcuts import get_objects_for_user

extra = {}
user = request.user
model_cls = queryset.model
Expand All @@ -302,4 +307,4 @@ def filter_queryset(self, request, queryset, view):
extra = {'accept_global_perms': False}
else:
extra = {}
return guardian.shortcuts.get_objects_for_user(user, permission, queryset, **extra)
return get_objects_for_user(user, permission, queryset, **extra)
15 changes: 13 additions & 2 deletions rest_framework/schemas.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import re
from collections import OrderedDict
from importlib import import_module
Expand Down Expand Up @@ -37,6 +36,18 @@
})


def common_path(paths):
split_paths = [path.strip('/').split('/') for path in paths]
s1 = min(split_paths)
s2 = max(split_paths)
common = s1
for i, c in enumerate(s1):
if c != s2[i]:
common = s1[:i]
break
return '/' + '/'.join(common)


def get_pk_name(model):
meta = model._meta.concrete_model._meta
return _get_pk(meta).name
Expand Down Expand Up @@ -292,7 +303,7 @@ def determine_path_prefix(self, paths):
# one URL that doesn't have a path prefix.
return '/'
prefixes.append('/' + prefix + '/')
return os.path.commonprefix(prefixes)
return common_path(prefixes)

def create_view(self, callback, method, request=None):
"""
Expand Down
11 changes: 11 additions & 0 deletions tests/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,14 @@ def test_schema_for_regular_views(self):
}
)
self.assertEqual(schema, expected)


@unittest.skipUnless(coreapi, 'coreapi is not installed')
class Test4605Regression(TestCase):
def test_4605_regression(self):
generator = SchemaGenerator()
prefix = generator.determine_path_prefix([
'/api/v1/items/',
'/auth/convert-token/'
])
assert prefix == '/'