kinda working safe point
This commit is contained in:
parent
b9a82af12f
commit
6dda02141e
31 changed files with 4302 additions and 2937 deletions
107
app/__init__.py
107
app/__init__.py
|
@ -1,4 +1,4 @@
|
|||
from flask import Flask, current_app
|
||||
from flask import Flask, current_app, render_template
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from flask_login import LoginManager
|
||||
from config import Config
|
||||
|
@ -7,6 +7,13 @@ from datetime import datetime
|
|||
import sqlite3
|
||||
import logging
|
||||
|
||||
# Configure logging
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Initialize extensions
|
||||
db = SQLAlchemy()
|
||||
login_manager = LoginManager()
|
||||
|
@ -18,37 +25,57 @@ def initialize_database(app):
|
|||
with app.app_context():
|
||||
app.logger.info("Initializing database...")
|
||||
try:
|
||||
# Create all tables (this is safe to call even if tables exist)
|
||||
db.create_all()
|
||||
# Check if tables exist before creating them
|
||||
from sqlalchemy import inspect
|
||||
inspector = inspect(db.engine)
|
||||
existing_tables = inspector.get_table_names()
|
||||
|
||||
# Check if we need to add the Share and Download models
|
||||
inspector = db.inspect(db.engine)
|
||||
if not inspector.has_table('share'):
|
||||
app.logger.info("Creating Share table...")
|
||||
# Import models to ensure they're registered with SQLAlchemy
|
||||
from app.models import Share
|
||||
# Only create tables that don't exist
|
||||
if not existing_tables:
|
||||
app.logger.info("Creating database tables...")
|
||||
db.create_all()
|
||||
|
||||
# Check if we need to add the Share and Download models
|
||||
inspector = db.inspect(db.engine)
|
||||
if not inspector.has_table('share'):
|
||||
# Create the Share table
|
||||
Share.__table__.create(db.engine)
|
||||
|
||||
if not inspector.has_table('download'):
|
||||
app.logger.info("Creating Download table...")
|
||||
from app.models import Download
|
||||
app.logger.info("Creating Share table...")
|
||||
# Import models to ensure they're registered with SQLAlchemy
|
||||
from app.models import Share
|
||||
if not inspector.has_table('share'):
|
||||
# Create the Share table
|
||||
Share.__table__.create(db.engine)
|
||||
|
||||
if not inspector.has_table('download'):
|
||||
# Create the Download table
|
||||
Download.__table__.create(db.engine)
|
||||
app.logger.info("Creating Download table...")
|
||||
from app.models import Download
|
||||
if not inspector.has_table('download'):
|
||||
# Create the Download table
|
||||
Download.__table__.create(db.engine)
|
||||
|
||||
# Check for existing users - create admin if none
|
||||
from app.models import User
|
||||
if User.query.count() == 0:
|
||||
app.logger.info("No users found, creating default admin user...")
|
||||
admin = User(username='admin', email='admin@example.com')
|
||||
admin.set_password('adminpassword')
|
||||
db.session.add(admin)
|
||||
db.session.commit()
|
||||
app.logger.info("Default admin user created")
|
||||
|
||||
# Check for existing users - create admin if none
|
||||
from app.models import User
|
||||
if User.query.count() == 0:
|
||||
app.logger.info("No users found, creating default admin user...")
|
||||
admin = User(username='admin', email='admin@example.com')
|
||||
admin.set_password('adminpassword')
|
||||
db.session.add(admin)
|
||||
db.session.commit()
|
||||
app.logger.info("Default admin user created")
|
||||
app.logger.info("Database initialization complete")
|
||||
else:
|
||||
app.logger.info(f"Database already initialized with tables: {existing_tables}")
|
||||
|
||||
# Check for missing tables
|
||||
from app.models import User, File, Folder, Download
|
||||
required_tables = ['users', 'files', 'folders', 'downloads']
|
||||
missing_tables = [table for table in required_tables if table not in existing_tables]
|
||||
|
||||
if missing_tables:
|
||||
app.logger.info(f"Creating missing tables: {missing_tables}")
|
||||
# Create only the missing tables
|
||||
db.create_all()
|
||||
|
||||
app.logger.info("Database initialization complete")
|
||||
except Exception as e:
|
||||
app.logger.error(f"Error initializing database: {str(e)}")
|
||||
# Don't raise the exception to prevent app startup failure
|
||||
|
@ -160,7 +187,17 @@ def format_file_size(size):
|
|||
|
||||
def create_app(config_class=Config):
|
||||
app = Flask(__name__)
|
||||
app.config.from_object(config_class)
|
||||
|
||||
# Configure app
|
||||
if config_class:
|
||||
app.config.from_object(config_class)
|
||||
else:
|
||||
# Use default configuration
|
||||
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'dev-key-change-in-production')
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'sqlite:///app.db')
|
||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||
app.config['UPLOAD_FOLDER'] = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../uploads')
|
||||
app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024 # 100 MB max upload
|
||||
|
||||
# Configure logging
|
||||
if not app.debug:
|
||||
|
@ -198,13 +235,15 @@ def create_app(config_class=Config):
|
|||
run_migrations(app)
|
||||
|
||||
# Register blueprints
|
||||
from app.routes.auth import auth_bp
|
||||
from app.routes.files import files_bp
|
||||
from app.routes.dashboard import dashboard_bp
|
||||
from app.routes.auth import bp as auth_bp
|
||||
from app.routes.files import bp as files_bp
|
||||
from app.routes.dashboard import bp as dashboard_bp
|
||||
from app.routes.admin import bp as admin_bp
|
||||
|
||||
app.register_blueprint(auth_bp)
|
||||
app.register_blueprint(files_bp)
|
||||
app.register_blueprint(auth_bp, url_prefix='/auth')
|
||||
app.register_blueprint(files_bp, url_prefix='/files')
|
||||
app.register_blueprint(dashboard_bp)
|
||||
app.register_blueprint(admin_bp, url_prefix='/admin')
|
||||
|
||||
# Add context processor for template variables
|
||||
@app.context_processor
|
||||
|
@ -227,8 +266,8 @@ def create_app(config_class=Config):
|
|||
db.session.rollback() # Rollback any failed database transactions
|
||||
return render_template('errors/500.html'), 500
|
||||
|
||||
logger.info("Flask Files startup")
|
||||
return app
|
||||
|
||||
# Import must come after create_app to avoid circular imports
|
||||
from flask import render_template # For error handlers
|
||||
from app import models
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue