1from django.contrib.auth.decorators import permission_required
2from django.template.response import TemplateResponse
3
4from wagtail.admin.userbar import (
5    AddPageItem, ApproveModerationEditPageItem, EditPageItem, RejectModerationEditPageItem)
6from wagtail.core import hooks
7from wagtail.core.models import Page, PageRevision
8
9
10@permission_required('wagtailadmin.access_admin', raise_exception=True)
11def for_frontend(request, page_id):
12    items = [
13        EditPageItem(Page.objects.get(id=page_id)),
14        AddPageItem(Page.objects.get(id=page_id)),
15    ]
16
17    for fn in hooks.get_hooks('construct_wagtail_userbar'):
18        fn(request, items)
19
20    # Render the items
21    rendered_items = [item.render(request) for item in items]
22
23    # Remove any unrendered items
24    rendered_items = [item for item in rendered_items if item]
25
26    # Render the edit bird
27    return TemplateResponse(request, 'wagtailadmin/userbar/base.html', {
28        'items': rendered_items,
29    })
30
31
32@permission_required('wagtailadmin.access_admin', raise_exception=True)
33def for_moderation(request, revision_id):
34    items = [
35        EditPageItem(PageRevision.objects.get(id=revision_id).page),
36        AddPageItem(PageRevision.objects.get(id=revision_id).page),
37        ApproveModerationEditPageItem(PageRevision.objects.get(id=revision_id)),
38        RejectModerationEditPageItem(PageRevision.objects.get(id=revision_id)),
39    ]
40
41    for fn in hooks.get_hooks('construct_wagtail_userbar'):
42        fn(request, items)
43
44    # Render the items
45    rendered_items = [item.render(request) for item in items]
46
47    # Remove any unrendered items
48    rendered_items = [item for item in rendered_items if item]
49
50    # Render the edit bird
51    return TemplateResponse(request, 'wagtailadmin/userbar/base.html', {
52        'items': rendered_items,
53    })
54