Skip to content

Commit edb5932

Browse files
authored
feat: show/hide history & view on site (#201)
1 parent 5a940bc commit edb5932

File tree

5 files changed

+59
-6
lines changed

5 files changed

+59
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ UNFOLD = {
165165
"dark": lambda request: static("logo-dark.svg"), # dark mode
166166
},
167167
"SITE_SYMBOL": "speed", # symbol from icon set
168+
"SHOW_HISTORY": True, # show/hide "History" button, default: True
169+
"SHOW_VIEW_ON_SITE": True, # show/hide "View on site" button, default: True
168170
"ENVIRONMENT": "sample_app.environment_callback",
169171
"DASHBOARD_CALLBACK": "sample_app.dashboard_callback",
170172
"LOGIN": {

src/unfold/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"SITE_ICON": None,
1010
"SITE_SYMBOL": None,
1111
"SITE_LOGO": None,
12+
"SHOW_HISTORY": True,
13+
"SHOW_VIEW_ON_SITE": True,
1214
"COLORS": {
1315
"primary": {
1416
"50": "250 245 255",

src/unfold/sites.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ def each_context(self, request: HttpRequest) -> Dict[str, Any]:
6464
"site_symbol": self._get_value(
6565
get_config(self.settings_name)["SITE_SYMBOL"], request
6666
),
67+
"show_history": get_config(self.settings_name)["SHOW_HISTORY"],
68+
"show_view_on_site": get_config(self.settings_name)[
69+
"SHOW_VIEW_ON_SITE"
70+
],
6771
"colors": get_config(self.settings_name)["COLORS"],
6872
"tab_list": self.get_tabs_list(request),
6973
"styles": [

src/unfold/templates/admin/change_form_object_tools.html

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{% load i18n admin_urls %}
22

33
{% block object-tools-items %}
4-
{% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %}
5-
6-
{% trans 'History' as title %}
7-
{% add_preserved_filters history_url as link %}
8-
{% include "unfold/helpers/tab_action.html" with title=title link=link %}
4+
{% if show_history %}
5+
{% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %}
6+
{% trans 'History' as title %}
7+
{% add_preserved_filters history_url as link %}
8+
{% include "unfold/helpers/tab_action.html" with title=title link=link %}
9+
{% endif %}
910

10-
{% if has_absolute_url %}
11+
{% if has_absolute_url and show_view_on_site %}
1112
{% trans 'View on site' as title %}
1213
{% include "unfold/helpers/tab_action.html" with title=title link=absolute_url blank=1 %}
1314
{% endif %}

tests/test_show.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from django.contrib.auth.models import AnonymousUser
2+
from django.test import TestCase
3+
from django.test.client import RequestFactory
4+
from django.test.utils import override_settings
5+
from unfold.settings import CONFIG_DEFAULTS, get_config
6+
from unfold.sites import UnfoldAdminSite
7+
8+
9+
class ShowTestCase(TestCase):
10+
@override_settings(UNFOLD={**CONFIG_DEFAULTS})
11+
def test_show_history_default(self):
12+
admin_site = UnfoldAdminSite()
13+
request = RequestFactory().get("/rand")
14+
request.user = AnonymousUser()
15+
context = admin_site.each_context(request)
16+
self.assertTrue(context.get("show_history"))
17+
get_config.cache_clear()
18+
19+
@override_settings(UNFOLD={**CONFIG_DEFAULTS, **{"SHOW_HISTORY": False}})
20+
def test_show_history_hide(self):
21+
admin_site = UnfoldAdminSite()
22+
request = RequestFactory().get("/rand")
23+
request.user = AnonymousUser()
24+
context = admin_site.each_context(request)
25+
self.assertFalse(context.get("show_history"))
26+
get_config.cache_clear()
27+
28+
@override_settings(UNFOLD={**CONFIG_DEFAULTS})
29+
def test_show_view_on_site_default(self):
30+
admin_site = UnfoldAdminSite()
31+
request = RequestFactory().get("/rand")
32+
request.user = AnonymousUser()
33+
context = admin_site.each_context(request)
34+
self.assertTrue(context.get("show_view_on_site"))
35+
get_config.cache_clear()
36+
37+
@override_settings(UNFOLD={**CONFIG_DEFAULTS, **{"SHOW_VIEW_ON_SITE": False}})
38+
def test_show_view_on_site_hide(self):
39+
admin_site = UnfoldAdminSite()
40+
request = RequestFactory().get("/rand")
41+
request.user = AnonymousUser()
42+
context = admin_site.each_context(request)
43+
self.assertFalse(context.get("show_view_on_site"))
44+
get_config.cache_clear()

0 commit comments

Comments
 (0)