P
P
pcdesign2015-01-03 13:55:16
Flask
pcdesign, 2015-01-03 13:55:16

How to remove the endless @login_required and @admin_required?

There is a site on flask with 100500 routes.
And it is necessary to prescribe decorators for routes like a woodpecker.
For example:

@app.route('/userarea/income/')
@login_required
def user_income():
    return render_template("user_index.html")


@app.route('/userarea/income/add/')
@login_required
def user_income_add():
    return render_template("user_index.html")


@app.route('/adminarea/')
@login_required
@admin_required
def admin_firm():  
    return render_template("admin.html")

И т.д.

Question: What's the point of using flask-login if @app.before_request also has the ability to split it into 2-3 lines?
And in principle, what's the point of using flask-login?
When are sessions missing?
@app.before_request
def before_request():
    g.user_id = session.get('user_id', None)
    g.admin_id = session.get('admin_id', None)
    if ((not g.user_id and request.path.startswith('/userarea/')) or
            (not g.admin_id and request.path.startswith('/adminarea/'))):
        return redirect(url_for('login'))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2015-01-03
@pcdesign

And what's the point of using flask if you're too lazy?
Use django, they have already added middlewares for you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question