Skip to content

Commit

Permalink
add Section.get_tree_depth
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolas committed Sep 16, 2016
1 parent cb6fd02 commit 2aef773
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
1.2.4
==================

* Added `Section.get_tree_depth()`

1.2.3 (2016-09-09)
==================

Expand Down
12 changes: 12 additions & 0 deletions pagetree/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ def clear_caches(self):
cache.delete("pagetree.%d.get_absolute_url" % self.id)
cache.delete("pagetree.%d.is_last_child" % self.id)
cache.delete("pagetree.%d.get_edit_url" % self.id)
cache.delete("pagetree.%d.get_tree_depth" % self.id)

if hasattr(settings, 'PAGETREE_CUSTOM_CACHE_CLEAR') and \
callable(settings.PAGETREE_CUSTOM_CACHE_CLEAR):
Expand Down Expand Up @@ -665,6 +666,17 @@ def clone(original, section):

return section

def get_tree_depth(self):
key = 'pagetree.{}.get_tree_depth'.format(self.pk)
v = cache.get(key)
if v is not None:
return v

for idx, sec in enumerate(self.get_tree()):
if sec == self:
cache.set(key, idx)
return idx


@python_2_unicode_compatible
class PageBlock(models.Model):
Expand Down
5 changes: 5 additions & 0 deletions pagetree/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ def test_update_children_order(self):
r = self.root.get_children()
self.assertEqual([s.id for s in r], normal_order)

def test_get_tree_depth(self):
self.assertEqual(self.section1.get_tree_depth(), 1)
self.assertEqual(self.section2.get_tree_depth(), 2)
self.assertEqual(self.section3.get_tree_depth(), 3)


class OneLevelWithBlocksTest(unittest.TestCase):
def setUp(self):
Expand Down

0 comments on commit 2aef773

Please sign in to comment.