reformatted with black
This commit is contained in:
parent
4ace9bd2a0
commit
1a6f4abca0
86
app.py
86
app.py
@ -5,83 +5,95 @@ from werkzeug.security import generate_password_hash, check_password_hash
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.config['SECRET_KEY'] = os.environ.get(
|
app.config["SECRET_KEY"] = os.environ.get(
|
||||||
'FLASK_SECRET_KEY',
|
"FLASK_SECRET_KEY", "your-secret-key-here" # Fallback for development only
|
||||||
'your-secret-key-here' # Fallback for development only
|
|
||||||
)
|
)
|
||||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(os.path.abspath(os.path.dirname(__file__)), 'instance/users.db')
|
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + os.path.join(
|
||||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
os.path.abspath(os.path.dirname(__file__)), "instance/users.db"
|
||||||
|
)
|
||||||
|
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
||||||
|
|
||||||
db = SQLAlchemy(app)
|
db = SQLAlchemy(app)
|
||||||
|
|
||||||
|
|
||||||
# Initialize database CLI command
|
# Initialize database CLI command
|
||||||
@app.cli.command("init-db")
|
@app.cli.command("init-db")
|
||||||
def init_db():
|
def init_db():
|
||||||
import os
|
import os
|
||||||
db_path = app.config['SQLALCHEMY_DATABASE_URI'].replace('sqlite:///', '')
|
|
||||||
|
db_path = app.config["SQLALCHEMY_DATABASE_URI"].replace("sqlite:///", "")
|
||||||
os.makedirs(os.path.dirname(db_path), exist_ok=True, mode=0o755)
|
os.makedirs(os.path.dirname(db_path), exist_ok=True, mode=0o755)
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
db.create_all()
|
db.create_all()
|
||||||
print(f"Database created at {db_path}")
|
print(f"Database created at {db_path}")
|
||||||
|
|
||||||
|
|
||||||
login_manager = LoginManager(app)
|
login_manager = LoginManager(app)
|
||||||
login_manager.login_view = 'login'
|
login_manager.login_view = "login"
|
||||||
|
|
||||||
|
|
||||||
class User(UserMixin, db.Model):
|
class User(UserMixin, db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
email = db.Column(db.String(100), unique=True)
|
email = db.Column(db.String(100), unique=True)
|
||||||
password = db.Column(db.String(100))
|
password = db.Column(db.String(100))
|
||||||
|
|
||||||
|
|
||||||
@login_manager.user_loader
|
@login_manager.user_loader
|
||||||
def load_user(user_id):
|
def load_user(user_id):
|
||||||
return User.query.get(int(user_id))
|
return User.query.get(int(user_id))
|
||||||
|
|
||||||
@app.route('/')
|
|
||||||
def home():
|
|
||||||
return redirect(url_for('login'))
|
|
||||||
|
|
||||||
@app.route('/login', methods=['GET', 'POST'])
|
@app.route("/")
|
||||||
|
def home():
|
||||||
|
return redirect(url_for("login"))
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/login", methods=["GET", "POST"])
|
||||||
def login():
|
def login():
|
||||||
if request.method == 'POST':
|
if request.method == "POST":
|
||||||
email = request.form.get('email')
|
email = request.form.get("email")
|
||||||
password = request.form.get('password')
|
password = request.form.get("password")
|
||||||
user = User.query.filter_by(email=email).first()
|
user = User.query.filter_by(email=email).first()
|
||||||
|
|
||||||
if user and check_password_hash(user.password, password):
|
if user and check_password_hash(user.password, password):
|
||||||
login_user(user)
|
login_user(user)
|
||||||
return redirect(url_for('dashboard'))
|
return redirect(url_for("dashboard"))
|
||||||
|
|
||||||
return 'Invalid credentials'
|
|
||||||
return render_template('login.html')
|
|
||||||
|
|
||||||
@app.route('/register', methods=['GET', 'POST'])
|
return "Invalid credentials"
|
||||||
|
return render_template("login.html")
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/register", methods=["GET", "POST"])
|
||||||
def register():
|
def register():
|
||||||
if request.method == 'POST':
|
if request.method == "POST":
|
||||||
email = request.form.get('email')
|
email = request.form.get("email")
|
||||||
password = request.form.get('password')
|
password = request.form.get("password")
|
||||||
confirm_password = request.form.get('confirm_password')
|
confirm_password = request.form.get("confirm_password")
|
||||||
|
|
||||||
if password != confirm_password:
|
if password != confirm_password:
|
||||||
return 'Passwords do not match'
|
return "Passwords do not match"
|
||||||
|
|
||||||
if User.query.filter_by(email=email).first():
|
if User.query.filter_by(email=email).first():
|
||||||
return 'Email already registered'
|
return "Email already registered"
|
||||||
|
|
||||||
password = generate_password_hash(password)
|
password = generate_password_hash(password)
|
||||||
|
|
||||||
new_user = User(email=email, password=password)
|
new_user = User(email=email, password=password)
|
||||||
db.session.add(new_user)
|
db.session.add(new_user)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
return redirect(url_for('login'))
|
|
||||||
return render_template('register.html')
|
|
||||||
|
|
||||||
@app.route('/dashboard')
|
return redirect(url_for("login"))
|
||||||
|
return render_template("register.html")
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/dashboard")
|
||||||
@login_required
|
@login_required
|
||||||
def dashboard():
|
def dashboard():
|
||||||
return render_template('dashboard.html')
|
return render_template("dashboard.html")
|
||||||
|
|
||||||
@app.route('/logout')
|
|
||||||
|
@app.route("/logout")
|
||||||
@login_required
|
@login_required
|
||||||
def logout():
|
def logout():
|
||||||
logout_user()
|
logout_user()
|
||||||
return redirect(url_for('login'))
|
return redirect(url_for("login"))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user