Skip to content

Commit

Permalink
feat: show/hide history & view on site (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasvinclav authored Nov 17, 2023
1 parent 5a940bc commit edb5932
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ UNFOLD = {
"dark": lambda request: static("logo-dark.svg"), # dark mode
},
"SITE_SYMBOL": "speed", # symbol from icon set
"SHOW_HISTORY": True, # show/hide "History" button, default: True
"SHOW_VIEW_ON_SITE": True, # show/hide "View on site" button, default: True
"ENVIRONMENT": "sample_app.environment_callback",
"DASHBOARD_CALLBACK": "sample_app.dashboard_callback",
"LOGIN": {
Expand Down
2 changes: 2 additions & 0 deletions src/unfold/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"SITE_ICON": None,
"SITE_SYMBOL": None,
"SITE_LOGO": None,
"SHOW_HISTORY": True,
"SHOW_VIEW_ON_SITE": True,
"COLORS": {
"primary": {
"50": "250 245 255",
Expand Down
4 changes: 4 additions & 0 deletions src/unfold/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ def each_context(self, request: HttpRequest) -> Dict[str, Any]:
"site_symbol": self._get_value(
get_config(self.settings_name)["SITE_SYMBOL"], request
),
"show_history": get_config(self.settings_name)["SHOW_HISTORY"],
"show_view_on_site": get_config(self.settings_name)[
"SHOW_VIEW_ON_SITE"
],
"colors": get_config(self.settings_name)["COLORS"],
"tab_list": self.get_tabs_list(request),
"styles": [
Expand Down
13 changes: 7 additions & 6 deletions src/unfold/templates/admin/change_form_object_tools.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{% load i18n admin_urls %}

{% block object-tools-items %}
{% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %}

{% trans 'History' as title %}
{% add_preserved_filters history_url as link %}
{% include "unfold/helpers/tab_action.html" with title=title link=link %}
{% if show_history %}
{% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %}
{% trans 'History' as title %}
{% add_preserved_filters history_url as link %}
{% include "unfold/helpers/tab_action.html" with title=title link=link %}
{% endif %}

{% if has_absolute_url %}
{% if has_absolute_url and show_view_on_site %}
{% trans 'View on site' as title %}
{% include "unfold/helpers/tab_action.html" with title=title link=absolute_url blank=1 %}
{% endif %}
Expand Down
44 changes: 44 additions & 0 deletions tests/test_show.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from django.contrib.auth.models import AnonymousUser
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.sites import UnfoldAdminSite


class ShowTestCase(TestCase):
@override_settings(UNFOLD={**CONFIG_DEFAULTS})
def test_show_history_default(self):
admin_site = UnfoldAdminSite()
request = RequestFactory().get("/rand")
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):
admin_site = UnfoldAdminSite()
request = RequestFactory().get("/rand")
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):
admin_site = UnfoldAdminSite()
request = RequestFactory().get("/rand")
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):
admin_site = UnfoldAdminSite()
request = RequestFactory().get("/rand")
request.user = AnonymousUser()
context = admin_site.each_context(request)
self.assertFalse(context.get("show_view_on_site"))
get_config.cache_clear()

0 comments on commit edb5932

Please sign in to comment.