<s> from django import template <EOL> from django . template . loader import render_to_string <EOL> from django . utils . safestring import mark_safe <EOL> import corehq . apps . style . utils as style_utils <EOL> from corehq . tabs import MENU_TABS <EOL> from corehq . tabs . exceptions import TabClassError , TabClassErrorSummary <EOL> from corehq . tabs . utils import path_starts_with_url <EOL> register = template . Library ( ) <EOL> def _get_active_tab ( visible_tabs , request_path ) : <EOL> """<STR_LIT>""" <EOL> matching_tabs = sorted ( <EOL> ( url_prefix , tab ) <EOL> for tab in visible_tabs <EOL> for url_prefix in tab . url_prefixes <EOL> if request_path . startswith ( url_prefix ) <EOL> ) <EOL> if matching_tabs : <EOL> _ , tab = matching_tabs [ - <NUM_LIT:1> ] <EOL> return tab <EOL> def get_all_tabs ( request , domain , couch_user , project ) : <EOL> """<STR_LIT>""" <EOL> all_tabs = [ ] <EOL> instantiation_errors = [ ] <EOL> for tab_class in MENU_TABS : <EOL> try : <EOL> tab = tab_class ( <EOL> request , domain = domain , <EOL> couch_user = couch_user , project = project ) <EOL> except TabClassError as e : <EOL> instantiation_errors . append ( e ) <EOL> else : <EOL> all_tabs . append ( tab ) <EOL> if instantiation_errors : <EOL> messages = ( <EOL> '<STR_LIT>' . format ( e . __class__ . __name__ , e . message ) <EOL> for e in instantiation_errors <EOL> ) <EOL> summary_message = '<STR_LIT>' . format ( '<STR_LIT:\n>' . join ( messages ) ) <EOL> raise TabClassErrorSummary ( summary_message ) <EOL> else : <EOL> return all_tabs <EOL> class MainMenuNode ( template . Node ) : <EOL> def render ( self , context ) : <EOL> request = context [ '<STR_LIT>' ] <EOL> couch_user = getattr ( request , '<STR_LIT>' , None ) <EOL> project = getattr ( request , '<STR_LIT>' , None ) <EOL> domain = context . get ( '<STR_LIT>' ) <EOL> all_tabs = get_all_tabs ( request , domain = domain , couch_user = couch_user , <EOL> project = project ) <EOL> active_tab = _get_active_tab ( all_tabs , request . get_full_path ( ) ) <EOL> if active_tab : <EOL> active_tab . is_active_tab = True <EOL> visible_tabs = [ tab for tab in all_tabs if tab . should_show ( ) ] <EOL> context . dicts [ <NUM_LIT:0> ] [ '<STR_LIT>' ] = active_tab <EOL> return mark_safe ( render_to_string ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : visible_tabs , <EOL> } ) ) <EOL> @ register . tag ( name = "<STR_LIT>" ) <EOL> def format_main_menu ( parser , token ) : <EOL> return MainMenuNode ( ) <EOL> @ register . simple_tag ( takes_context = True ) <EOL> def format_sidebar ( context ) : <EOL> current_url_name = context [ '<STR_LIT>' ] <EOL> active_tab = context . get ( '<STR_LIT>' , None ) <EOL> request = context [ '<STR_LIT>' ] <EOL> sections = active_tab . sidebar_items if active_tab else None <EOL> if sections : <EOL> for section_title , navs in sections : <EOL> for nav in navs : <EOL> full_path = request . get_full_path ( ) <EOL> if path_starts_with_url ( full_path , nav [ '<STR_LIT:url>' ] ) : <EOL> nav [ '<STR_LIT>' ] = True <EOL> else : <EOL> nav [ '<STR_LIT>' ] = False <EOL> if '<STR_LIT>' in nav : <EOL> for subpage in nav [ '<STR_LIT>' ] : <EOL> if subpage [ '<STR_LIT>' ] == current_url_name : <EOL> if callable ( subpage [ '<STR_LIT:title>' ] ) : <EOL> actual_context = { } <EOL> for d in context . dicts : <EOL> actual_context . update ( d ) <EOL> subpage [ '<STR_LIT>' ] = True <EOL> subpage [ '<STR_LIT:title>' ] = subpage [ '<STR_LIT:title>' ] ( ** actual_context ) <EOL> nav [ '<STR_LIT>' ] = subpage <EOL> break <EOL> template = { <EOL> style_utils . BOOTSTRAP_2 : '<STR_LIT>' , <EOL> style_utils . BOOTSTRAP_3 : '<STR_LIT>' , <EOL> } [ style_utils . get_bootstrap_version ( ) ] <EOL> return mark_safe ( render_to_string ( template , { <EOL> '<STR_LIT>' : sections <EOL> } ) ) </s>
