Add Flask app and routes for basic authentication
Implemented a Flask application factory with SQLAlchemy, Flask-Login, and CSRF protection. Added auth and main blueprints providing login, logout, and index routes. Staged new source files, database, and compiled bytecode. Updated configuration to serve over HTTPS in development.
This commit is contained in:
parent
482d2e28f9
commit
cb3db118d0
10 changed files with 35 additions and 1 deletions
BIN
__pycache__/app.cpython-312.pyc
Normal file
BIN
__pycache__/app.cpython-312.pyc
Normal file
Binary file not shown.
Binary file not shown.
BIN
__pycache__/models.cpython-312.pyc
Normal file
BIN
__pycache__/models.cpython-312.pyc
Normal file
Binary file not shown.
BIN
__pycache__/routes.cpython-312.pyc
Normal file
BIN
__pycache__/routes.cpython-312.pyc
Normal file
Binary file not shown.
BIN
app.db
Normal file
BIN
app.db
Normal file
Binary file not shown.
6
app.py
6
app.py
|
|
@ -40,6 +40,7 @@ def create_app(test_config=None):
|
|||
# Initialise extensions
|
||||
db.init_app(app)
|
||||
login_manager.init_app(app)
|
||||
# User loader will be set after models import
|
||||
csrf.init_app(app)
|
||||
|
||||
# Register blueprints
|
||||
|
|
@ -76,7 +77,10 @@ login_manager.login_view = "auth.login"
|
|||
csrf = CSRFProtect()
|
||||
|
||||
# Import models after db is defined
|
||||
from models import User, Inspection, Photo # noqa: E402 pylint: disable=wrong-import-position
|
||||
from models import User, Inspection, Photo, load_user # noqa: E402 pylint: disable=wrong-import-position
|
||||
|
||||
# Set user loader
|
||||
login_manager.user_loader(load_user)
|
||||
|
||||
# If this script is executed directly, run the development server
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
0
new.db
Normal file
0
new.db
Normal file
30
routes.py
Normal file
30
routes.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
from flask import Blueprint, render_template, redirect, url_for, request, flash
|
||||
from flask_login import login_user, logout_user, current_user, login_required
|
||||
|
||||
# Auth blueprint
|
||||
auth_bp = Blueprint('auth', __name__, url_prefix='/auth')
|
||||
|
||||
@auth_bp.route('/login', methods=['GET', 'POST'])
|
||||
def login():
|
||||
if request.method == 'POST':
|
||||
from models import User
|
||||
username = request.form.get('username')
|
||||
password = request.form.get('password')
|
||||
user = User.query.filter_by(username=username).first()
|
||||
if user and user.check_password(password):
|
||||
login_user(user)
|
||||
return redirect(url_for('main.index'))
|
||||
flash('Invalid credentials')
|
||||
return render_template('login.html')
|
||||
|
||||
@auth_bp.route('/logout')
|
||||
def logout():
|
||||
logout_user()
|
||||
return redirect(url_for('auth.login'))
|
||||
|
||||
# Main blueprint
|
||||
main_bp = Blueprint('main', __name__)
|
||||
|
||||
@main_bp.route('/')
|
||||
def index():
|
||||
return render_template('index.html')
|
||||
0
testdb.sqlite
Normal file
0
testdb.sqlite
Normal file
0
testfile
Normal file
0
testfile
Normal file
Loading…
Reference in a new issue