Commit c5b84d2d by Őry Máté

acl: add get_objects_with_group_level

parent 6f0adf06
...@@ -201,5 +201,17 @@ class AclBase(Model): ...@@ -201,5 +201,17 @@ class AclBase(Model):
clsfilter |= Q(owner=user) clsfilter |= Q(owner=user)
return cls.objects.filter(clsfilter) return cls.objects.filter(clsfilter)
@classmethod
def get_objects_with_group_level(cls, level, group):
if isinstance(level, basestring):
level = cls.get_level_object(level)
ct = ContentType.objects.get_for_model(cls)
levelfilter = Q(groups=group)
ols = ObjectLevel.objects.filter(
levelfilter,
content_type=ct, level__weight__gte=level.weight).distinct()
clsfilter = Q(object_level_set__in=ols.all())
return cls.objects.filter(clsfilter)
class Meta: class Meta:
abstract = True abstract = True
from django.db.models import TextField from django.db.models import TextField, ForeignKey
from django.contrib.auth.models import User
from ..models import AclBase from ..models import AclBase
...@@ -15,6 +16,7 @@ class TestModel(AclBase): ...@@ -15,6 +16,7 @@ class TestModel(AclBase):
class Test2Model(AclBase): class Test2Model(AclBase):
normal2_field = TextField() normal2_field = TextField()
owner = ForeignKey(User)
ACL_LEVELS = ( ACL_LEVELS = (
('one', 'One'), ('one', 'One'),
......
...@@ -14,6 +14,8 @@ class AclUserTest(TestCase): ...@@ -14,6 +14,8 @@ class AclUserTest(TestCase):
self.g1.user_set.add(self.u1) self.g1.user_set.add(self.u1)
self.g1.user_set.add(self.u2) self.g1.user_set.add(self.u2)
self.g1.save() self.g1.save()
self.g2 = Group.objects.create(name='group2')
self.g2.save()
def test_level_exists(self): def test_level_exists(self):
for codename, name in TestModel.ACL_LEVELS: for codename, name in TestModel.ACL_LEVELS:
...@@ -147,3 +149,14 @@ class AclUserTest(TestCase): ...@@ -147,3 +149,14 @@ class AclUserTest(TestCase):
self.assertTrue(i.has_level(self.u1, 'alfa')) self.assertTrue(i.has_level(self.u1, 'alfa'))
i.set_level(self.g1, None) i.set_level(self.g1, None)
self.assertFalse(i.has_level(self.u1, 'alfa')) self.assertFalse(i.has_level(self.u1, 'alfa'))
def test_get_objects_with_group_level(self):
i1 = TestModel.objects.create(normal_field='Hello1')
i2 = TestModel.objects.create(normal_field='Hello2')
i1.set_level(self.g1, 'alfa')
i2.set_level(self.g1, 'bravo')
i2.set_level(self.g2, 'bravo')
self.assertItemsEqual(
TestModel.get_objects_with_group_level('alfa', self.g1), [i1, i2])
self.assertItemsEqual(
TestModel.get_objects_with_group_level('alfa', self.g2), [i2])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment