<s> from __future__ import absolute_import , unicode_literals <EOL> from django import template <EOL> from django . template . loader import render_to_string <EOL> from wagtail . wagtailadmin . userbar import ( <EOL> AddPageItem , AdminItem , ApproveModerationEditPageItem , EditPageItem , ExplorePageItem , <EOL> RejectModerationEditPageItem ) <EOL> from wagtail . wagtailcore import hooks <EOL> from wagtail . wagtailcore . models import PAGE_TEMPLATE_VAR , Page , PageRevision <EOL> register = template . Library ( ) <EOL> def get_page_instance ( context ) : <EOL> """<STR_LIT>""" <EOL> possible_names = [ PAGE_TEMPLATE_VAR , '<STR_LIT>' ] <EOL> for name in possible_names : <EOL> if name in context : <EOL> page = context [ name ] <EOL> if isinstance ( page , Page ) : <EOL> return page <EOL> @ register . simple_tag ( takes_context = True ) <EOL> def wagtailuserbar ( context , position = '<STR_LIT>' ) : <EOL> try : <EOL> request = context [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> return '<STR_LIT>' <EOL> if not request . user . has_perm ( '<STR_LIT>' ) : <EOL> return '<STR_LIT>' <EOL> page = get_page_instance ( context ) <EOL> if page is None : <EOL> return '<STR_LIT>' <EOL> if page . pk is None : <EOL> return '<STR_LIT>' <EOL> try : <EOL> revision_id = request . revision_id <EOL> except AttributeError : <EOL> revision_id = None <EOL> if revision_id is None : <EOL> items = [ <EOL> AdminItem ( ) , <EOL> ExplorePageItem ( Page . objects . get ( id = page . id ) ) , <EOL> EditPageItem ( Page . objects . get ( id = page . id ) ) , <EOL> AddPageItem ( Page . objects . get ( id = page . id ) ) , <EOL> ] <EOL> else : <EOL> items = [ <EOL> AdminItem ( ) , <EOL> ExplorePageItem ( PageRevision . objects . get ( id = revision_id ) . page ) , <EOL> EditPageItem ( PageRevision . objects . get ( id = revision_id ) . page ) , <EOL> AddPageItem ( PageRevision . objects . get ( id = revision_id ) . page ) , <EOL> ApproveModerationEditPageItem ( PageRevision . objects . get ( id = revision_id ) ) , <EOL> RejectModerationEditPageItem ( PageRevision . objects . get ( id = revision_id ) ) , <EOL> ] <EOL> for fn in hooks . get_hooks ( '<STR_LIT>' ) : <EOL> fn ( request , items ) <EOL> rendered_items = [ item . render ( request ) for item in items ] <EOL> rendered_items = [ item for item in rendered_items if item ] <EOL> return render_to_string ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : request , <EOL> '<STR_LIT>' : rendered_items , <EOL> '<STR_LIT>' : position , <EOL> '<STR_LIT>' : page , <EOL> '<STR_LIT>' : revision_id <EOL> } ) </s>
