Skip to content

Commit

Permalink
Add support for Django 3.0 (#667)
Browse files Browse the repository at this point in the history
Co-authored-by: Samiul Sk <sam91v@gmail.com>
Co-authored-by: Asif Saif Uddin <auvipy@gmail.com>
  • Loading branch information
3 people committed Dec 24, 2019
1 parent 9048264 commit febb63e
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 23 deletions.
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ notifications:
templates:
django21: &django21 DJANGO_VERSION=2.1.*
django22: &django22 DJANGO_VERSION=2.2.*
django30: &django30 DJANGO_VERSION=3.0.*
djangomaster: &djangomaster DJANGO_VERSION=master

postgres: &postgres DATABASE_URL=postgres://postgres@/django_guardian
Expand Down Expand Up @@ -43,6 +44,9 @@ matrix:
- { python: 3.6, env: [*django22, *postgres], <<: *pgdb}
- { python: 3.6, env: [*django22, *mysql], <<: *mariadb}
- { python: 3.6, env: [*django22, *sqlite]}
- { python: 3.6, env: [*django30, *postgres], <<: *pgdb}
- { python: 3.6, env: [*django30, *mysql], <<: *mariadb}
- { python: 3.6, env: [*django30, *sqlite]}
- { python: 3.6, env: [*djangomaster, *postgres], <<: *pgdb}
- { python: 3.6, env: [*djangomaster, *mysql], <<: *mariadb}
- { python: 3.6, env: [*djangomaster, *sqlite]}
Expand All @@ -53,13 +57,19 @@ matrix:
- { python: 3.7, env: [*django22, *postgres], <<: *pgdb}
- { python: 3.7, env: [*django22, *mysql], <<: *mariadb}
- { python: 3.7, env: [*django22, *sqlite]}
- { python: 3.7, env: [*django30, *postgres], <<: *pgdb}
- { python: 3.7, env: [*django30, *mysql], <<: *mariadb}
- { python: 3.7, env: [*django30, *sqlite]}
- { python: 3.7, env: [*djangomaster, *postgres], <<: *pgdb}
- { python: 3.7, env: [*djangomaster, *mysql], <<: *mariadb}
- { python: 3.7, env: [*djangomaster, *sqlite]}

- { python: 3.8, env: [*django22, *postgres], <<: *pgdb}
- { python: 3.8, env: [*django22, *mysql], <<: *mariadb}
- { python: 3.8, env: [*django22, *sqlite]}
- { python: 3.8, env: [*django30, *postgres], <<: *pgdb}
- { python: 3.8, env: [*django30, *mysql], <<: *mariadb}
- { python: 3.8, env: [*django30, *sqlite]}
- { python: 3.8, env: [*djangomaster, *postgres], <<: *pgdb}
- { python: 3.8, env: [*djangomaster, *mysql], <<: *mariadb}
- { python: 3.8, env: [*djangomaster, *sqlite]}
Expand Down
14 changes: 5 additions & 9 deletions contrib/travis/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ find . -name '*.py' | xargs pyupgrade --py3-only
python ./setup.py --version
py.test --cov=guardian

# Test example_project on supported django versions
if [ "${DJANGO_VERSION:0:3}" = "2.1" ] || \
[ "${DJANGO_VERSION:0:3}" = "2.2" ] || \
[ "$DJANGO_VERSION" = "master" ]; then
pip install .;
cd example_project;
python -Wa manage.py makemigrations --check --dry-run;
python -Wa manage.py test;
fi;
# Test example_project
pip install .;
cd example_project;
python -Wa manage.py makemigrations --check --dry-run;
python -Wa manage.py test;
8 changes: 4 additions & 4 deletions guardian/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from django.contrib.auth import get_user_model
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse, path
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import ugettext
from django.utils.translation import gettext_lazy as _
from django.utils.translation import gettext
from guardian.forms import GroupObjectPermissionsForm, UserObjectPermissionsForm
from django.contrib.auth.models import Group
from guardian.shortcuts import (get_group_perms, get_groups_with_perms, get_perms_for_model, get_user_perms,
Expand Down Expand Up @@ -234,7 +234,7 @@ def obj_perms_manage_user_view(self, request, object_pk, user_id):

if request.method == 'POST' and form.is_valid():
form.save_obj_perms()
msg = ugettext("Permissions saved.")
msg = gettext("Permissions saved.")
messages.success(request, msg)
info = (
self.admin_site.name,
Expand Down Expand Up @@ -306,7 +306,7 @@ def obj_perms_manage_group_view(self, request, object_pk, group_id):

if request.method == 'POST' and form.is_valid():
form.save_obj_perms()
msg = ugettext("Permissions saved.")
msg = gettext("Permissions saved.")
messages.success(request, msg)
info = (
self.admin_site.name,
Expand Down
12 changes: 6 additions & 6 deletions guardian/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
from django.db.models.query import QuerySet
from django.utils.encoding import force_text
from django.utils.encoding import force_str

from guardian.conf import settings as guardian_settings
from guardian.ctypes import get_content_type
Expand All @@ -18,15 +18,15 @@ def _get_pks_model_and_ctype(objects):

if isinstance(objects, QuerySet):
model = objects.model
pks = [force_text(pk) for pk in objects.values_list('pk', flat=True)]
pks = [force_str(pk) for pk in objects.values_list('pk', flat=True)]
ctype = get_content_type(model)
else:
pks = []
for idx, obj in enumerate(objects):
if not idx:
model = type(obj)
ctype = get_content_type(model)
pks.append(force_text(obj.pk))
pks.append(force_str(obj.pk))

return pks, model, ctype

Expand Down Expand Up @@ -176,7 +176,7 @@ def get_local_cache_key(self, obj):
Returns cache key for ``_obj_perms_cache`` dict.
"""
ctype = get_content_type(obj)
return (ctype.id, force_text(obj.pk))
return (ctype.id, force_str(obj.pk))

def prefetch_perms(self, objects):
"""
Expand All @@ -198,7 +198,7 @@ def prefetch_perms(self, objects):
.values_list("codename")))

for pk in pks:
key = (ctype.id, force_text(pk))
key = (ctype.id, force_str(pk))
self._obj_perms_cache[key] = perms

return True
Expand Down Expand Up @@ -259,7 +259,7 @@ def prefetch_perms(self, objects):
if type(perm).objects.is_generic():
key = (ctype.id, perm.object_pk)
else:
key = (ctype.id, force_text(perm.content_object_id))
key = (ctype.id, force_str(perm.content_object_id))

self._obj_perms_cache[key].append(perm.permission.codename)

Expand Down
2 changes: 1 addition & 1 deletion guardian/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django import forms
from django.utils.translation import ugettext as _
from django.utils.translation import gettext as _
from guardian.shortcuts import assign_perm, get_group_perms, get_perms_for_model, get_user_perms, remove_perm


Expand Down
10 changes: 9 additions & 1 deletion guardian/testapp/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import copy
import os
import unittest

from django import forms
from django import VERSION as DJANGO_VERSION, forms
from django.conf import settings
from django.contrib import admin
from django.contrib.auth import get_user_model
Expand Down Expand Up @@ -83,6 +85,9 @@ def test_view_manage_user_form(self):
'user_id': self.user.pk})
self.assertEqual(response.request['PATH_INFO'], redirect_url)

@unittest.skipIf(DJANGO_VERSION >= (3, 0) and
"mysql" in os.environ.get("DATABASE_URL", ""),
"Negative ids no longer work in Django 3.0+ with MySQL.")
def test_view_manage_negative_user_form(self):
self._login_superuser()
url = reverse('admin:%s_%s_permissions' % self.obj_info,
Expand Down Expand Up @@ -187,6 +192,9 @@ def test_view_manage_group_form(self):
self.obj_info, args=[self.obj.pk, self.group.id])
self.assertEqual(response.request['PATH_INFO'], redirect_url)

@unittest.skipIf(DJANGO_VERSION >= (3, 0) and
"mysql" in os.environ.get("DATABASE_URL", ""),
"Negative ids no longer work in Django 3.0+ with MySQL.")
def test_view_manage_negative_group_form(self):
self._login_superuser()
url = reverse('admin:%s_%s_permissions' % self.obj_info,
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
'Framework :: Django',
'Framework :: Django :: 2.1',
'Framework :: Django :: 2.2',
'Framework :: Django :: 3.0',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
Expand Down
7 changes: 5 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[tox]
downloadcache = {toxworkdir}/cache/
envlist = # sort by django version, next by python version
{core,example,docs}-py{35,36,py37}-django21,
{core,example,docs}-py{35,36,py37,py38}-django22,
{core,example,docs}-py{35,36,37}-django21,
{core,example,docs}-py{35,36,37,38}-django22,
{core,example,docs}-py{36,37,38}-django30,

[testenv]
passenv = DATABASE_URL
Expand All @@ -17,6 +18,7 @@ changedir =
commands =
django21: python {toxinidir}/manage.py makemigrations --check --dry-run
django22: python {toxinidir}/manage.py makemigrations --check --dry-run
django30: python {toxinidir}/manage.py makemigrations --check --dry-run
core: py.test --cov=guardian
docs: sphinx-build -b html -d {envtmpdir}/doctrees . {envtmpdir}/html
example: python manage.py test
Expand All @@ -34,3 +36,4 @@ deps =
docs: setuptools_scm
django21: django>=2.1,<2.2
django22: django>=2.2,<2.3
django30: django>=3.0,<3.1

0 comments on commit febb63e

Please sign in to comment.