Refactor code for improved readability and consistency

- Cleaned up whitespace and formatting across multiple files for better readability.
This commit is contained in:
JSC
2025-07-02 10:37:48 +02:00
parent e63c7a0767
commit 171dbb9b63
19 changed files with 361 additions and 260 deletions

View File

@@ -8,10 +8,10 @@ def init_database():
"""Initialize database tables and seed with default data."""
# Create all tables
db.create_all()
# Seed plans if they don't exist
seed_plans()
# Migrate existing users to have plans
migrate_users_to_plans()
@@ -21,7 +21,7 @@ def seed_plans():
# Check if plans already exist
if Plan.query.count() > 0:
return
# Create default plans
plans_data = [
{
@@ -46,11 +46,11 @@ def seed_plans():
"max_credits": 300,
},
]
for plan_data in plans_data:
plan = Plan(**plan_data)
db.session.add(plan)
db.session.commit()
print(f"Seeded {len(plans_data)} plans into database")
@@ -58,11 +58,11 @@ def seed_plans():
def migrate_users_to_plans():
"""Assign plans to existing users who don't have one."""
from app.models.user import User
try:
# Find users without plans
users_without_plans = User.query.filter(User.plan_id.is_(None)).all()
# Find users with plans but NULL credits (only if credits column exists)
# Note: We only migrate users with NULL credits, not 0 credits
# 0 credits means they spent them, NULL means they never got assigned
@@ -73,19 +73,19 @@ def migrate_users_to_plans():
except Exception:
# Credits column doesn't exist yet, will be handled by create_all
users_without_credits = []
if not users_without_plans and not users_without_credits:
return
# Get default and pro plans
default_plan = Plan.get_default_plan()
pro_plan = Plan.get_pro_plan()
# Get the first user (admin) from all users ordered by ID
first_user = User.query.order_by(User.id).first()
updated_count = 0
# Assign plans to users without plans
for user in users_without_plans:
# First user gets pro plan, others get free plan
@@ -104,17 +104,19 @@ def migrate_users_to_plans():
except Exception:
pass
updated_count += 1
# Assign credits to users with plans but no credits
for user in users_without_credits:
user.credits = user.plan.credits
updated_count += 1
if updated_count > 0:
db.session.commit()
print(f"Updated {updated_count} existing users with plans and credits")
print(
f"Updated {updated_count} existing users with plans and credits"
)
except Exception:
# If there's any error (like missing columns), just skip migration
# The database will be properly created by create_all()
pass
pass