mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-09-19 04:59:01 +00:00
Refactor templatetags and tests for styling
This commit is contained in:
parent
d4b98b9e48
commit
10496d89a5
@ -25,10 +25,12 @@ def mark_active_menuitem(menu, path):
|
|||||||
"""Mark the best-matching menu item with 'active'
|
"""Mark the best-matching menu item with 'active'
|
||||||
|
|
||||||
Input: a menu dict in the form of:
|
Input: a menu dict in the form of:
|
||||||
{'title': 'x',
|
{'title': 'foo menu',
|
||||||
'items': [{'url': 'a/b', 'text': 'myUrl'}, {'url': ...}]
|
'items': [{'url': '/path/to/choice1/', 'text': 'choice 1'}, {'url': ...}]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
URL paths are expected to end with a slash for matches to work properly.
|
||||||
|
|
||||||
Output: The same dictionary; the best-matching URL dict gets the value
|
Output: The same dictionary; the best-matching URL dict gets the value
|
||||||
'active': True. All other URL dicts get the value 'active': False.
|
'active': True. All other URL dicts get the value 'active': False.
|
||||||
|
|
||||||
@ -41,8 +43,8 @@ def mark_active_menuitem(menu, path):
|
|||||||
urlitem['active'] = False
|
urlitem['active'] = False
|
||||||
if not path.startswith(str(urlitem['url'])):
|
if not path.startswith(str(urlitem['url'])):
|
||||||
continue
|
continue
|
||||||
match = os.path.commonprefix([urlitem['url'], path])
|
|
||||||
|
|
||||||
|
match = os.path.commonprefix([urlitem['url'], path])
|
||||||
if len(match) > len(best_match):
|
if len(match) > len(best_match):
|
||||||
best_match = match
|
best_match = match
|
||||||
best_match_item = urlitem
|
best_match_item = urlitem
|
||||||
@ -54,7 +56,7 @@ def mark_active_menuitem(menu, path):
|
|||||||
|
|
||||||
|
|
||||||
@register.inclusion_tag('subsubmenu.html', takes_context=True)
|
@register.inclusion_tag('subsubmenu.html', takes_context=True)
|
||||||
def show_subsubmenu(context, menudata):
|
def show_subsubmenu(context, menu):
|
||||||
"""Mark the active menu item and display the subsubmenu"""
|
"""Mark the active menu item and display the subsubmenu"""
|
||||||
menudata = mark_active_menuitem(menudata, context['request'].path)
|
menu = mark_active_menuitem(menu, context['request'].path)
|
||||||
return {'subsubmenu': menudata}
|
return {'subsubmenu': menu}
|
||||||
|
|||||||
@ -1,5 +1,3 @@
|
|||||||
#!/usr/bin/python3
|
|
||||||
# -*- mode: python; mode: auto-fill; fill-column: 80 -*-
|
|
||||||
#
|
#
|
||||||
# This file is part of Plinth.
|
# This file is part of Plinth.
|
||||||
#
|
#
|
||||||
@ -22,48 +20,38 @@ import unittest
|
|||||||
from plinth.templatetags.plinth_extras import mark_active_menuitem
|
from plinth.templatetags.plinth_extras import mark_active_menuitem
|
||||||
|
|
||||||
|
|
||||||
class TestPrivileged(unittest.TestCase):
|
class TestShowSubSubMenu(unittest.TestCase):
|
||||||
"""Verify that the highlighting of the subsubmenu is working correctly"""
|
"""Verify that the highlighting of the subsubmenu is working correctly"""
|
||||||
|
def assert_active_url(self, menu, url):
|
||||||
def is_active_url(self, menu, url):
|
|
||||||
"""Verify that only the given url is set as 'active' in the menu"""
|
"""Verify that only the given url is set as 'active' in the menu"""
|
||||||
for urlitem in menu['items']:
|
for item in menu['items']:
|
||||||
if urlitem['url'] == url and not urlitem['active']:
|
if item['url'] == url:
|
||||||
return False
|
self.assertTrue(item['active'])
|
||||||
if urlitem['url'] != url and urlitem['active']:
|
else:
|
||||||
return False
|
self.assertFalse(item['active'])
|
||||||
return True
|
|
||||||
|
|
||||||
def verify_active_menuitems(self, menu):
|
def _verify_active_menuitems(self, menu):
|
||||||
"""Verify that one and only one menuitem is marked as active"""
|
"""Verify that one and only one menuitem is marked as active"""
|
||||||
return sum([x['active'] for x in menu['items']])==1
|
return sum([item['active'] for item in menu['items']]) == 1
|
||||||
|
|
||||||
def test_highlighting(self):
|
def test_highlighting(self):
|
||||||
"""Test detection of active subsubmenu items using request.path"""
|
"""Test detection of active subsubmenu items using request.path"""
|
||||||
menu = {
|
menu = {
|
||||||
"text": "blubb",
|
'text': 'blubb',
|
||||||
"items": [{'url': '/abc/123/abc/', 'text': 'abc'},
|
'items': [{'url': '/abc/123/abc/', 'text': 'abc'},
|
||||||
{'url': '/abc/123/', 'text': 'overview'},
|
{'url': '/abc/123/', 'text': 'overview'},
|
||||||
{'url': '/abc/123/crunch/', 'text': 'crunch'},
|
{'url': '/abc/123/crunch/', 'text': 'crunch'},
|
||||||
{'url': '/abc/123/create/', 'text': 'create'}]
|
{'url': '/abc/123/create/', 'text': 'create'}]
|
||||||
}
|
}
|
||||||
|
|
||||||
menu = mark_active_menuitem(menu, '/abc/123/crunch/new/')
|
tests = [['/abc/123/crunch/new/', '/abc/123/crunch/'],
|
||||||
assert(self.is_active_url(menu, '/abc/123/crunch/'))
|
['/abc/123/create/', '/abc/123/create/'],
|
||||||
assert(self.verify_active_menuitems(menu))
|
['/abc/123/nolink/', '/abc/123/'],
|
||||||
|
['/abc/123/abx/', '/abc/123/'],
|
||||||
|
['/abc/123/ab/', '/abc/123/'],
|
||||||
|
['/abc/123/', '/abc/123/']]
|
||||||
|
|
||||||
menu = mark_active_menuitem(menu, '/abc/123/create/')
|
for check_path, expected_active_path in tests:
|
||||||
assert(self.is_active_url(menu, '/abc/123/create/'))
|
menu = mark_active_menuitem(menu, check_path)
|
||||||
assert(self.verify_active_menuitems(menu))
|
self.assert_active_url(menu, expected_active_path)
|
||||||
|
self.assertTrue(self._verify_active_menuitems(menu))
|
||||||
menu = mark_active_menuitem(menu, '/abc/123/nolink/')
|
|
||||||
assert(self.is_active_url(menu, '/abc/123/'))
|
|
||||||
assert(self.verify_active_menuitems(menu))
|
|
||||||
|
|
||||||
menu = mark_active_menuitem(menu, '/abc/123/abx/')
|
|
||||||
assert(self.is_active_url(menu, '/abc/123/'))
|
|
||||||
assert(self.verify_active_menuitems(menu))
|
|
||||||
|
|
||||||
menu = mark_active_menuitem(menu, '/abc/123/')
|
|
||||||
assert(self.is_active_url(menu, '/abc/123/'))
|
|
||||||
assert(self.verify_active_menuitems(menu))
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user