GithubHelp home page GithubHelp logo

tonyseek / flask-navigation Goto Github PK

View Code? Open in Web Editor NEW
20.0 20.0 19.0 441 KB

Build navigation bars in your Flask application.

Home Page: https://pypi.python.org/pypi/Flask-Navigation

License: MIT License

Python 93.81% HTML 6.19%

flask-navigation's People

Contributors

jbobron avatar noirbizarre avatar tonyseek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

flask-navigation's Issues

Be "active" for included resources

The related topic is here #4

The situation is like this:

top_bar = nav.Bar('top', [
    nav.Item(u'Home', 'home'),
    nav.Item(u'Posts', 'post_list'),
])

@bp.route('/post/<int:post_id>')
def post(post_id):
    post = Post.query.get_or_404(post_id)
    return render_template('post.html', post=post)

The problem is how to let the item.is_active be True while accessing /post/123. I have three solutions for it:

  • Using URL prefix
top_bar['post_list'].mount_views(url_prefix='/post', blueprint=bp)
  • Marking for each URL rules
top_bar['post_list'].mount_url_rule(endpoint='post')
  • Using whole blueprint as view bundle
top_bar['post_list'].mount_blueprint(bp)

@shonenada @tonicbupt What do you think about these?

Render HTML like WTForm

Maybe we can improve inerfaces in rendering html like:

<ul>
    {% for item in nav.top %}
    <li class="{{ 'current' if item.is_active else '' }}">
        {{ item.a(class="some_style", id="some_id") }}
    </li>
    {% endfor %}
</ul>

Conditional rendering based on roles/permissions

@tonyseek can suggest a way to handle conditional render of nav items based on role/permissions?
I have functioning @roles_required('role-name') on my routes. I would like to not render a link if the User does not have permission to that route.

Thanks.

args callable is accessed ouside application/request context

Actually it's not possible to write:

from flask import g

args_getter = lambda: {'user': g.current_user}

nav.Bar('usermenu', (
    nav.Item(_('Dashboard'), 'user.dashboard', args_getter),
    nav.Item(_('Profile'), 'user.profile', args_getter),
    nav.Item(_('Settings'), 'user.settings', args_getter),
))

Args are evaluated on navbar creation to fill the ident but they should only be accessed on rendering to ensure app/request context is pushed.

I will try to submit a pull-request to fix this behavior but idents need to be changed.

Another solution is to provide a callable to generate url like that:

<a href="{{ item.url_for(user=current_user) }}">{{ item.label }}</a>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.