[#2711] setting AUF_PERMISSIONS_RULES
authorEric Mc Sween <eric.mcsween@auf.org>
Wed, 29 Feb 2012 23:53:56 +0000 (18:53 -0500)
committerEric Mc Sween <eric.mcsween@auf.org>
Wed, 29 Feb 2012 23:55:25 +0000 (18:55 -0500)
auf/django/permissions/__init__.py
auf/django/permissions/predicates.py
setup.py
tests/food/tests.py
tests/permissions.py [new file with mode: 0644]
tests/settings.py

index 0204c40..8398acf 100644 (file)
@@ -2,6 +2,8 @@
 
 from collections import defaultdict
 
 
 from collections import defaultdict
 
+from django.conf import settings
+from django.utils.importlib import import_module
 
 class Predicate(object):
     """
 
 class Predicate(object):
     """
@@ -123,7 +125,7 @@ class Rules(object):
         else:
             return queryset.filter(result)
 
         else:
             return queryset.filter(result)
 
-    def clear():
+    def clear(self):
         self.allow_rules.clear()
         self.deny_rules.clear()
 
         self.allow_rules.clear()
         self.deny_rules.clear()
 
@@ -146,3 +148,14 @@ class AuthenticationBackend(object):
     def get_user(self, user_id):
         # We don't authenticate
         return None
     def get_user(self, user_id):
         # We don't authenticate
         return None
+
+_rules = None
+def get_rules():
+    global _rules
+    if _rules is None:
+        if not hasattr(settings, 'AUF_PERMISSIONS_RULES'):
+            raise ImproperlyConfigured('Vous devez configurer la variable AUF_PERMISSIONS_RULES')
+        module_name, dot, attr = settings.AUF_PERMISSIONS_RULES.rpartition('.')
+        module = import_module(module_name)
+        _rules = getattr(module, attr)
+    return _rules
index 924a8a6..74af53d 100644 (file)
@@ -4,7 +4,7 @@
 Builtin predicates.
 """
 
 Builtin predicates.
 """
 
-from auf.django.permissions import Predicate, predicate_for_perm, predicate_generator
+from auf.django.permissions import Predicate, get_rules
 
 
 def has_global_perm(perm):
 
 
 def has_global_perm(perm):
@@ -14,5 +14,5 @@ def has_global_perm(perm):
 
 def has_object_perm(perm):
     def p(user, obj, cls):
 
 def has_object_perm(perm):
     def p(user, obj, cls):
-        return predicate_for_perm(perm, cls or obj.__class__)(user, obj, cls)
+        return get_rules().predicate_for_perm(perm, cls or obj.__class__)(user, obj, cls)
     return Predicate(p)
     return Predicate(p)
index 9ae5e8b..5086473 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -3,7 +3,7 @@
 from setuptools import setup, find_packages
 
 name = 'auf.django.permissions'
 from setuptools import setup, find_packages
 
 name = 'auf.django.permissions'
-version = '0.1'
+version = '0.2'
 
 setup(name=name,
       version=version,
 
 setup(name=name,
       version=version,
index 2ece9a6..6f09729 100644 (file)
@@ -7,9 +7,10 @@ from django.template import Template, RequestContext
 from django.utils import unittest
 from django.utils.text import normalize_newlines
 
 from django.utils import unittest
 from django.utils.text import normalize_newlines
 
-from auf.django.permissions import Rules, Predicate, AuthenticationBackend
+from auf.django.permissions import Predicate, AuthenticationBackend
 
 from tests.food.models import Food
 
 from tests.food.models import Food
+from tests.permissions import rules
 
 @Predicate
 def is_allergic(user, obj, cls):
 
 @Predicate
 def is_allergic(user, obj, cls):
@@ -24,8 +25,7 @@ class FoodTestCase(unittest.TestCase):
         self.apple = Food.objects.create(name='apple')
         self.banana = Food.objects.create(name='banana')
         self.banana.allergic_users.add(self.alice)
         self.apple = Food.objects.create(name='apple')
         self.banana = Food.objects.create(name='banana')
         self.banana.allergic_users.add(self.alice)
-        self.rules = Rules()
-        AuthenticationBackend.rules = self.rules
+        rules.clear()
 
     def tearDown(self):
         self.alice.delete()
 
     def tearDown(self):
         self.alice.delete()
@@ -37,38 +37,38 @@ class FoodTestCase(unittest.TestCase):
 class RulesTestCase(FoodTestCase):
 
     def test_global_perms(self):
 class RulesTestCase(FoodTestCase):
 
     def test_global_perms(self):
-        self.rules.allow_global('sing', Predicate(lambda user: user is self.alice))
+        rules.allow_global('sing', Predicate(lambda user: user is self.alice))
         self.assertTrue(self.alice.has_perm('sing'))
         self.assertFalse(self.alice.has_perm('dance'))
 
     def test_global_deny(self):
         self.assertTrue(self.alice.has_perm('sing'))
         self.assertFalse(self.alice.has_perm('dance'))
 
     def test_global_deny(self):
-        self.rules.allow_global('eat', Predicate(True))
-        self.rules.deny_global('eat', Predicate(lambda user: user is self.bob))
+        rules.allow_global('eat', Predicate(True))
+        rules.deny_global('eat', Predicate(lambda user: user is self.bob))
         self.assertTrue(self.alice.has_perm('eat'))
         self.assertFalse(self.bob.has_perm('eat'))
 
     def test_object_perms(self):
         self.assertTrue(self.alice.has_perm('eat'))
         self.assertFalse(self.bob.has_perm('eat'))
 
     def test_object_perms(self):
-        self.rules.allow('eat', Food, ~is_allergic)
+        rules.allow('eat', Food, ~is_allergic)
         self.assertTrue(self.alice.has_perm('eat', self.apple))
         self.assertFalse(self.alice.has_perm('eat', self.banana))
 
     def test_object_deny(self):
         self.assertTrue(self.alice.has_perm('eat', self.apple))
         self.assertFalse(self.alice.has_perm('eat', self.banana))
 
     def test_object_deny(self):
-        self.rules.allow('eat', Food, Predicate(True))
-        self.rules.deny('eat', Food, is_allergic)
+        rules.allow('eat', Food, Predicate(True))
+        rules.deny('eat', Food, is_allergic)
         self.assertTrue(self.alice.has_perm('eat', self.apple))
         self.assertFalse(self.alice.has_perm('eat', self.banana))
 
     def test_no_rules(self):
         self.assertFalse(self.alice.has_perm('climb'))
         self.assertTrue(self.alice.has_perm('eat', self.apple))
         self.assertFalse(self.alice.has_perm('eat', self.banana))
 
     def test_no_rules(self):
         self.assertFalse(self.alice.has_perm('climb'))
-        self.rules.allow('eat', Food, Predicate(True))
+        rules.allow('eat', Food, Predicate(True))
         self.assertTrue(self.alice.has_perm('eat', self.apple))
         self.assertFalse(self.alice.has_perm('eat', self.bob))
 
     def test_q_rules(self):
         self.assertTrue(self.alice.has_perm('eat', self.apple))
         self.assertFalse(self.alice.has_perm('eat', self.bob))
 
     def test_q_rules(self):
-        self.rules.allow('eat', Food, ~is_allergic)
-        self.assertListEqual(list(self.rules.filter_queryset(self.alice, 'eat', Food.objects.all())),
+        rules.allow('eat', Food, ~is_allergic)
+        self.assertListEqual(list(rules.filter_queryset(self.alice, 'eat', Food.objects.all())),
                              [self.apple])
                              [self.apple])
-        self.assertListEqual(list(self.rules.filter_queryset(self.bob, 'eat', Food.objects.all())),
+        self.assertListEqual(list(rules.filter_queryset(self.bob, 'eat', Food.objects.all())),
                              [self.apple, self.banana])
         self.assertTrue(self.alice.has_perm('eat', self.apple))
         self.assertFalse(self.alice.has_perm('eat', self.banana))
                              [self.apple, self.banana])
         self.assertTrue(self.alice.has_perm('eat', self.apple))
         self.assertFalse(self.alice.has_perm('eat', self.banana))
@@ -78,8 +78,8 @@ class TemplateTagsTestCase(FoodTestCase):
 
     def setUp(self):
         FoodTestCase.setUp(self)
 
     def setUp(self):
         FoodTestCase.setUp(self)
-        self.rules.allow('eat', Food, ~is_allergic)
-        self.rules.allow('throw', Food, Predicate(True))
+        rules.allow('eat', Food, ~is_allergic)
+        rules.allow('throw', Food, Predicate(True))
 
     def test_ifhasperm(self):
         template = Template("""{% load permissions %}
 
     def test_ifhasperm(self):
         template = Template("""{% load permissions %}
diff --git a/tests/permissions.py b/tests/permissions.py
new file mode 100644 (file)
index 0000000..a62a047
--- /dev/null
@@ -0,0 +1,3 @@
+from auf.django.permissions import Rules
+
+rules = Rules()
index 2002da0..bbd9ede 100644 (file)
@@ -23,3 +23,5 @@ TEMPLATE_LOADERS=(
 TEMPLATE_CONTEXT_PROCESSORS=(
     'django.core.context_processors.request',
 )
 TEMPLATE_CONTEXT_PROCESSORS=(
     'django.core.context_processors.request',
 )
+
+AUF_PERMISSIONS_RULES = 'tests.permissions.rules'