Skip to content

Commit

Permalink
Merge 1e13400 into ba054d8
Browse files Browse the repository at this point in the history
  • Loading branch information
zerolab committed May 6, 2021
2 parents ba054d8 + 1e13400 commit 3c2f0b9
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 109 deletions.
Empty file removed rules/compat/__init__.py
Empty file.
9 changes: 0 additions & 9 deletions rules/compat/inspect.py

This file was deleted.

25 changes: 0 additions & 25 deletions rules/compat/six.py

This file was deleted.

4 changes: 1 addition & 3 deletions rules/contrib/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
# versions before 1.9. For usage help see Django's docs for 1.9 or later.
from django.views.generic.edit import BaseCreateView

from rules.compat.six import string_types, wraps # noqa

LoginRequiredMixin = mixins.LoginRequiredMixin
UserPassesTestMixin = mixins.UserPassesTestMixin

Expand Down Expand Up @@ -205,7 +203,7 @@ def decorator(view_func):
@wraps(view_func)
def _wrapped_view(request, *args, **kwargs):
# Normalize to a list of permissions
if isinstance(perm, string_types):
if isinstance(perm, str):
perms = (perm,)
else:
perms = perm
Expand Down
19 changes: 9 additions & 10 deletions rules/predicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
import operator
import threading
from functools import partial, update_wrapper

from .compat import inspect
from inspect import getfullargspec, isfunction, ismethod

logger = logging.getLogger("rules")


def assert_has_kwonlydefaults(fn, msg):
argspec = inspect.getfullargspec(fn)
argspec = getfullargspec(fn)
if hasattr(argspec, "kwonlyargs"):
if not argspec.kwonlyargs:
return
Expand Down Expand Up @@ -63,23 +62,23 @@ def __init__(self, fn, name=None, bind=False):
innerfn = fn
elif isinstance(fn, partial):
innerfn = fn.func
argspec = inspect.getfullargspec(innerfn)
argspec = getfullargspec(innerfn)
var_args = argspec.varargs is not None
num_args = len(argspec.args) - len(fn.args)
if inspect.ismethod(innerfn):
if ismethod(innerfn):
num_args -= 1 # skip `self`
name = fn.func.__name__
elif inspect.ismethod(fn):
argspec = inspect.getfullargspec(fn)
elif ismethod(fn):
argspec = getfullargspec(fn)
var_args = argspec.varargs is not None
num_args = len(argspec.args) - 1 # skip `self`
elif inspect.isfunction(fn):
argspec = inspect.getfullargspec(fn)
elif isfunction(fn):
argspec = getfullargspec(fn)
var_args = argspec.varargs is not None
num_args = len(argspec.args)
elif isinstance(fn, object):
innerfn = getattr(fn, "__call__") # noqa
argspec = inspect.getfullargspec(innerfn)
argspec = getfullargspec(innerfn)
var_args = argspec.varargs is not None
num_args = len(argspec.args) - 1 # skip `self`
name = name or type(fn).__name__
Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ exclude_lines =
pragma: no cover
omit =
*/rules/apps.py
*/rules/compat/*
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def get_version(version):
"rules",
"rules.templatetags",
"rules.contrib",
"rules.compat",
],
classifiers=[
"Development Status :: 5 - Production/Stable",
Expand Down
27 changes: 7 additions & 20 deletions tests/testapp/models.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
from __future__ import absolute_import

import sys

from django.conf import settings
from django.db import models

try:
from django.utils.encoding import python_2_unicode_compatible
except ImportError:

def python_2_unicode_compatible(c):
return c


import rules
from rules.contrib.models import RulesModel


@python_2_unicode_compatible
class Book(models.Model):
isbn = models.CharField(max_length=50, unique=True)
title = models.CharField(max_length=100)
Expand All @@ -26,13 +16,10 @@ def __str__(self):
return self.title


if sys.version_info.major >= 3:
from rules.contrib.models import RulesModel

class TestModel(RulesModel):
class Meta:
rules_permissions = {"add": rules.always_true, "view": rules.always_true}
class TestModel(RulesModel):
class Meta:
rules_permissions = {"add": rules.always_true, "view": rules.always_true}

@classmethod
def preprocess_rules_permissions(cls, perms):
perms["custom"] = rules.always_true
@classmethod
def preprocess_rules_permissions(cls, perms):
perms["custom"] = rules.always_true
24 changes: 0 additions & 24 deletions tests/testsuite/_test_predicates_kwonly.py

This file was deleted.

5 changes: 0 additions & 5 deletions tests/testsuite/contrib/test_models.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
from __future__ import absolute_import

import sys
import unittest

from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase

import rules


@unittest.skipIf(sys.version_info.major < 3, "Python 3 only")
class RulesModelTests(TestCase):
def test_preprocess(self):

self.assertTrue(rules.perm_exists("testapp.add_testmodel"))
self.assertTrue(rules.perm_exists("testapp.custom_testmodel"))

Expand Down
4 changes: 0 additions & 4 deletions tests/testsuite/contrib/test_rest_framework.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from __future__ import absolute_import

import sys
import unittest

from django.contrib.auth.models import AnonymousUser
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase
Expand All @@ -17,7 +14,6 @@
from rules.contrib.rest_framework import AutoPermissionViewSetMixin


@unittest.skipIf(sys.version_info.major < 3, "Python 3 only")
class AutoPermissionRequiredMixinTests(TestCase):
def setUp(self):
from testapp.models import TestModel
Expand Down
4 changes: 0 additions & 4 deletions tests/testsuite/contrib/test_views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from __future__ import absolute_import

import sys
import unittest

from django.contrib.auth.models import AnonymousUser
from django.core.exceptions import ImproperlyConfigured, PermissionDenied
from django.http import Http404, HttpRequest
Expand Down Expand Up @@ -152,7 +149,6 @@ def test_permission_required_mixin(self):
self.assertIn(response.status_code, [302, 403])


@unittest.skipIf(sys.version_info.major < 3, "Python 3 only")
class AutoPermissionRequiredMixinTests(TestCase):
def setUp(self):
from testapp.models import TestModel
Expand Down
23 changes: 20 additions & 3 deletions tests/testsuite/test_predicates.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import functools
import sys
from unittest import TestCase

from rules.predicates import (
Expand All @@ -12,8 +11,26 @@
predicate,
)

if sys.version_info >= (3,):
from ._test_predicates_kwonly import * # noqa

class PredicateKwonlyTests(TestCase):
def test_predicate_kwargonly(self):
def p(foo, *, bar):
return True

with self.assertRaises(TypeError):
predicate(p)

def p2(foo, *a, bar):
return True

with self.assertRaises(TypeError):
predicate(p2)

def p3(foo, *, bar="bar"):
return True

# Should not fail
predicate(p3)


class PredicateTests(TestCase):
Expand Down

0 comments on commit 3c2f0b9

Please sign in to comment.