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

Django 2 compatibility #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 5 additions & 4 deletions django_rules/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
except ImportError: # python = 2.6
from django.utils.importlib import import_module # NOQA

from .exceptions import (NonexistentFieldName, NonexistentPermission,
NotBooleanPermission, RulesError)
from .models import RulePermission
from django_rules.exceptions import (NonexistentFieldName,
NonexistentPermission,
NotBooleanPermission, RulesError)
from django_rules.models import RulePermission


class RulePermCache(object):
Expand Down Expand Up @@ -51,7 +52,7 @@ def has_perm(self, user_obj, perm, obj=None):
if obj is None:
return False

if not user_obj.is_authenticated():
if not user_obj.is_authenticated:
user_obj = User.objects.get(pk=settings.ANONYMOUS_USER_ID)

# Centralized authorizations
Expand Down
8 changes: 4 additions & 4 deletions django_rules/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
from django.conf import settings
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import NoReverseMatch, reverse
from django.shortcuts import get_object_or_404
from django.urls import NoReverseMatch, reverse
from django.utils.functional import wraps
from django.utils.http import urlquote

from .backends import ObjectPermissionBackend, rule_cache
from .exceptions import NonexistentPermission, RulesError
from .models import RulePermission
from django_rules.backends import ObjectPermissionBackend, rule_cache
from django_rules.exceptions import NonexistentPermission, RulesError
from django_rules.models import RulePermission


def object_permission_required(perm, **kwargs):
Expand Down
16 changes: 10 additions & 6 deletions django_rules/models.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
# -*- coding: utf-8 -*-
import inspect

from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.db import models

from .exceptions import NonexistentFieldName, RulesError
from django_rules.exceptions import NonexistentFieldName, RulesError


class RulePermission(models.Model):
class Meta:
app_label = 'django_rules'
app_label = "django_rules"

"""
This model holds the rules for the authorization system
"""
codename = models.CharField(primary_key=True, max_length=30)
field_name = models.CharField(max_length=30)
content_type = models.ForeignKey(ContentType)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
view_param_pk = models.CharField(max_length=30)
description = models.CharField(max_length=140, null=True)

Expand All @@ -26,19 +27,22 @@ def save(self, *args, **kwargs):
raises ValidationError if it doesn't. We need to restrict security rules creation
"""
# If not set use codename as field_name as default
if self.field_name == '':
if self.field_name == "":
self.field_name = self.codename

# If not set use primary key attribute name as default
if self.view_param_pk == '':
if self.view_param_pk == "":
self.view_param_pk = self.content_type.model_class()._meta.pk.get_attname()

# First search for a method or property defined in the model class
# Then we look in the meta field_names
# If field_name does not exist a ValidationError is raised
if not hasattr(self.content_type.model_class(), self.field_name):
# Search within attributes field names
if not (self.field_name in self.content_type.model_class()._meta.get_all_field_names()):
if not (
self.field_name
in self.content_type.model_class()._meta.get_all_field_names()
):
raise NonexistentFieldName(
"Could not create rule: field_name %s of rule %s does not exist in model %s"
% (self.field_name, self.codename, self.content_type.model)
Expand Down
5 changes: 3 additions & 2 deletions django_rules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

from django.contrib.contenttypes.models import ContentType

from models import RulePermission

from django_rules.models import RulePermission


def register(app_name, codename, model, field_name='', view_param_pk='', description=''):
"""
Call this function in your rules.py to register your RulePermissions
Expand Down