def hri_build_nav_icon(menu_item, title, **kw): ''' helpers.py:build_nav_icon / helpers.py:_make_menu_item - fixed version for HRI Was required because the routine could not handle the changed controllers from organization -> ckanext.hrifi.controllers.hri_organization_controller:organization group -> ckanext.hrifi.controllers.hri_group_controller:group and the mismatch caused "Dataset" tab not to highlight. Sigh. ''' import copy from ckan.lib.helpers import _link_to _menu_items = config['routes.named_routes'] if menu_item not in _menu_items: raise Exception('menu item `%s` cannot be found' % menu_item) item = copy.copy(_menu_items[menu_item]) item.update(kw) # HACK Forget about the controller - just see if the action is the same. highlight_actions = kw.get('highlight_actions', kw.get('action', '')).split(' ') active = c.action in highlight_actions needed = item.pop('needed') for need in needed: if need not in kw: raise Exception('menu item `%s` need parameter `%s`' % (menu_item, need)) link = _link_to(title, menu_item, suppress_active_class=True, **item) if active: return p.toolkit.literal( '<li class="active">') + link + p.toolkit.literal('</li>') return p.toolkit.literal('<li>') + link + p.toolkit.literal('</li>')
def _make_menu_item(menu_item, title, **kw): ''' build a navigation item used for example breadcrumbs outputs <li><a href="..."></i> title</a></li> :param menu_item: the name of the defined menu item defined in config/routing as the named route of the same name :type menu_item: string :param title: text used for the link :type title: string :param **kw: additional keywords needed for creating url eg id=... :rtype: HTML literal This function is called by wrapper functions. ''' menu_item = h.map_pylons_to_flask_route_name(menu_item) _menu_items = config['routes.named_routes'] if menu_item not in _menu_items: raise Exception('menu item `%s` cannot be found' % menu_item) item = copy.copy(_menu_items[menu_item]) item.update(kw) needed = item.pop('needed') # log.debug('Items is: {}'.format(item)) for need in needed: if need not in kw: raise Exception('menu item `%s` need parameter `%s`' % (menu_item, need)) link = h._link_to(title, menu_item, suppress_active_class=True, **item) return link
def hri_build_nav_icon(menu_item, title, **kw): ''' helpers.py:build_nav_icon / helpers.py:_make_menu_item - fixed version for HRI Was required because the routine could not handle the changed controllers from organization -> ckanext.hrifi.controllers.hri_organization_controller:organization group -> ckanext.hrifi.controllers.hri_group_controller:group and the mismatch caused "Dataset" tab not to highlight. Sigh. ''' import copy from ckan.lib.helpers import _link_to _menu_items = config['routes.named_routes'] if menu_item not in _menu_items: raise Exception('menu item `%s` cannot be found' % menu_item) item = copy.copy(_menu_items[menu_item]) item.update(kw) # HACK Forget about the controller - just see if the action is the same. highlight_actions = kw.get('highlight_actions', kw.get('action', '')).split(' ') active = c.action in highlight_actions needed = item.pop('needed') for need in needed: if need not in kw: raise Exception('menu item `%s` need parameter `%s`' % (menu_item, need)) link = _link_to(title, menu_item, suppress_active_class=True, **item) if active: return p.toolkit.literal('<li class="active">') + link + p.toolkit.literal('</li>') return p.toolkit.literal('<li>') + link + p.toolkit.literal('</li>')
def _make_menu_item_handling_many_package_types(menu_item, title, **kw): # See ckan/lib/helpers.py:545 _menu_items = config['routes.named_routes'] if menu_item not in _menu_items: raise Exception('menu item `%s` cannot be found' % menu_item) item = copy.copy(_menu_items[menu_item]) item.update(kw) active = h._link_active(item) if c.controller == 'package' and len(menu_item) > 7: # Guess type of package if request.path == '/': type = 'dataset' else: parts = [x for x in request.path.split('/') if x] if len(parts[0]) == 2: # is it locale? simple check type = parts[1] else: type = parts[0] active = type == menu_item[:-7] # assuming menu_item == '<type>_search' needed = item.pop('needed') for need in needed: if need not in kw: raise Exception('menu item `%s` need parameter `%s`' % (menu_item, need)) link = h._link_to(title, menu_item, suppress_active_class=True, **item) if active: return literal('<li class="active">') + link + literal('</li>') return literal('<li>') + link + literal('</li>')
def resource_link(resource_dict, package_id): """ monkey patched version of ckan.lib.helpers.resource_link which extracts the correct translation of the resource name """ if 'name' in resource_dict and resource_dict['name']: resource_dict['name'] = get_localized_value_for_display( ast.literal_eval(resource_dict['name'])) text = ogdch_resource_display_name(resource_dict) url = url_for(controller='package', action='resource_read', id=package_id, resource_id=resource_dict['id']) return _link_to(text, url)
def _make_menu_item_cread(menu_item, title, **kw): ''' build a navigation item used for example breadcrumbs outputs <li><a href="..."></i> title</a></li> :param menu_item: the name of the defined menu item defined in config/routing as the named route of the same name :type menu_item: string :param title: text used for the link :type title: string :param **kw: additional keywords needed for creating url eg id=... :rtype: HTML literal This function is called by wrapper functions. ''' _menu_items = h.config['routes.named_routes'] if menu_item not in _menu_items: raise Exception('menu item `%s` cannot be found' % menu_item) item = h.copy.copy(_menu_items[menu_item]) item.update(kw) active = h._link_active(item) needed = item.pop('needed') for need in needed: if need not in kw: raise Exception('menu item `%s` need parameter `%s`' % (menu_item, need)) link = h._link_to(title, menu_item, suppress_active_class=True, **item) ##log.info('::::::::::::::::::::::::::: %r', menu_item) if active: return h.literal('<li class="active_cread">') + link + h.literal('</li>') if menu_item == "search": return h.literal('<li class="blue_bg">') + link + h.literal('</li>') if menu_item == "organizations_index": return h.literal('<li class="blue_bg">') + link + h.literal('</li>') return h.literal('<li class="green_bg">') + link + h.literal('</li>')
def _make_menu_item_handling_many_package_types( menu_item, title, **kw): # See ckan/lib/helpers.py:545 _menu_items = config['routes.named_routes'] if menu_item not in _menu_items: raise Exception('menu item `%s` cannot be found' % menu_item) item = copy.copy(_menu_items[menu_item]) item.update(kw) active = h._link_active(item) if c.controller == 'package' and len(menu_item) > 7: # Guess type of package if request.path == '/': type = 'dataset' else: parts = [x for x in request.path.split('/') if x] if len(parts[0]) == 2: # is it locale? simple check type = parts[1] else: type = parts[0] active = type == menu_item[: -7] # assuming menu_item == '<type>_search' needed = item.pop('needed') for need in needed: if need not in kw: raise Exception('menu item `%s` need parameter `%s`' % (menu_item, need)) link = h._link_to(title, menu_item, suppress_active_class=True, **item) if active: return literal('<li class="active">') + link + literal('</li>') return literal('<li>') + link + literal('</li>')
def group_link(group): """ monkey patched version of ckan.lib.helpers.group_link which extracts the correct translation of the group title """ url = url_for(controller='group', action='read', id=group['name']) title = group['title'] try: # The group creation message contains str(dict), so we must parse the # string with literal_eval to fix it. If the title is really just a # string, a ValueError is thrown. title = ast.literal_eval(title) title = get_localized_value_for_display(title) except ValueError: pass link = _link_to(title, url) try: # Sometimes the title has special characters encoded as unicode_escape # (e.g. '\u00e9'). Sometimes they are already decoded (e.g. 'é'). link = link.decode('unicode_escape') except UnicodeEncodeError: pass return link