Skip to content

Commit

Permalink
feat: lambda support in links (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasvinclav committed Jan 14, 2024
1 parent a1a2e6d commit 06efaa5
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 31 deletions.
3 changes: 0 additions & 3 deletions src/unfold/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from functools import lru_cache

from django.conf import settings

CONFIG_DEFAULTS = {
Expand Down Expand Up @@ -44,7 +42,6 @@
}


@lru_cache
def get_config(settings_name=None):
if settings_name is None:
settings_name = "UNFOLD"
Expand Down
6 changes: 6 additions & 0 deletions src/unfold/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ def _get_is_active(link: str) -> bool:
if has_primary_link and has_tab_link_active:
item["active"] = True

if isinstance(item["link"], Callable):
item["link"] = item["link"](request)

# Badge callbacks
if "badge" in item and isinstance(item["badge"], str):
try:
Expand Down Expand Up @@ -284,6 +287,9 @@ def get_tabs_list(self, request: HttpRequest) -> List[Dict[str, Any]]:
if not self._call_permission_callback(item.get("permission"), request):
continue

if isinstance(item["link"], Callable):
item["link"] = item["link"](request)

allowed_items.append(item)

tab["items"] = allowed_items
Expand Down
6 changes: 1 addition & 5 deletions tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.test import TestCase
from django.test.client import RequestFactory
from django.test.utils import override_settings
from unfold.settings import CONFIG_DEFAULTS, get_config
from unfold.settings import CONFIG_DEFAULTS
from unfold.sites import UnfoldAdminSite


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

get_config.cache_clear()

@override_settings(
UNFOLD={
**CONFIG_DEFAULTS,
Expand Down Expand Up @@ -92,5 +90,3 @@ def test_colors_rgb(self):
self.assertEqual(context["colors"]["primary"][800], "7 89 133")
self.assertEqual(context["colors"]["primary"][900], "12 74 110")
self.assertEqual(context["colors"]["primary"][950], "8 47 73")

get_config.cache_clear()
5 changes: 1 addition & 4 deletions tests/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.test import TestCase
from django.test.client import RequestFactory
from django.test.utils import override_settings
from unfold.settings import CONFIG_DEFAULTS, get_config
from unfold.settings import CONFIG_DEFAULTS
from unfold.sites import UnfoldAdminSite


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

@override_settings(
UNFOLD={
Expand All @@ -34,7 +33,6 @@ def test_incorrect_environment_callback(self):
request.user = AnonymousUser()
context = admin_site.each_context(request)
self.assertTrue("environment" not in context)
get_config.cache_clear()

@override_settings(
UNFOLD={
Expand All @@ -51,4 +49,3 @@ def test_correct_environment_callback(self):
context = admin_site.each_context(request)
self.assertTrue("environment" in context)
self.assertEqual(context["environment"], ["Testing Environment", "warning"])
get_config.cache_clear()
6 changes: 1 addition & 5 deletions tests/test_navigations.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.test import TestCase
from django.test.client import RequestFactory
from django.test.utils import override_settings
from unfold.settings import CONFIG_DEFAULTS, get_config
from unfold.settings import CONFIG_DEFAULTS
from unfold.sites import UnfoldAdminSite


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

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

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

@override_settings(
UNFOLD={
Expand All @@ -112,4 +109,3 @@ def test_check_tab_path_allow_permission(self):
request = RequestFactory().get("/rand")
tabs = admin_site.get_tabs_list(request)
self.assertEqual(len(tabs[0]["items"]), 1)
get_config.cache_clear()
6 changes: 1 addition & 5 deletions tests/test_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.test import TestCase
from django.test.client import RequestFactory
from django.test.utils import override_settings
from unfold.settings import CONFIG_DEFAULTS, get_config
from unfold.settings import CONFIG_DEFAULTS
from unfold.sites import UnfoldAdminSite


Expand All @@ -14,7 +14,6 @@ def test_show_history_default(self):
request.user = AnonymousUser()
context = admin_site.each_context(request)
self.assertTrue(context.get("show_history"))
get_config.cache_clear()

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

@override_settings(UNFOLD={**CONFIG_DEFAULTS})
def test_show_view_on_site_default(self):
Expand All @@ -32,7 +30,6 @@ def test_show_view_on_site_default(self):
request.user = AnonymousUser()
context = admin_site.each_context(request)
self.assertTrue(context.get("show_view_on_site"))
get_config.cache_clear()

@override_settings(UNFOLD={**CONFIG_DEFAULTS, **{"SHOW_VIEW_ON_SITE": False}})
def test_show_view_on_site_hide(self):
Expand All @@ -41,4 +38,3 @@ def test_show_view_on_site_hide(self):
request.user = AnonymousUser()
context = admin_site.each_context(request)
self.assertFalse(context.get("show_view_on_site"))
get_config.cache_clear()
10 changes: 1 addition & 9 deletions tests/test_site_branding.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.test import TestCase
from django.test.client import RequestFactory
from django.test.utils import override_settings
from unfold.settings import CONFIG_DEFAULTS, get_config
from unfold.settings import CONFIG_DEFAULTS
from unfold.sites import UnfoldAdminSite


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

@override_settings(
UNFOLD={
Expand All @@ -38,7 +37,6 @@ def test_correct_string_site_icon(self):
request.user = AnonymousUser()
context = admin_site.each_context(request)
self.assertEqual(context["site_icon"], "hardcoded-icon.svg")
get_config.cache_clear()

@override_settings(
UNFOLD={
Expand All @@ -59,7 +57,6 @@ def test_correct_mode_site_icon(self):
self.assertDictEqual(
context["site_icon"], {"light": "icon-light.svg", "dark": "icon-dark.svg"}
)
get_config.cache_clear()

@override_settings(
UNFOLD={
Expand All @@ -77,7 +74,6 @@ def test_incorrect_mode_site_icon(self):
request.user = AnonymousUser()
context = admin_site.each_context(request)
self.assertIsNone(context["site_icon"])
get_config.cache_clear()

@override_settings(
UNFOLD={
Expand All @@ -93,7 +89,6 @@ def test_correct_callback_site_logo(self):
request.user = AnonymousUser()
context = admin_site.each_context(request)
self.assertEqual(context["site_logo"], "logo.svg")
get_config.cache_clear()

@override_settings(
UNFOLD={
Expand All @@ -109,7 +104,6 @@ def test_correct_string_site_logo(self):
request.user = AnonymousUser()
context = admin_site.each_context(request)
self.assertEqual(context["site_logo"], "hardcoded-logo.svg")
get_config.cache_clear()

@override_settings(
UNFOLD={
Expand All @@ -130,7 +124,6 @@ def test_correct_mode_site_logo(self):
self.assertDictEqual(
context["site_logo"], {"light": "logo-light.svg", "dark": "logo-dark.svg"}
)
get_config.cache_clear()

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

0 comments on commit 06efaa5

Please sign in to comment.