Skip to content

Commit

Permalink
Python 3 support. Fixes #115
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszb committed Feb 17, 2013
1 parent a30ddc5 commit 49fd61c
Show file tree
Hide file tree
Showing 47 changed files with 130 additions and 51 deletions.
2 changes: 1 addition & 1 deletion benchmarks/run_benchmarks.py
Expand Up @@ -97,7 +97,7 @@ def __init__(self, name, users_count, objects_count,
self.perm = 'auth.change_%s' % model._meta.module_name

def info(self, msg):
print colorize(msg + '\n', fg='green')
print(colorize(msg + '\n', fg='green'))

def prepare_db(self):
from django.core.management import call_command
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Expand Up @@ -32,7 +32,7 @@
if rst2pdf.version >= '0.16':
extensions.append('rst2pdf.pdfbuilder')
except ImportError:
print "[NOTE] In order to build PDF you need rst2pdf with version >=0.16"
print("[NOTE] In order to build PDF you need rst2pdf with version >=0.16")


autoclass_content = "both"
Expand Down Expand Up @@ -105,7 +105,7 @@
# The theme to use for HTML and HTML Help pages. Major themes that come with
# Sphinx are currently 'default' and 'sphinxdoc'.
#html_theme = 'default'
# Theme URL: https://github.com/coordt/ADCtheme/
# Theme URL: https://github.com/coordt/ADCtheme/
html_theme = 'ADCtheme'

# Theme options are theme-specific and customize the look and feel of a theme
Expand Down
8 changes: 6 additions & 2 deletions example_project/settings.py
Expand Up @@ -33,20 +33,24 @@
'django.contrib.sites',
'django.contrib.admin',
'django.contrib.messages',
'django.contrib.staticfiles',

'guardian',
'guardian.tests.testapp',
'posts',
)
if django.VERSION < (1, 3):
INSTALLED_APPS += ('staticfiles',)
else:
INSTALLED_APPS += ('django.contrib.staticfiles',)

if TEST_SOUTH:
INSTALLED_APPS += ('south',)
if 'GRAPPELLI' in os.environ:
try:
__import__('grappelli')
INSTALLED_APPS = ('grappelli',) + INSTALLED_APPS
except ImportError:
print "django-grappelli not installed"
print("django-grappelli not installed")

MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
Expand Down
2 changes: 1 addition & 1 deletion extras.py
Expand Up @@ -81,6 +81,6 @@ def run(self):
sys.stderr.write("ERROR: Finished with total %d warnings.\n" % warns)
sys.exit(1)
else:
print "No problems found in source codes."
print("No problems found in source codes.")


2 changes: 2 additions & 0 deletions guardian/__init__.py
@@ -1,6 +1,8 @@
"""
Implementation of per object permissions for Django 1.2 or later.
"""
from __future__ import unicode_literals

VERSION = (1, 0, 5, 'dev')

__version__ = '.'.join((str(each) for each in VERSION[:4]))
Expand Down
2 changes: 2 additions & 0 deletions guardian/admin.py
@@ -1,3 +1,5 @@
from __future__ import unicode_literals

from django import forms
from django.conf import settings
from guardian.compat import url, patterns
Expand Down
2 changes: 2 additions & 0 deletions guardian/backends.py
@@ -1,3 +1,5 @@
from __future__ import unicode_literals

from django.db import models

from guardian.compat import get_user_model
Expand Down
10 changes: 10 additions & 0 deletions guardian/compat.py
@@ -1,3 +1,5 @@
from __future__ import unicode_literals

from django.conf import settings
from django.contrib.auth.models import Group
from django.contrib.auth.models import Permission
Expand Down Expand Up @@ -60,3 +62,11 @@ def get_user_permission_codename(perm):
"""
return get_user_permission_full_codename(perm).split('.')[1]

# Python 3
try:
unicode = unicode # pyflakes:ignore
basestring = basestring # pyflakes:ignore
str = str # pyflakes:ignore
except NameError:
basestring = unicode = str = str

2 changes: 2 additions & 0 deletions guardian/conf/__init__.py
@@ -0,0 +1,2 @@
from __future__ import unicode_literals

1 change: 1 addition & 0 deletions guardian/conf/settings.py
@@ -1,3 +1,4 @@
from __future__ import unicode_literals
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

Expand Down
2 changes: 2 additions & 0 deletions guardian/core.py
@@ -1,3 +1,5 @@
from __future__ import unicode_literals

from itertools import chain

from django.contrib.auth.models import Permission
Expand Down
3 changes: 3 additions & 0 deletions guardian/decorators.py
@@ -1,10 +1,13 @@
from __future__ import unicode_literals

from django.conf import settings
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.utils.functional import wraps
from django.db.models import Model, get_model
from django.db.models.base import ModelBase
from django.db.models.query import QuerySet
from django.shortcuts import get_object_or_404
from guardian.compat import basestring
from guardian.exceptions import GuardianError
from guardian.utils import get_403_or_None

Expand Down
2 changes: 2 additions & 0 deletions guardian/exceptions.py
Expand Up @@ -2,6 +2,8 @@
Exceptions used by django-guardian. All internal and guardian-specific errors
should extend GuardianError class.
"""
from __future__ import unicode_literals


class GuardianError(Exception):
pass
Expand Down
6 changes: 4 additions & 2 deletions guardian/forms.py
@@ -1,3 +1,5 @@
from __future__ import unicode_literals

from django import forms
from django.utils.translation import ugettext as _

Expand Down Expand Up @@ -107,7 +109,7 @@ class UserObjectPermissionsForm(BaseObjectPermissionsForm):
from myapp.models import Post
from guardian.forms import UserObjectPermissionsForm
from django.contrib.auth.models import User
def my_view(request, post_slug, user_id):
user = get_object_or_404(User, id=user_id)
post = get_object_or_404(Post, slug=post_slug)
Expand Down Expand Up @@ -154,7 +156,7 @@ class GroupObjectPermissionsForm(BaseObjectPermissionsForm):
from myapp.models import Post
from guardian.forms import GroupObjectPermissionsForm
from guardian.models import Group
def my_view(request, post_slug, group_id):
group = get_object_or_404(Group, id=group_id)
post = get_object_or_404(Post, slug=post_slug)
Expand Down
1 change: 1 addition & 0 deletions guardian/management/__init__.py
@@ -1,3 +1,4 @@
from __future__ import unicode_literals

from django.db.models import signals

Expand Down
2 changes: 2 additions & 0 deletions guardian/management/commands/__init__.py
@@ -0,0 +1,2 @@
from __future__ import unicode_literals

5 changes: 3 additions & 2 deletions guardian/management/commands/clean_orphan_obj_perms.py
@@ -1,3 +1,4 @@
from __future__ import unicode_literals
from django.core.management.base import NoArgsCommand

from guardian.utils import clean_orphan_obj_perms
Expand All @@ -19,6 +20,6 @@ class Command(NoArgsCommand):
def handle_noargs(self, **options):
removed = clean_orphan_obj_perms()
if options['verbosity'] > 0:
print "Removed %d object permission entries with no targets" %\
removed
print("Removed %d object permission entries with no targets" %
removed)

2 changes: 2 additions & 0 deletions guardian/managers.py
@@ -1,3 +1,5 @@
from __future__ import unicode_literals

from django.db import models
from django.contrib.contenttypes.models import ContentType

Expand Down
3 changes: 3 additions & 0 deletions guardian/mixins.py
@@ -1,9 +1,12 @@
from __future__ import unicode_literals

from collections import Iterable
from django.conf import settings
from django.contrib.auth.decorators import REDIRECT_FIELD_NAME
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ImproperlyConfigured
from django.core.exceptions import PermissionDenied
from guardian.compat import basestring
from guardian.utils import get_403_or_None


Expand Down
2 changes: 2 additions & 0 deletions guardian/models.py
@@ -1,3 +1,4 @@
from __future__ import unicode_literals

from django.db import models
from django.core.exceptions import ValidationError
Expand All @@ -9,6 +10,7 @@
from guardian.compat import Permission
from guardian.compat import get_user_model
from guardian.compat import user_model_label
from guardian.compat import unicode
from guardian.managers import GroupObjectPermissionManager
from guardian.managers import UserObjectPermissionManager
from guardian.utils import get_anonymous_user
Expand Down
8 changes: 5 additions & 3 deletions guardian/shortcuts.py
@@ -1,6 +1,7 @@
"""
Convenient shortcuts to manage or check object permissions.
"""
from __future__ import unicode_literals

from django.contrib.auth.models import Group
from django.contrib.auth.models import Permission
Expand All @@ -11,6 +12,7 @@
from itertools import groupby

from guardian.compat import get_user_model
from guardian.compat import basestring
from guardian.core import ObjectPermissionChecker
from guardian.exceptions import MixedContentTypeError
from guardian.exceptions import WrongAppError
Expand Down Expand Up @@ -142,7 +144,7 @@ def get_perms_for_model(cls):
Returns queryset of all Permission objects for the given class. It is
possible to pass Model as class or instance.
"""
if isinstance(cls, str):
if isinstance(cls, basestring):
app_label, model_name = cls.split('.')
model = models.get_model(app_label, model_name)
else:
Expand Down Expand Up @@ -221,7 +223,7 @@ def get_users_with_perms(obj, attach_perms=False, with_superusers=False,
users = {}
for user in get_users_with_perms(obj,
with_group_users=with_group_users):
users[user] = get_perms(user, obj)
users[user] = sorted(get_perms(user, obj))
return users

def get_groups_with_perms(obj, attach_perms=False):
Expand Down Expand Up @@ -272,7 +274,7 @@ def get_groups_with_perms(obj, attach_perms=False):
groups = {}
for group in get_groups_with_perms(obj):
if not group in groups:
groups[group] = get_perms(group, obj)
groups[group] = sorted(get_perms(group, obj))
return groups

def get_objects_for_user(user, perms, klass=None, use_groups=True, any_perm=False):
Expand Down
2 changes: 2 additions & 0 deletions guardian/templatetags/__init__.py
@@ -0,0 +1,2 @@
from __future__ import unicode_literals

1 change: 1 addition & 0 deletions guardian/templatetags/guardian_tags.py
Expand Up @@ -5,6 +5,7 @@
{% load guardian_tags %}
"""
from __future__ import unicode_literals
from django import template
from django.contrib.auth.models import Group, AnonymousUser
from django.template import get_library
Expand Down
27 changes: 14 additions & 13 deletions guardian/tests/__init__.py
@@ -1,21 +1,22 @@
from __future__ import unicode_literals
import django
from django.conf import settings

from conf_test import *
from core_test import *
from custompkmodel_test import *
from decorators_test import *
from direct_rel_test import *
from forms_test import *
from orphans_test import *
from other_test import *
from utils_test import *
from shortcuts_test import *
from tags_test import *
from .conf_test import *
from .core_test import *
from .custompkmodel_test import *
from .decorators_test import *
from .direct_rel_test import *
from .forms_test import *
from .orphans_test import *
from .other_test import *
from .utils_test import *
from .shortcuts_test import *
from .tags_test import *


if 'django.contrib.admin' in settings.INSTALLED_APPS:
from admin_test import *
from .admin_test import *
if django.VERSION >= (1, 3):
from mixins_test import *
from .mixins_test import *

6 changes: 4 additions & 2 deletions guardian/tests/admin_test.py
@@ -1,3 +1,4 @@
from __future__ import unicode_literals
import copy

from django import forms
Expand All @@ -12,6 +13,7 @@

from guardian.admin import GuardedModelAdmin
from guardian.compat import get_user_model
from guardian.compat import str
from guardian.shortcuts import get_perms
from guardian.shortcuts import get_perms_for_model
from guardian.tests.conf import TEST_SETTINGS
Expand Down Expand Up @@ -292,7 +294,7 @@ def _get_gma(self, attrs=None, name=None, model=None):
Returns ``GuardedModelAdmin`` instance.
"""
attrs = attrs or {}
name = name or 'GMA'
name = str(name or 'GMA')
model = model or User
GMA = type(name, (GuardedModelAdmin,), attrs)
gma = GMA(model, admin.site)
Expand Down Expand Up @@ -371,7 +373,7 @@ def _get_gma(self, attrs=None, name=None, model=None):
Returns ``GuardedModelAdmin`` instance.
"""
attrs = attrs or {}
name = name or 'GMA'
name = str(name or 'GMA')
model = model or User
GMA = type(name, (GuardedModelAdmin,), attrs)
gma = GMA(model, admin.site)
Expand Down
1 change: 1 addition & 0 deletions guardian/tests/conf.py
@@ -1,3 +1,4 @@
from __future__ import unicode_literals
import os
from guardian.utils import abspath
from django.conf import settings
Expand Down
3 changes: 2 additions & 1 deletion guardian/tests/conf_test.py
@@ -1,3 +1,4 @@
from __future__ import unicode_literals
import mock
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase
Expand All @@ -7,7 +8,7 @@
class TestConfiguration(TestCase):

def test_check_configuration(self):

with mock.patch('guardian.conf.settings.RENDER_403', True):
with mock.patch('guardian.conf.settings.RAISE_403', True):
self.assertRaises(ImproperlyConfigured,
Expand Down
1 change: 1 addition & 0 deletions guardian/tests/core_test.py
@@ -1,3 +1,4 @@
from __future__ import unicode_literals
from itertools import chain

from django.conf import settings
Expand Down
1 change: 1 addition & 0 deletions guardian/tests/custompkmodel_test.py
@@ -1,3 +1,4 @@
from __future__ import unicode_literals
from django.contrib.contenttypes.models import ContentType
from django.test import TestCase

Expand Down

0 comments on commit 49fd61c

Please sign in to comment.