From knecht.stefan at gmail.com Tue Mar 1 05:11:41 2022 From: knecht.stefan at gmail.com (Stefan Knecht) Date: Tue, 1 Mar 2022 17:11:41 +0700 Subject: [Flask] Stuck with @login_required which breaks our app In-Reply-To: <45d5d396-247b-4fb2-9ad2-6e25c5bcf670@gmx.de> References: <45d5d396-247b-4fb2-9ad2-6e25c5bcf670@gmx.de> Message-ID: Thank you David, you led me down the right path! On that discord you recommended was a super helpful user who identified the problem: from app import login_manager login_manager = LoginManager() login_manager.init_app(app) login_manager.login_view = "home_blueprint.login" Commented out that second line that overwrites login_manager, and voila, it started working. Stared at that code way too long and still can't believe I didn't see that all this time. Oh well. Thanks, really appreciate it! On Fri, Feb 25, 2022 at 4:51 PM David Nieder wrote: > Hello Stefan, > > I took your code and put together a minimal example but I could not > reproduce your error: the redirect always happens for me. > Looking at the code of Flask-Login, it seems to me that the only time > 401 is returned is if login_view is not set. Maybe look at that > again? But with your other dependencies there could be more going on. > > So the only tip I can give is: check if Flask-Login works with a > stripped-down version of your code and add components back in. > Here is what I used: > > from flask import Flask, request > from flask_login import LoginManager, login_required, current_user, > UserMixin > > app = Flask(__name__) > app.secret_key = '123456' > > login_manager = LoginManager() > login_manager.init_app(app) > login_manager.login_view = 'login' > > > @login_manager.user_loader > def load_user(user_id): > return UserMixin > > @app.route('/index') > def index(): > return 'the index page' > > @app.route('/contents') > @login_required > def contents(): > return 'the content page' > > @app.route('/login', methods=['GET', 'POST']) > def login(): > if not current_user.is_authenticated: > return 'current_user not authenticated' > return 'current_user authenticated' > > > > Good luck > David > > P.S. there is flask community on discord which is a lot more active than > this list. Maybe you can find more help there. > https://discord.gg/pallets > > > > On 24.02.22 09:49, Stefan Knecht wrote: > > Hello all > > > > I'm hoping to end a week-long struggle by reaching out here. > > > > We've been evaluating and testing building flask apps to give our vast > > python command line tool arsenal a face lift. > > > > I've got the basics working with coreUI for templates and flask doing > the > > core functionality. Or well, so it should. > > > > I've been following tutorials and the documentation, and as far as I can > > tell, we're doing everything correctly - yet it doesn't work anymore, as > > soon as I make use of @login_required. > > > > This is our routes.py: > > > > from app.home import blueprint > > from app.base.forms import LoginForm, CreateAccountForm > > > > from flask import Flask, current_app, render_template, redirect, > url_for, > > request, jsonify, session > > from flask_login import LoginManager, login_required, current_user, > > login_user, logout_user > > > > from app import login_manager > > from app.base.models import User > > > > app = Flask(__name__) > > > > login_manager = LoginManager() > > login_manager.init_app(app) > > login_manager.login_view = 'home_blueprint.login' > > > > > > @login_manager.user_loader > > def load_user(user_id): > > print(f"User loader {user_id}") > > return User.get(user_id) > > > > @blueprint.route('/index') > > def index(): > > print("Index") > > return render_template('index.html') > > > > @blueprint.route('/contents') > > @login_required > > def contents(): > > print("Contents") > > return ('hello', 200) > > > > > > @blueprint.route('/login', methods=['GET', 'POST']) > > def login(): > > > > print(f"HEADER {request.headers}") > > > > login_form = LoginForm(request.form) > > if 'login' in request.form: > > > > # read form data > > username = request.form['username'] > > password = request.form['password'] > > > > > > if not current_user.is_authenticated: > > return render_template( 'accounts/login.html', form=login_form) > > return redirect(url_for('home_blueprint.index')) > > > > If I remove @login_required from the /contents route, everything works > > fine. As soon as I add it there (or anywhere else for that matter) we > get > > nothing but 401 Unauthorized. > > > > This is what we use: > > > > $ pip3 list | grep -i flask > > Flask 1.1.1 > > Flask-Bootstrap 3.3.7.1 > > Flask-Ext 0.1 > > flask-ldap-login 0.3.0 > > flask-ldap3-login 0.9.16 > > Flask-Login 0.5.0 > > Flask-Migrate 2.5.3 > > flask-nav 0.6 > > Flask-Session 0.3.2 > > Flask-SimpleLDAP 1.4.0 > > Flask-SQLAlchemy 2.4.4 > > Flask-WTF 0.14.3 > > > > Any hint would be greatly appreciated. The head hurts, the wall's > already > > got a big hole in it. > > > > > > Stefan > > > > > > _______________________________________________ > > Flask mailing list > > Flask at python.org > > https://mail.python.org/mailman/listinfo/flask > _______________________________________________ > Flask mailing list > Flask at python.org > https://mail.python.org/mailman/listinfo/flask > -------------- next part -------------- An HTML attachment was scrubbed... URL: From James.P.Brosnahan at outlook.com Fri Mar 11 23:50:59 2022 From: James.P.Brosnahan at outlook.com (James Brosnahan) Date: Sat, 12 Mar 2022 04:50:59 +0000 Subject: [Flask] Flask Extension Development Help Message-ID: I need help finding information on building a flask extension for data manipulation and analysis between existing flask database extensions and the main flask API routes. I have read the instructions at Flask Extension Development but, I am confused by the example of a SQLite class. I am looking for information on writing an extension that flattens data from multiple tables using SQL Alchemy, loads the data to a pandas data frame, and then returns pivot tables of the data frames to an API route function that sends JSON to a client browser. Sent from Mail for Windows -------------- next part -------------- An HTML attachment was scrubbed... URL: