From 9a5e0d1d06d3d8d89267e660d4a0b2fcc18c04f8 Mon Sep 17 00:00:00 2001 From: Tim Nyborg Date: Sat, 13 Feb 2021 12:24:47 +0000 Subject: [PATCH 1/4] update usage.rst to include callable url --- docs/usage.rst | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/docs/usage.rst b/docs/usage.rst index 72566ec..0c33f49 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -23,8 +23,15 @@ The ``MenuItem`` class should be instantiated and passed to the ``add_item`` class method with the appropriate parameters. ``MenuItem`` accepts a wide number of options to its constructor method, the majority of which are simply attributes that become available in your templates when you're rendering out -the menus. The required arguments to MenuItem are the first two; the title of -the menu and the URL, and the keywords that affect menu generation are: +the menus. + +``MenuItem`` requires the first two arguments: + + * The ``title`` of the item + * The ``url`` of the item, which can be a string or a callable which accepts the request + object and returns a string. + +The keywords that affect menu generation are: * The ``weight`` keyword argument affects sorting of the menu. * The ``children`` keyword argument is either a list of ``MenuItem`` objects, From 75a75839bba4dff0fa1798d45763d12f83461622 Mon Sep 17 00:00:00 2001 From: nyborgt Date: Sat, 13 Feb 2021 13:03:50 +0000 Subject: [PATCH 2/4] test for callable url --- simple_menu/tests/test_menu.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/simple_menu/tests/test_menu.py b/simple_menu/tests/test_menu.py index 14aa6f6..3b5d35b 100644 --- a/simple_menu/tests/test_menu.py +++ b/simple_menu/tests/test_menu.py @@ -39,6 +39,13 @@ def kids3_2_title(request): return "-".join([request.path, self.kids3_2_desired_title]) return 'kids3-2' + self.kids3_2_desired_url = None + def kids3_2_url(request): + "Allow the url of kids3-2 to be changed" + if self.kids3_2_desired_url is not None: + return '/'.join([request.path, self.kids3_2_desired_url]) + return '/parent3/kids3-2' + def kids2_2_check(request): "Hide kids2-2 whenever the request path ends with /hidden" if request.path.endswith('/hidden'): @@ -66,7 +73,7 @@ def kids3_1(request): kids3 = ( CustomMenuItem("kids3-1", "/parent3/kids3-1", children=kids3_1, slug="salty"), - CustomMenuItem(kids3_2_title, "/parent3/kids3-2") + CustomMenuItem(kids3_2_title, kids3_2_url) ) Menu.items = {} @@ -161,6 +168,15 @@ def test_callable_title(self): items = Menu.process(request, 'test') self.assertEqual(items[1].children[1].title, "/parent3-fun") + def test_callable_url(self): + """ + Ensure callable urls work + """ + self.kids3_2_desired_url = "custom" + request = self.factory.get('/parent3') + items = Menu.process(request, 'test') + self.assertEqual(items[1].children[1].url, "/parent3/custom") + def test_select_parents(self): """ Ensure the MENU_SELECT_PARENTS setting works From 18d3993ee2492541b7a7877ab68776c11d4a461a Mon Sep 17 00:00:00 2001 From: Gabriel Dugny Date: Sun, 27 Nov 2022 14:18:28 +0100 Subject: [PATCH 3/4] Callable urls --- simple_menu/menu.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/simple_menu/menu.py b/simple_menu/menu.py index fb1df43..d89edf7 100644 --- a/simple_menu/menu.py +++ b/simple_menu/menu.py @@ -194,6 +194,10 @@ def process(self, request): if not self.visible: return + # evaluate our url + if callable(self.url): + self.url = self.url(request) + # evaluate our title if callable(self.title): self.title = self.title(request) From 764faa1d47785b9ab85cb91e1a1a6f6839548a92 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 27 Nov 2022 13:23:39 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/usage.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/usage.rst b/docs/usage.rst index 0c33f49..89e1709 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -23,14 +23,14 @@ The ``MenuItem`` class should be instantiated and passed to the ``add_item`` class method with the appropriate parameters. ``MenuItem`` accepts a wide number of options to its constructor method, the majority of which are simply attributes that become available in your templates when you're rendering out -the menus. +the menus. -``MenuItem`` requires the first two arguments: +``MenuItem`` requires the first two arguments: * The ``title`` of the item - * The ``url`` of the item, which can be a string or a callable which accepts the request + * The ``url`` of the item, which can be a string or a callable which accepts the request object and returns a string. - + The keywords that affect menu generation are: * The ``weight`` keyword argument affects sorting of the menu.