Skip to content

Commit

Permalink
Merge pull request #3862 from tino097/flask-helpers-update
Browse files Browse the repository at this point in the history
Flask helpers update
  • Loading branch information
amercader committed Oct 13, 2017
2 parents f1f36f8 + 48556ff commit b5b8bf0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
15 changes: 15 additions & 0 deletions ckan/config/middleware/flask_app.py
Expand Up @@ -162,6 +162,21 @@ def hello_world_post():
if hasattr(plugin, 'get_blueprint'):
app.register_extension_blueprint(plugin.get_blueprint())

# Set flask routes in named_routes
for rule in app.url_map.iter_rules():
if '.' not in rule.endpoint:
continue
controller, action = rule.endpoint.split('.')
route = {
rule.endpoint: {
'action': action,
'controller': controller,
'highlight_actions': action,
'needed': list(rule.arguments)
}
}
config['routes.named_routes'].update(route)

# Start other middleware
for plugin in PluginImplementations(IMiddleware):
app = plugin.make_middleware(app, config)
Expand Down
42 changes: 40 additions & 2 deletions ckan/lib/helpers.py
Expand Up @@ -685,12 +685,25 @@ def are_there_flash_messages():

def _link_active(kwargs):
''' creates classes for the link_to calls '''
if is_flask_request():
return _link_active_flask(kwargs)
else:
return _link_active_pylons(kwargs)


def _link_active_pylons(kwargs):
highlight_actions = kwargs.get('highlight_actions',
kwargs.get('action', '')).split()
return (c.controller == kwargs.get('controller')
and c.action in highlight_actions)


def _link_active_flask(kwargs):
blueprint, endpoint = request.url_rule.endpoint.split('.')
return(kwargs.get('controller') == blueprint and
kwargs.get('action') == endpoint)


def _link_to(text, *args, **kwargs):
'''Common link making code for several helper functions'''
assert len(args) < 2, 'Too many unnamed arguments'
Expand Down Expand Up @@ -731,6 +744,30 @@ def nav_link(text, *args, **kwargs):
:param condition: if ``False`` then no link is returned
'''
if is_flask_request():
return nav_link_flask(text, *args, **kwargs)
else:
return nav_link_pylons(text, *args, **kwargs)


def nav_link_flask(text, *args, **kwargs):
if len(args) > 1:
raise Exception('Too many unnamed parameters supplied')
blueprint, endpoint = request.url_rule.endpoint.split('.')
if args:
kwargs['controller'] = blueprint or None
named_route = kwargs.pop('named_route', '')
if kwargs.pop('condition', True):
if named_route:
link = _link_to(text, named_route, **kwargs)
else:
link = _link_to(text, **kwargs)
else:
link = ''
return link


def nav_link_pylons(text, *args, **kwargs):
if len(args) > 1:
raise Exception('Too many unnamed parameters supplied')
if args:
Expand Down Expand Up @@ -923,8 +960,9 @@ def get_facet_items_dict(facet, limit=None, exclude_active=False):
facets.append(dict(active=True, **facet_item))
# Sort descendingly by count and ascendingly by case-sensitive display name
facets.sort(key=lambda it: (-it['count'], it['display_name'].lower()))
if c.search_facets_limits and limit is None:
limit = c.search_facets_limits.get(facet)
if hasattr(c, 'search_facets_limits'):
if c.search_facets_limits and limit is None:
limit = c.search_facets_limits.get(facet)
# zero treated as infinite for hysterical raisins
if limit is not None and limit > 0:
return facets[:limit]
Expand Down

0 comments on commit b5b8bf0

Please sign in to comment.