updated solution

This commit is contained in:
2025-07-31 16:37:54 +03:00
parent 756f9052b5
commit c8bbbebb48
31 changed files with 199 additions and 190 deletions

31
app.py
View File

@@ -7,9 +7,13 @@ from werkzeug.utils import secure_filename
from functools import wraps
from extensions import db, bcrypt, login_manager
from sqlalchemy import text
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# First import models
from models import User, Player, Content, Group, ServerLog
from models import User, Player, Group, Content, ServerLog, group_player
# Then import utilities that use the models
from flask_login import login_user, logout_user, login_required, current_user
@@ -47,8 +51,10 @@ app = Flask(__name__, instance_relative_config=True)
# Set the secret key from environment variable or use a default value
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY', 'Ana_Are_Multe_Mere-Si_Nu_Are_Pere')
# Configure the database location to be in the instance folder
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(app.instance_path, 'dashboard.db')
instance_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'instance'))
os.makedirs(instance_dir, exist_ok=True)
db_path = os.path.join(instance_dir, 'dashboard.db')
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{db_path}'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Set maximum content length to 1GB
@@ -56,6 +62,7 @@ app.config['MAX_CONTENT_LENGTH'] = 2048 * 2048 * 2048 # 2GB, adjust as needed
# Ensure the instance folder exists
os.makedirs(app.instance_path, exist_ok=True)
os.makedirs(instance_dir, exist_ok=True)
db.init_app(app)
bcrypt.init_app(app)
@@ -68,8 +75,9 @@ app.config['UPLOAD_FOLDERLOGO'] = UPLOAD_FOLDERLOGO
# Ensure the upload folder exists
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
os.makedirs(UPLOAD_FOLDERLOGO)
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
if not os.path.exists(UPLOAD_FOLDERLOGO):
os.makedirs(UPLOAD_FOLDERLOGO, exist_ok=True)
login_manager.login_view = 'login'
@@ -303,7 +311,8 @@ def add_player():
hostname = request.form['hostname']
password = bcrypt.generate_password_hash(request.form['password']).decode('utf-8')
quickconnect_password = bcrypt.generate_password_hash(request.form['quickconnect_password']).decode('utf-8')
add_player_util(username, hostname, password, quickconnect_password)
orientation = request.form.get('orientation', 'Landscape') # <-- Get orientation
add_player_util(username, hostname, password, quickconnect_password, orientation) # <-- Pass orientation
flash(f'Player "{username}" added successfully.', 'success')
return redirect(url_for('dashboard'))
return render_template('add_player.html')
@@ -637,6 +646,16 @@ def create_admin(username, password):
db.session.commit()
print(f"Admin user '{username}' created successfully.")
from models.create_default_user import create_default_user
with app.app_context():
try:
db.session.execute(db.select(User).limit(1))
except Exception as e:
print("Database not initialized or missing tables. Re-initializing...")
db.create_all()
# Always ensure default user exists
create_default_user(db, User, bcrypt)
# Add this at the end of app.py
if __name__ == '__main__':