Skip to content

Commit 821bfc6

Browse files
committed
Pass context to toolbar, add admin object link
1 parent 79654db commit 821bfc6

File tree

4 files changed

+44
-23
lines changed

4 files changed

+44
-23
lines changed

plain-admin/plain/admin/templates/admin/toolbar/button.html

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
{% if request.impersonator is defined %}
2-
<div class="flex items-center font-light">
3-
Impersonating&nbsp;<span class="font-medium">{{ request.user }}</span>
4-
<a href="{{ url('admin:impersonate:stop') }}" title="Stop impersonating" class="flex items-center px-1 ml-1 text-red-300 hover:text-white">
5-
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="w-4 h-4 bi bi-x-octagon-fill" viewBox="0 0 16 16">
6-
<path d="M11.46.146A.5.5 0 0 0 11.107 0H4.893a.5.5 0 0 0-.353.146L.146 4.54A.5.5 0 0 0 0 4.893v6.214a.5.5 0 0 0 .146.353l4.394 4.394a.5.5 0 0 0 .353.146h6.214a.5.5 0 0 0 .353-.146l4.394-4.394a.5.5 0 0 0 .146-.353V4.893a.5.5 0 0 0-.146-.353L11.46.146zm-6.106 4.5L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 1 1 .708-.708z"/>
7-
</svg>
1+
<div class="flex items-center space-x-1">
2+
{% if request.impersonator is defined %}
3+
<div class="flex items-center font-light">
4+
Impersonating&nbsp;<span class="font-medium">{{ request.user }}</span>
5+
<a href="{{ url('admin:impersonate:stop') }}" title="Stop impersonating" class="flex items-center px-1 ml-1 text-red-300 hover:text-white">
6+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="w-4 h-4 bi bi-x-octagon-fill" viewBox="0 0 16 16">
7+
<path d="M11.46.146A.5.5 0 0 0 11.107 0H4.893a.5.5 0 0 0-.353.146L.146 4.54A.5.5 0 0 0 0 4.893v6.214a.5.5 0 0 0 .146.353l4.394 4.394a.5.5 0 0 0 .353.146h6.214a.5.5 0 0 0 .353-.146l4.394-4.394a.5.5 0 0 0 .146-.353V4.893a.5.5 0 0 0-.146-.353L11.46.146zm-6.106 4.5L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 1 1 .708-.708z"/>
8+
</svg>
9+
</a>
10+
</div>
11+
{% endif %}
12+
13+
<a href="{{ url('admin:index') }}" class="hover:underline">Admin</a>
14+
15+
{% if object_admin_url %}
16+
<span></span>
17+
<a href="{{ object_admin_url }}" class="hover:underline">
18+
{{ object_class_name }}
819
</a>
20+
{% endif %}
921
</div>
10-
{% endif %}
11-
12-
<a href="{{ url('admin:index') }}" class="hover:underline">Admin</a>

plain-admin/plain/admin/toolbar.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
from plain.toolbar import ToolbarItem, register_toolbar_item
22

3+
from .views.registry import registry
4+
35

46
@register_toolbar_item
57
class AdminToolbarItem(ToolbarItem):
68
name = "Admin"
79
button_template_name = "admin/toolbar/button.html"
10+
11+
def get_template_context(self):
12+
context = super().get_template_context()
13+
# Add admin-specific context for the object if it exists
14+
if "object" in context:
15+
obj = context["object"]
16+
context["object_admin_url"] = registry.get_model_detail_url(obj)
17+
context["object_class_name"] = obj.__class__.__name__
18+
return context

plain-toolbar/plain/toolbar/templates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ class ToolbarExtension(InclusionTagExtension):
1010
template_name = "toolbar/toolbar.html"
1111

1212
def get_context(self, context, *args, **kwargs):
13-
context.vars["toolbar"] = Toolbar(request=context["request"])
13+
context.vars["toolbar"] = Toolbar(context=context)
1414
return context

plain-toolbar/plain/toolbar/toolbar.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111

1212
class Toolbar:
13-
def __init__(self, request):
14-
self.request = request
13+
def __init__(self, context):
14+
self.context = context
15+
self.request = context["request"]
1516
self.version = settings.APP_VERSION
1617

1718
def should_render(self):
@@ -37,12 +38,12 @@ def request_exception(self):
3738
return exception
3839

3940
def get_items(self):
40-
items = [item(self.request) for item in registry.get_items()]
41+
items = [item(self.context) for item in registry.get_items()]
4142

4243
if self.request_exception():
4344
exception = self.request_exception()
4445
items = [
45-
_ExceptionToolbarItem(self.request, exception),
46+
_ExceptionToolbarItem(self.context, exception),
4647
] + items
4748

4849
return items
@@ -53,14 +54,14 @@ class ToolbarItem:
5354
panel_template_name: str = ""
5455
button_template_name: str = ""
5556

56-
def __init__(self, request):
57-
self.request = request
57+
def __init__(self, context):
58+
self.context = context
59+
self.request = context["request"]
5860

5961
def get_template_context(self):
60-
return {
61-
"request": self.request,
62-
"panel": self,
63-
}
62+
context = dict(self.context)
63+
context["panel"] = self
64+
return context
6465

6566
def render_panel(self):
6667
template = Template(self.panel_template_name)
@@ -79,8 +80,8 @@ class _ExceptionToolbarItem(ToolbarItem):
7980
panel_template_name = "toolbar/exception.html"
8081
button_template_name = "toolbar/exception_button.html"
8182

82-
def __init__(self, request, exception):
83-
super().__init__(request)
83+
def __init__(self, context, exception):
84+
super().__init__(context)
8485
self.exception = exception
8586

8687
def get_template_context(self):

0 commit comments

Comments
 (0)