-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Fix handling of optional guardian module #6038
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
Changes from all commits
327028c
d77784f
0537c14
c0c6ca0
f913130
0fc0ee1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,8 @@ matrix: | |
- { python: "3.6", env: DJANGO=master } | ||
- { python: "3.6", env: DJANGO=1.11 } | ||
- { python: "3.6", env: DJANGO=2.0 } | ||
- { python: "3.6", env: DJANGO=2.0 } | ||
- { python: "3.6", env: TOXENV=py36-django20-optionals } | ||
- { python: "3.6", env: DJANGO=2.1 } | ||
- { python: "2.7", env: TOXENV=lint } | ||
- { python: "2.7", env: TOXENV=docs } | ||
|
@@ -32,6 +34,13 @@ matrix: | |
- tox --installpkg ./dist/djangorestframework-*.whl | ||
- tox # test sdist | ||
|
||
- python: "3.6" | ||
env: TOXENV=dist-optionals | ||
script: | ||
- python setup.py bdist_wheel | ||
- tox --installpkg ./dist/djangorestframework-*.whl | ||
- tox # test sdist | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These changes don't immediately appear connected to the rest of the PR - let's drop them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They are necessary to cover the change in the tests, but I can move that into a separate PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. => #6041 |
||
exclude: | ||
- { python: "2.7", env: DJANGO=master } | ||
- { python: "2.7", env: DJANGO=2.0 } | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -139,16 +139,6 @@ 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would regress #1712 then. |
||
import guardian # noqa | ||
except ImportError: | ||
pass | ||
|
||
|
||
# PATCH method is not implemented by Django | ||
if 'patch' not in View.http_method_names: | ||
View.http_method_names = View.http_method_names + ['patch'] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
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 | ||
from rest_framework.settings import api_settings | ||
|
||
|
||
|
@@ -282,7 +282,11 @@ class DjangoObjectPermissionsFilter(BaseFilterBackend): | |
has read object level permissions. | ||
""" | ||
def __init__(self): | ||
assert guardian, 'Using DjangoObjectPermissionsFilter, but django-guardian is not installed' | ||
try: | ||
import guardian | ||
except ImportError: | ||
raise ImportError('Using DjangoObjectPermissionsFilter, but django-guardian is not installed') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think changing this to an ImportError instead of AssertionError is fine?! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably use a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could also include a comment linking to the issue. (As we do in |
||
self.guardian_version = guardian.VERSION | ||
|
||
perm_format = '%(app_label)s.view_%(model_name)s' | ||
|
||
|
@@ -300,7 +304,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(self.guardian_version) >= (1, 3): | ||
# Maintain behavior compatibility with versions prior to 1.3 | ||
extra = {'accept_global_perms': False} | ||
else: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This env isn't listed in tox?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does not have to be (AFAIK).
Or at least I've assumed that tox-travis will use the explicit env, but haven't checked the build job itself.
(tox itself allows for
tox -e dist-optionals
)