forked from numerique-gouv/sites-faciles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwagtail_hooks.py
47 lines (38 loc) · 1.68 KB
/
wagtail_hooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from django.templatetags.static import static
from django.urls import reverse
from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _
from wagtail import hooks
from wagtail.admin.menu import MenuItem
@hooks.register("insert_global_admin_css")
def global_admin_css():
return format_html('<link rel="stylesheet" href="{}">', static("css/admin.css"))
@hooks.register("register_admin_menu_item")
def register_site_menu_item():
return MenuItem(
_("Visit site"),
"/",
icon_name="home",
order=0,
)
class UserbarPageAPILinkItem:
"""
When on a Wagtail page, dd a link to the Page API for admin users in the wagtail toolbar
"""
def render(self, request) -> str:
if hasattr(request, "_wagtail_route_for_request"):
page = request._wagtail_route_for_request.page
page_url = reverse("wagtailapi:pages:detail", kwargs={"pk": page.id})
page_in_api_label = _("See page entry in API")
if page and request.user.has_perm("wagtailadmin.access_admin"):
return f"""<li class="w-userbar__item " role="presentation">
<a href="{page_url}" target="_parent" role="menuitem" tabindex="-1">
<svg class="icon icon-crosshairs w-action-icon" aria-hidden="true">
<use href="#icon-crosshairs"></use>
</svg> {page_in_api_label}
</a>
</li>"""
return ""
@hooks.register("construct_wagtail_userbar")
def add_page_api_link_item(request, items):
return items.append(UserbarPageAPILinkItem())