Skip to content

Commit 06efaa5

Browse files
authored
feat: lambda support in links (#245)
1 parent a1a2e6d commit 06efaa5

7 files changed

+11
-31
lines changed

src/unfold/settings.py

-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from functools import lru_cache
2-
31
from django.conf import settings
42

53
CONFIG_DEFAULTS = {
@@ -44,7 +42,6 @@
4442
}
4543

4644

47-
@lru_cache
4845
def get_config(settings_name=None):
4946
if settings_name is None:
5047
settings_name = "UNFOLD"

src/unfold/sites.py

+6
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ def _get_is_active(link: str) -> bool:
257257
if has_primary_link and has_tab_link_active:
258258
item["active"] = True
259259

260+
if isinstance(item["link"], Callable):
261+
item["link"] = item["link"](request)
262+
260263
# Badge callbacks
261264
if "badge" in item and isinstance(item["badge"], str):
262265
try:
@@ -284,6 +287,9 @@ def get_tabs_list(self, request: HttpRequest) -> List[Dict[str, Any]]:
284287
if not self._call_permission_callback(item.get("permission"), request):
285288
continue
286289

290+
if isinstance(item["link"], Callable):
291+
item["link"] = item["link"](request)
292+
287293
allowed_items.append(item)
288294

289295
tab["items"] = allowed_items

tests/test_colors.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from django.test import TestCase
33
from django.test.client import RequestFactory
44
from django.test.utils import override_settings
5-
from unfold.settings import CONFIG_DEFAULTS, get_config
5+
from unfold.settings import CONFIG_DEFAULTS
66
from unfold.sites import UnfoldAdminSite
77

88

@@ -49,8 +49,6 @@ def test_colors_hex_to_rgb(self):
4949
self.assertEqual(context["colors"]["primary"][900], "12 74 110")
5050
self.assertEqual(context["colors"]["primary"][950], "8 47 73")
5151

52-
get_config.cache_clear()
53-
5452
@override_settings(
5553
UNFOLD={
5654
**CONFIG_DEFAULTS,
@@ -92,5 +90,3 @@ def test_colors_rgb(self):
9290
self.assertEqual(context["colors"]["primary"][800], "7 89 133")
9391
self.assertEqual(context["colors"]["primary"][900], "12 74 110")
9492
self.assertEqual(context["colors"]["primary"][950], "8 47 73")
95-
96-
get_config.cache_clear()

tests/test_environment.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from django.test import TestCase
33
from django.test.client import RequestFactory
44
from django.test.utils import override_settings
5-
from unfold.settings import CONFIG_DEFAULTS, get_config
5+
from unfold.settings import CONFIG_DEFAULTS
66
from unfold.sites import UnfoldAdminSite
77

88

@@ -18,7 +18,6 @@ def test_empty_environment_callback(self):
1818
request.user = AnonymousUser()
1919
context = admin_site.each_context(request)
2020
self.assertTrue("environment" not in context)
21-
get_config.cache_clear()
2221

2322
@override_settings(
2423
UNFOLD={
@@ -34,7 +33,6 @@ def test_incorrect_environment_callback(self):
3433
request.user = AnonymousUser()
3534
context = admin_site.each_context(request)
3635
self.assertTrue("environment" not in context)
37-
get_config.cache_clear()
3836

3937
@override_settings(
4038
UNFOLD={
@@ -51,4 +49,3 @@ def test_correct_environment_callback(self):
5149
context = admin_site.each_context(request)
5250
self.assertTrue("environment" in context)
5351
self.assertEqual(context["environment"], ["Testing Environment", "warning"])
54-
get_config.cache_clear()

tests/test_navigations.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django.test import TestCase
22
from django.test.client import RequestFactory
33
from django.test.utils import override_settings
4-
from unfold.settings import CONFIG_DEFAULTS, get_config
4+
from unfold.settings import CONFIG_DEFAULTS
55
from unfold.sites import UnfoldAdminSite
66

77

@@ -37,7 +37,6 @@ def test_check_tab_lambda_deny_permission(self):
3737
request = RequestFactory().get("/rand")
3838
tabs = admin_site.get_tabs_list(request)
3939
self.assertEqual(len(tabs[0]["items"]), 0)
40-
get_config.cache_clear()
4140

4241
@override_settings(
4342
UNFOLD={
@@ -62,7 +61,6 @@ def test_check_tab_lambda_allow_permission(self):
6261
request = RequestFactory().get("/rand")
6362
tabs = admin_site.get_tabs_list(request)
6463
self.assertEqual(len(tabs[0]["items"]), 1)
65-
get_config.cache_clear()
6664

6765
@override_settings(
6866
UNFOLD={
@@ -87,7 +85,6 @@ def test_check_tab_path_deny_permission(self):
8785
request = RequestFactory().get("/rand")
8886
tabs = admin_site.get_tabs_list(request)
8987
self.assertEqual(len(tabs[0]["items"]), 0)
90-
get_config.cache_clear()
9188

9289
@override_settings(
9390
UNFOLD={
@@ -112,4 +109,3 @@ def test_check_tab_path_allow_permission(self):
112109
request = RequestFactory().get("/rand")
113110
tabs = admin_site.get_tabs_list(request)
114111
self.assertEqual(len(tabs[0]["items"]), 1)
115-
get_config.cache_clear()

tests/test_show.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from django.test import TestCase
33
from django.test.client import RequestFactory
44
from django.test.utils import override_settings
5-
from unfold.settings import CONFIG_DEFAULTS, get_config
5+
from unfold.settings import CONFIG_DEFAULTS
66
from unfold.sites import UnfoldAdminSite
77

88

@@ -14,7 +14,6 @@ def test_show_history_default(self):
1414
request.user = AnonymousUser()
1515
context = admin_site.each_context(request)
1616
self.assertTrue(context.get("show_history"))
17-
get_config.cache_clear()
1817

1918
@override_settings(UNFOLD={**CONFIG_DEFAULTS, **{"SHOW_HISTORY": False}})
2019
def test_show_history_hide(self):
@@ -23,7 +22,6 @@ def test_show_history_hide(self):
2322
request.user = AnonymousUser()
2423
context = admin_site.each_context(request)
2524
self.assertFalse(context.get("show_history"))
26-
get_config.cache_clear()
2725

2826
@override_settings(UNFOLD={**CONFIG_DEFAULTS})
2927
def test_show_view_on_site_default(self):
@@ -32,7 +30,6 @@ def test_show_view_on_site_default(self):
3230
request.user = AnonymousUser()
3331
context = admin_site.each_context(request)
3432
self.assertTrue(context.get("show_view_on_site"))
35-
get_config.cache_clear()
3633

3734
@override_settings(UNFOLD={**CONFIG_DEFAULTS, **{"SHOW_VIEW_ON_SITE": False}})
3835
def test_show_view_on_site_hide(self):
@@ -41,4 +38,3 @@ def test_show_view_on_site_hide(self):
4138
request.user = AnonymousUser()
4239
context = admin_site.each_context(request)
4340
self.assertFalse(context.get("show_view_on_site"))
44-
get_config.cache_clear()

tests/test_site_branding.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from django.test import TestCase
44
from django.test.client import RequestFactory
55
from django.test.utils import override_settings
6-
from unfold.settings import CONFIG_DEFAULTS, get_config
6+
from unfold.settings import CONFIG_DEFAULTS
77
from unfold.sites import UnfoldAdminSite
88

99

@@ -22,7 +22,6 @@ def test_correct_callback_site_icon(self):
2222
request.user = AnonymousUser()
2323
context = admin_site.each_context(request)
2424
self.assertEqual(context["site_icon"], "icon.svg")
25-
get_config.cache_clear()
2625

2726
@override_settings(
2827
UNFOLD={
@@ -38,7 +37,6 @@ def test_correct_string_site_icon(self):
3837
request.user = AnonymousUser()
3938
context = admin_site.each_context(request)
4039
self.assertEqual(context["site_icon"], "hardcoded-icon.svg")
41-
get_config.cache_clear()
4240

4341
@override_settings(
4442
UNFOLD={
@@ -59,7 +57,6 @@ def test_correct_mode_site_icon(self):
5957
self.assertDictEqual(
6058
context["site_icon"], {"light": "icon-light.svg", "dark": "icon-dark.svg"}
6159
)
62-
get_config.cache_clear()
6360

6461
@override_settings(
6562
UNFOLD={
@@ -77,7 +74,6 @@ def test_incorrect_mode_site_icon(self):
7774
request.user = AnonymousUser()
7875
context = admin_site.each_context(request)
7976
self.assertIsNone(context["site_icon"])
80-
get_config.cache_clear()
8177

8278
@override_settings(
8379
UNFOLD={
@@ -93,7 +89,6 @@ def test_correct_callback_site_logo(self):
9389
request.user = AnonymousUser()
9490
context = admin_site.each_context(request)
9591
self.assertEqual(context["site_logo"], "logo.svg")
96-
get_config.cache_clear()
9792

9893
@override_settings(
9994
UNFOLD={
@@ -109,7 +104,6 @@ def test_correct_string_site_logo(self):
109104
request.user = AnonymousUser()
110105
context = admin_site.each_context(request)
111106
self.assertEqual(context["site_logo"], "hardcoded-logo.svg")
112-
get_config.cache_clear()
113107

114108
@override_settings(
115109
UNFOLD={
@@ -130,7 +124,6 @@ def test_correct_mode_site_logo(self):
130124
self.assertDictEqual(
131125
context["site_logo"], {"light": "logo-light.svg", "dark": "logo-dark.svg"}
132126
)
133-
get_config.cache_clear()
134127

135128
@override_settings(
136129
UNFOLD={
@@ -148,4 +141,3 @@ def test_incorrect_mode_site_logo(self):
148141
request.user = AnonymousUser()
149142
context = admin_site.each_context(request)
150143
self.assertIsNone(context["site_logo"])
151-
get_config.cache_clear()

0 commit comments

Comments
 (0)