Source code for railgun.website.navibar

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# @file: railgun/website/navibar.py
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# This file is released under BSD 2-clause license.

"""Railgun has a site-wide navigation bar.

Navigation bar is a two-level menu.  Each item is related to some
certain page.  When the user is visiting some page, the corresponding
item as well as its parent items should be marked as `active`.

This module provides the utility to track active navibar items and maintain
the navigation bar object for each request.  Each navibar item should be
given a `navibar identity`.

The identity is stored in :token:`g.navibar_identity`.  At the start of
each request, :data:`flask.request.endpoint` will be used as the initial
value of :token:`g.navibar_identity`.  If you wish to change the identity,
you may call :func:`set_navibar_identity` anytime before render the
template.
"""

from flask import request, g, url_for

from .context import app






navigates = Navibar()


@app.context_processor
def __inject_navigate_links():
    """inject navigate links into template context."""
    return dict(navibar=NavibarProxy(navigates))


[docs]def set_navibar_identity(identity): """Set the identity of nagivation item for current request. :param identity: The navibar item identity. :type identity: :class:`str` """ g.navibar_identity = identity
@app.before_request def __mark_navibar_identity(*args, **kwargs): """set default value of g.navibar_identity to '[request.endpoint]'.""" set_navibar_identity(request.endpoint)