mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-01-21 07:55:00 +00:00
menu: Implement a helper method to lookup menu items using URL name
- This will used to create breadcumbs. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: Veiko Aasa <veiko17@disroot.org>
This commit is contained in:
parent
296c25627e
commit
86031d25f1
@ -90,6 +90,18 @@ class Menu(app.FollowerComponent):
|
|||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_with_url_name(url_name: str) -> 'Menu':
|
||||||
|
"""Return a menu item with given URL name.
|
||||||
|
|
||||||
|
Raise LookupError of the request item is not found.
|
||||||
|
"""
|
||||||
|
for item in Menu._all_menus:
|
||||||
|
if item.url_name == url_name:
|
||||||
|
return item
|
||||||
|
|
||||||
|
raise LookupError
|
||||||
|
|
||||||
|
|
||||||
main_menu = None
|
main_menu = None
|
||||||
|
|
||||||
|
|||||||
@ -13,4 +13,14 @@ urlpatterns = [
|
|||||||
re_path(r'^sys/$', _test_view, name='system'),
|
re_path(r'^sys/$', _test_view, name='system'),
|
||||||
re_path(r'^test/(?P<a>\d+)/(?P<b>\d+)/(?P<c>\d+)/$', _test_view,
|
re_path(r'^test/(?P<a>\d+)/(?P<b>\d+)/(?P<c>\d+)/$', _test_view,
|
||||||
name='test'),
|
name='test'),
|
||||||
|
re_path(r'^test1/(?P<a>\d+)/(?P<b>\d+)/(?P<c>\d+)/$', _test_view,
|
||||||
|
name='test1'),
|
||||||
|
re_path(r'^test2/(?P<a>\d+)/(?P<b>\d+)/(?P<c>\d+)/$', _test_view,
|
||||||
|
name='test2'),
|
||||||
|
re_path(r'^test3/(?P<a>\d+)/(?P<b>\d+)/(?P<c>\d+)/$', _test_view,
|
||||||
|
name='test3'),
|
||||||
|
re_path(r'^test4/(?P<a>\d+)/(?P<b>\d+)/(?P<c>\d+)/$', _test_view,
|
||||||
|
name='test4'),
|
||||||
|
re_path(r'^test5/(?P<a>\d+)/(?P<b>\d+)/(?P<c>\d+)/$', _test_view,
|
||||||
|
name='test5'),
|
||||||
]
|
]
|
||||||
|
|||||||
@ -12,7 +12,7 @@ from django.urls import reverse
|
|||||||
from plinth import menu as menu_module
|
from plinth import menu as menu_module
|
||||||
from plinth.menu import Menu
|
from plinth.menu import Menu
|
||||||
|
|
||||||
URL_TEMPLATE = '/test/{}/{}/{}/'
|
URL_TEMPLATE = '/test{}/{}/{}/{}/'
|
||||||
|
|
||||||
# Test helper methods
|
# Test helper methods
|
||||||
|
|
||||||
@ -25,11 +25,11 @@ def build_menu(size=5):
|
|||||||
|
|
||||||
for index in range(1, size + 1):
|
for index in range(1, size + 1):
|
||||||
kwargs = {
|
kwargs = {
|
||||||
'component_id': 'menu-test-' + str(index),
|
'component_id': f'menu-test-{index}',
|
||||||
'name': 'Name' + str(index),
|
'name': f'Name{index}',
|
||||||
'short_description': 'ShortDescription' + str(index),
|
'short_description': f'ShortDescription{index}',
|
||||||
'icon': 'Icon' + str(index),
|
'icon': f'Icon{index}',
|
||||||
'url_name': 'test',
|
'url_name': f'test{index}',
|
||||||
'url_kwargs': {
|
'url_kwargs': {
|
||||||
'a': index,
|
'a': index,
|
||||||
'b': index,
|
'b': index,
|
||||||
@ -134,7 +134,7 @@ def test_active_item():
|
|||||||
|
|
||||||
for index in range(1, 8):
|
for index in range(1, 8):
|
||||||
request = HttpRequest()
|
request = HttpRequest()
|
||||||
request.path = URL_TEMPLATE.format(index, index, index)
|
request.path = URL_TEMPLATE.format(index, index, index, index)
|
||||||
item = menu.active_item(request)
|
item = menu.active_item(request)
|
||||||
if index <= 5:
|
if index <= 5:
|
||||||
assert request.path == item.url
|
assert request.path == item.url
|
||||||
@ -145,8 +145,18 @@ def test_active_item():
|
|||||||
def test_active_item_when_inside_subpath():
|
def test_active_item_when_inside_subpath():
|
||||||
"""Verify that the current URL could be a sub-path of a menu item."""
|
"""Verify that the current URL could be a sub-path of a menu item."""
|
||||||
menu = build_menu()
|
menu = build_menu()
|
||||||
expected_url = URL_TEMPLATE.format(1, 1, 1)
|
expected_url = URL_TEMPLATE.format(1, 1, 1, 1)
|
||||||
request = HttpRequest()
|
request = HttpRequest()
|
||||||
request.path = expected_url + 'd/e/f/'
|
request.path = expected_url + 'd/e/f/'
|
||||||
item = menu.active_item(request)
|
item = menu.active_item(request)
|
||||||
assert expected_url == item.url
|
assert expected_url == item.url
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_with_url_name():
|
||||||
|
"""Verify that menu item can be retrieved from all items."""
|
||||||
|
build_menu()
|
||||||
|
|
||||||
|
menu = Menu.get_with_url_name('test5')
|
||||||
|
assert menu.name == 'Name5'
|
||||||
|
with pytest.raises(LookupError):
|
||||||
|
Menu.get_with_url_name('x-non-existent')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user