def test_active_in_resource_controller_legacy_pylon_routes( self, test_request_context): dataset = factories.Dataset() with test_request_context(u'/dataset/' + dataset['id']): menu = ( ("home", "Home"), ("search", "Datasets", ['dataset', 'resource']), ("organizations_index", "Organizations"), ("group_index", "Groups"), ("about", "About"), ) assert h.build_nav_main(*menu) == ( '<li><a href="/">Home</a></li>' '<li class="active"><a href="/dataset/">Datasets</a></li>' '<li><a href="/organization/">Organizations</a></li>' '<li><a href="/group/">Groups</a></li>' '<li><a href="/about">About</a></li>') resource = factories.Resource(name="some_resource") with test_request_context(u'/dataset/' + resource['package_id'] + '/resource/' + resource['id']): menu = ( ("home", "Home"), ("search", "Datasets", ['dataset', 'resource']), ("organizations_index", "Organizations"), ("group_index", "Groups"), ("about", "About"), ) assert h.build_nav_main(*menu) == ( '<li><a href="/">Home</a></li>' '<li class="active"><a href="/dataset/">Datasets</a></li>' '<li><a href="/organization/">Organizations</a></li>' '<li><a href="/group/">Groups</a></li>' '<li><a href="/about">About</a></li>')
def uebpackage_build_main_navigation(*args): output = h.build_nav_main(*args) # show ciwater related menu options only if the user is loggedin if c.user: link_pkg = h.url_for(controller='ckanext.uebpackage.controllers.packagecreate:PackagecreateController', action='packagecreateform') link_run = h.url_for(controller='ckanext.uebpackage.controllers.uebexecute:UEBexecuteController', action='select_model_package') if p.toolkit.c.action == 'packagecreateform' or p.toolkit.c.action == 'select_model_package': menu = h.literal('<li class="dropdown">') else: menu = h.literal('<li class="dropdown">') menu += h.literal('<a id="drop1" role="button" data-toggle="dropdown" class="dropdown-toggle" href="#">') menu += 'UEB' + h.literal('<b><span class="caret"></span></b>') menu += h.literal('</a>') menu += h.literal('<ul class="dropdown-menu" role="menu" area-labelledby="drop1">') li = h.literal('<li role="presentation"><a role="menuitem" tabindex="-1" href=') + link_pkg + h.literal('>UEB Model Package</a>') + h.literal('</li>') menu += li li = h.literal('<li role="presentation"><a role="menuitem" tabindex="-1" href=') + link_run + h.literal('>Run UEB</a>') + h.literal('</li>') menu += li menu += h.literal('</ul>') menu += h.literal('</li>') output = menu + output return output
def build_pages_nav_main(*args): about_menu = toolkit.asbool(config.get('ckanext.pages.about_menu', False)) group_menu = toolkit.asbool(config.get('ckanext.pages.group_menu', False)) org_menu = toolkit.asbool( config.get('ckanext.pages.organization_menu', True)) new_args = [] for arg in args: if arg[0] == 'home.about' and not about_menu: continue if arg[0] == 'organizations_index' and not org_menu: continue if arg[0] == 'group_index' and not group_menu: continue new_args.append(arg) output = h.build_nav_main(*new_args) # add forum link forum_url = config.get('ckan.pages.forum.link', False) if forum_url: link = h.literal(u'<a href="/{}/{}">{}</a>'.format( h.lang(), 'forum', "Forum")) li = h.literal('<li>') + link + h.literal('</li>') output = output + li # do not display any private datasets in menu even for sysadmins pages_list = toolkit.get_action('ckanext_pages_list')(None, { 'order': True, 'private': False }) page_name = '' if (toolkit.c.action in ('pages_show', 'blog_show') and toolkit.c.controller == 'ckanext.pages.controller:PagesController'): page_name = toolkit.c.environ['routes.url'].current().split('/')[-1] for page in pages_list: type_ = 'blog' if page['page_type'] == 'blog' else 'pages' name = urllib.quote(page['name'].encode('utf-8')).decode('utf-8') title = cgi.escape(pages_page_title(h.lang(), page)) if h.lang(): link = h.literal(u'<a href="/{}/{}/{}">{}</a>'.format( h.lang(), type_, name, title)) else: link = h.literal(u'<a href="/{}/{}">{}</a>'.format( type_, name, title)) if page['name'] == page_name: li = h.literal('<li class="active">') + link + h.literal('</li>') else: li = h.literal('<li>') + link + h.literal('</li>') output = output + li return output
def build_pages_nav_main(*args): about_menu = p.toolkit.asbool(config.get('ckanext.pages.about_menu', True)) group_menu = p.toolkit.asbool(config.get('ckanext.pages.group_menu', True)) org_menu = p.toolkit.asbool( config.get('ckanext.pages.organization_menu', True)) new_args = [] for arg in args: if arg[0] == 'about' and not about_menu: continue if arg[0] == 'organizations_index' and not org_menu: continue if arg[0] == 'group_index' and not group_menu: continue new_args.append(arg) output = h.build_nav_main(*new_args) # do not display any private datasets in menu even for sysadmins pages_list = p.toolkit.get_action('ckanext_pages_list')(None, { 'order': True, 'private': False }) page_name = '' if (p.toolkit.c.action in ('pages_show', 'blog_show') and p.toolkit.c.controller == 'ckanext.pages.controller:PagesController'): page_name = p.toolkit.c.environ['routes.url'].current().split('/')[-1] desired_lang_code = pylons.request.environ['CKAN_LANG'] acceptable_lang_codes = [ desired_lang_code, desired_lang_code.split('_', 1)[0] ] for page in pages_list: if page.get('lang') and page.get('lang') not in acceptable_lang_codes: continue if page['page_type'] == 'blog': link = h.tags.link_to(page.get('title'), h.url_for('/blog/' + str(page['name']))) else: link = h.tags.link_to(page.get('title'), h.url_for('/pages/' + str(page['name']))) if page['name'] == page_name: li = h.literal('<li class="active">') + link + h.literal('</li>') else: li = h.literal('<li>') + link + h.literal('</li>') output = output + li return output
def test_legacy_pylon_routes(self): menu = (('home', 'Home'), ('search', 'Datasets'), ('organizations_index', 'Organizations'), ('group_index', 'Groups'), ('about', 'About')) eq_(h.build_nav_main(*menu), ('<li><a href="/">Home</a></li>' '<li><a href="/dataset/">Datasets</a></li>' '<li><a href="/organization/">Organizations</a></li>' '<li><a href="/group/">Groups</a></li>' '<li><a href="/about">About</a></li>'))
def build_pages_nav_main(*args): about_menu = toolkit.asbool(config.get('ckanext.pages.about_menu', True)) group_menu = toolkit.asbool(config.get('ckanext.pages.group_menu', True)) org_menu = toolkit.asbool( config.get('ckanext.pages.organization_menu', True)) new_args = [] for arg in args: if arg[0] == 'about' and not about_menu: continue if arg[0] == 'organizations_index' and not org_menu: continue if arg[0] == 'group_index' and not group_menu: continue new_args.append(arg) output = h.build_nav_main(*new_args) # do not display any private datasets in menu even for sysadmins pages_list = toolkit.get_action('ckanext_pages_list')(None, { 'order': True, 'private': False }) page_name = '' if (toolkit.c.action in ('pages_show', 'blog_show') and toolkit.c.controller == 'ckanext.pages.controller:PagesController'): page_name = toolkit.c.environ['routes.url'].current().split('/')[-1] desired_lang_code = pylons.request.environ['CKAN_LANG'] acceptable_lang_codes = [ desired_lang_code, desired_lang_code.split('_', 1)[0] ] for page in pages_list: if page.get('lang') and page.get('lang') not in acceptable_lang_codes: continue type_ = 'blog' if page['page_type'] == 'blog' else 'pages' name = urllib.quote(page['name'].encode('utf-8')).decode('utf-8') title = cgi.escape(page['title']) link = h.literal(u'<a href="/{}/{}">{}</a>'.format(type_, name, title)) if page['name'] == page_name: li = h.literal('<li class="active">') + link + h.literal('</li>') else: li = h.literal('<li>') + link + h.literal('</li>') output = output + li return output
def build_pages_nav_main(*args): about_menu = toolkit.asbool(config.get('ckanext.pages.about_menu', True)) group_menu = toolkit.asbool(config.get('ckanext.pages.group_menu', True)) org_menu = toolkit.asbool( config.get('ckanext.pages.organization_menu', True)) new_args = [] for arg in args: if arg[0] == 'about' and not about_menu: continue if arg[0] == 'organizations_index' and not org_menu: continue if arg[0] == 'group_index' and not group_menu: continue new_args.append(arg) output = h.build_nav_main(*new_args) # do not display any private datasets in menu even for sysadmins pages_list = toolkit.get_action('ckanext_pages_list')(None, { 'order': True, 'private': False }) page_name = '' try: if (toolkit.c.action in ('pages_show', 'blog_show') and toolkit.c.controller == 'ckanext.pages.controller:PagesController'): page_name = toolkit.c.environ['routes.url'].current().split( '/')[-1] except AttributeError: # NOTE(e0ne): we don't have 'action' attribute in Flask context. # We can igrore if it's Flask Bluprint-bases plugin pass for page in pages_list: if page['page_type'] == 'blog': link = h.literal('<a href="/blog/%s">%s</a>' % (str(page['name']), str(page['title']))) else: link = h.literal('<a href="/pages/%s">%s</a>' % (str(page['name']), str(page['title']))) if page['name'] == page_name: li = h.literal('<li class="active">') + link + h.literal('</li>') else: li = h.literal('<li>') + link + h.literal('</li>') output = output + li return output
def delineate_build_main_navigation(*args): output = h.build_nav_main(*args) if c.user: link = h.link_to('Delineate', h.url_for(controller='ckanext.delineate.controllers.delineatewatershed:DelineatewatershedController', action='show_form')) if p.toolkit.c.action == 'show_form': li = h.literal('<li class="active">') + link + h.literal('</li>') else: li = h.literal('<li>') + link + h.literal('</li>') output = li + output return output
def build_pages_nav_main(*args): about_menu = p.toolkit.asbool(config.get('ckanext.pages.about_menu', True)) group_menu = p.toolkit.asbool(config.get('ckanext.pages.group_menu', True)) org_menu = p.toolkit.asbool( config.get('ckanext.pages.organization_menu', True)) new_args = [] for arg in args: if arg[0] == 'about' and not about_menu: continue if arg[0] == 'organizations_index' and not org_menu: continue if arg[0] == 'group_index' and not group_menu: continue new_args.append(arg) output = h.build_nav_main(*new_args) # do not display any private datasets in menu even for sysadmins pages_list = p.toolkit.get_action('ckanext_pages_list')(None, { 'order': True, 'private': False }) page_name = '' if (p.toolkit.c.action in ('pages_show', 'blog_show') and p.toolkit.c.controller == 'ckanext.pages.controller:PagesController'): page_name = p.toolkit.c.environ['routes.url'].current().split('/')[-1] for page in pages_list: if page['page_type'] == 'blog': link = h.literal( '<a href="/blog/%s">%s</a>' % (page['name'].encode('ascii'), page['title'].encode( 'ascii', 'xmlcharrefreplace'))) else: link = h.literal( '<a href="/pages/%s">%s</a>' % (page['name'].encode('ascii'), page['title'].encode( 'ascii', 'xmlcharrefreplace'))) if page['name'] == page_name: li = h.literal('<li class="active">') + link + h.literal('</li>') else: li = h.literal('<li>') + link + h.literal('</li>') output = output + li return output
def test_flask_routes(self): menu = ( ("home.index", "Home"), ("dataset.search", "Datasets"), ("organization.index", "Organizations"), ("group.index", "Groups"), ("home.about", "About"), ) assert h.build_nav_main( *menu) == ('<li><a href="/">Home</a></li>' '<li><a href="/dataset/">Datasets</a></li>' '<li><a href="/organization/">Organizations</a></li>' '<li><a href="/group/">Groups</a></li>' '<li><a href="/about">About</a></li>')
def build_pages_nav_main(*args): about_menu = toolkit.asbool(config.get('ckanext.pages.about_menu', True)) group_menu = toolkit.asbool(config.get('ckanext.pages.group_menu', True)) org_menu = toolkit.asbool( config.get('ckanext.pages.organization_menu', True)) # Different CKAN versions use different route names - gotta catch em all! about_menu_routes = ['about', 'home.about'] group_menu_routes = ['group_index', 'home.group_index'] org_menu_routes = ['organizations_index', 'home.organizations_index'] new_args = [] for arg in args: if arg[0] in about_menu_routes and not about_menu: continue if arg[0] in org_menu_routes and not org_menu: continue if arg[0] in group_menu_routes and not group_menu: continue new_args.append(arg) output = h.build_nav_main(*new_args) # do not display any private datasets in menu even for sysadmins pages_list = toolkit.get_action('ckanext_pages_list')(None, { 'order': True, 'private': False }) page_name = '' if (hasattr(toolkit.c, 'action') and toolkit.c.action in ('pages_show', 'blog_show') and toolkit.c.controller == 'ckanext.pages.controller:PagesController'): page_name = toolkit.c.environ['routes.url'].current().split('/')[-1] for page in pages_list: type_ = 'blog' if page['page_type'] == 'blog' else 'pages' name = urllib.quote(page['name'].encode('utf-8')).decode('utf-8') title = cgi.escape(page['title']) link = h.literal(u'<a href="/{}/{}">{}</a>'.format(type_, name, title)) if page['name'] == page_name: li = h.literal('<li class="active">') + link + h.literal('</li>') else: li = h.literal('<li>') + link + h.literal('</li>') output = output + li return output
def test_legacy_pylon_routes(self): menu = ( ('home', 'Home'), ('search', 'Datasets'), ('organizations_index', 'Organizations'), ('group_index', 'Groups'), ('about', 'About') ) eq_(h.build_nav_main(*menu), ( '<li><a href="/">Home</a></li>' '<li><a href="/dataset/">Datasets</a></li>' '<li><a href="/organization/">Organizations</a></li>' '<li><a href="/group/">Groups</a></li>' '<li><a href="/about">About</a></li>'))
def test_legacy_pylon_routes(self): menu = ( ("home", "Home"), ("search", "Datasets"), ("organizations_index", "Organizations"), ("group_index", "Groups"), ("about", "About"), ) assert h.build_nav_main( *menu) == ('<li class="active"><a href="/">Home</a></li>' '<li><a href="/dataset/">Datasets</a></li>' '<li><a href="/organization/">Organizations</a></li>' '<li><a href="/group/">Groups</a></li>' '<li><a href="/about">About</a></li>')
def test_active_in_flask_routes(self, test_request_context): with test_request_context(u'/organization'): menu = ( ("home.index", "Home"), ("dataset.search", "Datasets", ['dataset', 'resource']), ("organization.index", "Organizations"), ("group.index", "Groups"), ("home.about", "About"), ) assert h.build_nav_main(*menu) == ( '<li><a href="/">Home</a></li>' '<li><a href="/dataset/">Datasets</a></li>' '<li class="active"><a href="/organization/">Organizations</a></li>' '<li><a href="/group/">Groups</a></li>' '<li><a href="/about">About</a></li>')
def build_pages_nav_main(*args): about_menu = p.toolkit.asbool(config.get('ckanext.pages.about_menu', True)) group_menu = p.toolkit.asbool(config.get('ckanext.pages.group_menu', True)) org_menu = p.toolkit.asbool(config.get('ckanext.pages.organization_menu', True)) new_args = [] for arg in args: if arg[0] == 'about' and not about_menu: continue if arg[0] == 'organizations_index' and not org_menu: continue if arg[0] == 'group_index' and not group_menu: continue new_args.append(arg) output = h.build_nav_main(*new_args) # do not display any private datasets in menu even for sysadmins pages_list = p.toolkit.get_action('ckanext_pages_list')(None, {'order': True, 'private': False}) page_name = '' if (p.toolkit.c.action in ('pages_show', 'blog_show') and p.toolkit.c.controller == 'ckanext.pages.controller:PagesController'): page_name = p.toolkit.c.environ['routes.url'].current().split('/')[-1] desired_lang_code = pylons.request.environ['CKAN_LANG'] acceptable_lang_codes = [desired_lang_code, desired_lang_code.split('_', 1)[0]] for page in pages_list: if page.get('lang') and page.get('lang') not in acceptable_lang_codes: continue if page['page_type'] == 'blog': link = h.tags.link_to(page.get('title'), h.url_for('/blog/' + str(page['name']))) else: link = h.tags.link_to(page.get('title'), h.url_for('/pages/' + str(page['name']))) if page['name'] == page_name: li = h.literal('<li class="active">') + link + h.literal('</li>') else: li = h.literal('<li>') + link + h.literal('</li>') output = output + li return output
def delineate_build_main_navigation(*args): output = h.build_nav_main(*args) if c.user: link = h.link_to( 'Delineate', h.url_for( controller= 'ckanext.delineate.controllers.delineatewatershed:DelineatewatershedController', action='show_form')) if p.toolkit.c.action == 'show_form': li = h.literal('<li class="active">') + link + h.literal('</li>') else: li = h.literal('<li>') + link + h.literal('</li>') output = li + output return output
def uebpackage_build_main_navigation(*args): output = h.build_nav_main(*args) # show ciwater related menu options only if the user is loggedin if c.user: link_pkg = h.url_for( controller= 'ckanext.uebpackage.controllers.packagecreate:PackagecreateController', action='packagecreateform') link_run = h.url_for( controller= 'ckanext.uebpackage.controllers.uebexecute:UEBexecuteController', action='select_model_package') if p.toolkit.c.action == 'packagecreateform' or p.toolkit.c.action == 'select_model_package': menu = h.literal('<li class="dropdown">') else: menu = h.literal('<li class="dropdown">') menu += h.literal( '<a id="drop1" role="button" data-toggle="dropdown" class="dropdown-toggle" href="#">' ) menu += 'UEB' + h.literal('<b><span class="caret"></span></b>') menu += h.literal('</a>') menu += h.literal( '<ul class="dropdown-menu" role="menu" area-labelledby="drop1">') li = h.literal( '<li role="presentation"><a role="menuitem" tabindex="-1" href=' ) + link_pkg + h.literal('>UEB Model Package</a>') + h.literal('</li>') menu += li li = h.literal( '<li role="presentation"><a role="menuitem" tabindex="-1" href=' ) + link_run + h.literal('>Run UEB</a>') + h.literal('</li>') menu += li menu += h.literal('</ul>') menu += h.literal('</li>') output = menu + output return output
def build_pages_nav_main(*args): about_menu = p.toolkit.asbool(config.get('ckanext.pages.about_menu', True)) group_menu = p.toolkit.asbool(config.get('ckanext.pages.group_menu', True)) org_menu = p.toolkit.asbool(config.get('ckanext.pages.organization_menu', True)) new_args = [] for arg in args: if arg[0] == 'about' and not about_menu: continue if arg[0] == 'organizations_index' and not org_menu: continue if arg[0] == 'group_index' and not group_menu: continue new_args.append(arg) output = h.build_nav_main(*new_args) # do not display any private datasets in menu even for sysadmins pages_list = p.toolkit.get_action('ckanext_pages_list')(None, {'order': True, 'private': False}) page_name = '' if (p.toolkit.c.action in ('pages_show', 'blog_show') and p.toolkit.c.controller == 'ckanext.pages.controller:PagesController'): page_name = p.toolkit.c.environ['routes.url'].current().split('/')[-1] for page in pages_list: if page['page_type'] == 'blog': link = h.literal('<a href="/blog/%s">%s</a>' % (page['name'], page['title'])) else: link = h.literal('<a href="/pages/%s">%s</a>' % (page['name'], page['title'])) if page['name'] == page_name: li = h.literal('<li class="active">') + link + h.literal('</li>') else: li = h.literal('<li>') + link + h.literal('</li>') output = output + li return output
def build_pages_nav_main(*args): about_menu = toolkit.asbool(config.get('ckanext.pages.about_menu', True)) group_menu = toolkit.asbool(config.get('ckanext.pages.group_menu', True)) org_menu = toolkit.asbool(config.get('ckanext.pages.organization_menu', True)) new_args = [] for arg in args: if arg[0] == 'about' and not about_menu: continue if arg[0] == 'organizations_index' and not org_menu: continue if arg[0] == 'group_index' and not group_menu: continue new_args.append(arg) output = h.build_nav_main(*new_args) # do not display any private datasets in menu even for sysadmins pages_list = toolkit.get_action('ckanext_pages_list')(None, {'order': True, 'private': False}) page_name = '' if (toolkit.c.action in ('pages_show', 'blog_show') and toolkit.c.controller == 'ckanext.pages.controller:PagesController'): page_name = toolkit.c.environ['routes.url'].current().split('/')[-1] for page in pages_list: type_ = 'blog' if page['page_type'] == 'blog' else 'pages' name = urllib.quote(page['name'].encode('utf-8')).decode('utf-8') title = cgi.escape(page['title']) link = h.literal(u'<a href="/{}/{}">{}</a>'.format(type_, name, title)) if page['name'] == page_name: li = h.literal('<li class="active">') + link + h.literal('</li>') else: li = h.literal('<li>') + link + h.literal('</li>') output = output + li return output
def build_pages_nav_main(*args): controller = CustomHeaderController() default_metadata = controller.get_default_custom_header_metadata() if not default_metadata.get('links'): plugins = config.get('ckan.plugins', '').split(' ') data = {'links': []} if 'pages' in plugins: from ckanext.pages.plugin import build_pages_nav_main output = build_pages_nav_main(*args) expr = re.compile('(<li><a href="(.*?)">(.*?)</a></li>)', flags=re.DOTALL) default_header_links = expr.findall(output) for index, link in enumerate(default_header_links): data['links'].append( Header(title=link[2], link=link[1], position=index)) else: from ckan.lib.helpers import build_nav_main base_links = build_nav_main(*args) expr = re.compile('(<li.*?</li>)', flags=re.DOTALL) default_header_links = expr.findall(base_links) parse_link = re.compile('href="(.*)">(.*?)</a>', flags=re.DOTALL) for index, link in enumerate(args): parsed_data = parse_link.findall(default_header_links[index]) parsed_data = parsed_data[0] if len(parsed_data) == 1 else ((), ()) data['links'].append( Header( title=parsed_data[1], link=parsed_data[0], position=index, )) controller.save_default_header_metadata(data) custom_header = controller.get_custom_header_metadata() final_header_links = [item for item in custom_header.get('links', [])] final_header_links.sort(key=lambda x: int(x.position)) return literal(''.join([item.html for item in final_header_links]))