Skip to content
This repository has been archived by the owner on Apr 16, 2023. It is now read-only.

Commit

Permalink
Add Route.level property
Browse files Browse the repository at this point in the history
  • Loading branch information
meshy committed Jan 21, 2018
1 parent 6560da7 commit 319d6c8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions conman/routes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ def handle(self, request, path):
# Deal with the request
return handler.handle(request, path)

@property
def level(self):
"""Fetch the one-indexed 'level' of this item in the URL tree."""
return self.url.count('/')

@level.setter
def level(self, new_value):
"""Silently fails to allow queryset annotation to work."""
pass

def move_to(self, new_url, *, move_children):
"""
Move this Route to a new url.
Expand Down
12 changes: 12 additions & 0 deletions tests/routes/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ def test_url_form_widget(self):
widget = Route._meta.get_field('url').formfield().widget
self.assertIsInstance(widget, forms.TextInput)

def test_level(self):
"""Route.level is based on the url field."""
urls = (
(1, '/'),
(2, '/branch/'),
(3, '/branch/leaf/'),
)
for level, url in urls:
with self.subTest(url=url):
route = RouteFactory.build(url=url)
self.assertEqual(route.level, level)


class RouteUniquenessTest(TestCase):
"""Check uniqueness conditions on Route are enforced in the DB."""
Expand Down

0 comments on commit 319d6c8

Please sign in to comment.