Browse Source

Group: add is_subgroup_of() method

merge-requests/64/head
Deimos 6 years ago
parent
commit
a4195964f4
  1. 27
      tildes/tests/test_group.py
  2. 8
      tildes/tildes/models/group/group.py

27
tildes/tests/test_group.py

@ -81,3 +81,30 @@ def test_duplicate_group(db):
with raises(IntegrityError): with raises(IntegrityError):
db.commit() db.commit()
def test_is_subgroup():
"""Ensure is_subgroup_of() works with an obvious subgroup."""
assert Group("test.subgroup").is_subgroup_of(Group("test"))
def test_is_subgroup_equal():
"""Ensure is_subgroup_of() returns False with the same groups."""
group = Group("testing")
assert not group.is_subgroup_of(group)
def test_is_subgroup_backwards():
"""Ensure is_subgroup_of() returns False if called "backwards"."""
assert not Group("test").is_subgroup_of(Group("test.subgroup"))
def test_is_subgroup_multilevel():
"""Ensure is_subgroup_of() works with "deeper" subgroups."""
path = "test.one.two.three"
group = Group(path)
path_parts = path.split(".")
# tests each of the "higher" groups (test, test.one, test.one.two)
for level in range(1, len(path_parts)):
assert group.is_subgroup_of(Group(".".join(path_parts[:level])))

8
tildes/tildes/models/group/group.py

@ -94,3 +94,11 @@ class Group(DatabaseModel):
acl.append(DENY_ALL) acl.append(DENY_ALL)
return acl return acl
def is_subgroup_of(self, other: "Group") -> bool:
"""Return whether this group is a sub-group of the other one."""
# descendant_of() returns True if the ltrees are equal, so avoid that
if self.path == other.path:
return False
return self.path.descendant_of(other.path)
Loading…
Cancel
Save