Remove deprecated files and templates: Delete unused files including deployment scripts, environment configurations, and various HTML templates to streamline the project structure. This cleanup enhances maintainability and reduces clutter in the codebase.
This commit is contained in:
34
utils/__init__.py
Executable file
34
utils/__init__.py
Executable file
@@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Utility functions for the website application.
|
||||
This package contains various utilities for database management,
|
||||
user management, and server administration.
|
||||
"""
|
||||
|
||||
from .db_fix import fix_database_schema
|
||||
from .db_rebuild import rebuild_database
|
||||
from .db_test import test_database_connection, test_models, print_database_stats, run_all_tests
|
||||
from .user_manager import list_users, create_user, reset_password, delete_user, create_admin_user
|
||||
from .server import run_development_server
|
||||
|
||||
__all__ = [
|
||||
# Database utilities
|
||||
'fix_database_schema',
|
||||
'rebuild_database',
|
||||
'test_database_connection',
|
||||
'test_models',
|
||||
'print_database_stats',
|
||||
'run_all_tests',
|
||||
|
||||
# User management
|
||||
'list_users',
|
||||
'create_user',
|
||||
'reset_password',
|
||||
'delete_user',
|
||||
'create_admin_user',
|
||||
|
||||
# Server management
|
||||
'run_development_server',
|
||||
]
|
||||
BIN
utils/__pycache__/__init__.cpython-311.pyc
Normal file
BIN
utils/__pycache__/__init__.cpython-311.pyc
Normal file
Binary file not shown.
BIN
utils/__pycache__/db_fix.cpython-311.pyc
Normal file
BIN
utils/__pycache__/db_fix.cpython-311.pyc
Normal file
Binary file not shown.
BIN
utils/__pycache__/db_rebuild.cpython-311.pyc
Normal file
BIN
utils/__pycache__/db_rebuild.cpython-311.pyc
Normal file
Binary file not shown.
BIN
utils/__pycache__/db_test.cpython-311.pyc
Normal file
BIN
utils/__pycache__/db_test.cpython-311.pyc
Normal file
Binary file not shown.
BIN
utils/__pycache__/server.cpython-311.pyc
Normal file
BIN
utils/__pycache__/server.cpython-311.pyc
Normal file
Binary file not shown.
BIN
utils/__pycache__/user_manager.cpython-311.pyc
Normal file
BIN
utils/__pycache__/user_manager.cpython-311.pyc
Normal file
Binary file not shown.
78
utils/db_fix.py
Executable file
78
utils/db_fix.py
Executable file
@@ -0,0 +1,78 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
import sqlite3
|
||||
from datetime import datetime
|
||||
import sys
|
||||
import importlib.util
|
||||
|
||||
# Add the parent directory to path so we can import the app
|
||||
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, parent_dir)
|
||||
|
||||
from app import app, db_path
|
||||
from models import db
|
||||
|
||||
def ensure_db_dir():
|
||||
"""Make sure the database directory exists."""
|
||||
os.makedirs(os.path.dirname(db_path), exist_ok=True)
|
||||
|
||||
def fix_database_schema():
|
||||
"""Fix the database schema by adding missing columns."""
|
||||
with app.app_context():
|
||||
# Ensure directory exists
|
||||
ensure_db_dir()
|
||||
|
||||
# Check if database exists, create tables if needed
|
||||
if not os.path.exists(db_path):
|
||||
print("Database doesn't exist. Creating all tables from scratch...")
|
||||
db.create_all()
|
||||
print("Database tables created successfully!")
|
||||
return
|
||||
|
||||
# Connect to existing database
|
||||
print(f"Connecting to database: {db_path}")
|
||||
conn = sqlite3.connect(db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Check if User table exists
|
||||
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='user'")
|
||||
if not cursor.fetchone():
|
||||
print("User table doesn't exist. Creating all tables from scratch...")
|
||||
conn.close()
|
||||
db.create_all()
|
||||
print("Database tables created successfully!")
|
||||
return
|
||||
|
||||
# Check existing columns
|
||||
cursor.execute("PRAGMA table_info(user)")
|
||||
columns = cursor.fetchall()
|
||||
column_names = [col[1] for col in columns]
|
||||
print("Existing columns in User table:", column_names)
|
||||
|
||||
# Add missing columns
|
||||
if 'created_at' not in column_names:
|
||||
print("Adding 'created_at' column to User table...")
|
||||
cursor.execute("ALTER TABLE user ADD COLUMN created_at TIMESTAMP")
|
||||
|
||||
if 'last_login' not in column_names:
|
||||
print("Adding 'last_login' column to User table...")
|
||||
cursor.execute("ALTER TABLE user ADD COLUMN last_login TIMESTAMP")
|
||||
|
||||
if 'avatar' not in column_names:
|
||||
print("Adding 'avatar' column to User table...")
|
||||
cursor.execute("ALTER TABLE user ADD COLUMN avatar VARCHAR(200)")
|
||||
|
||||
if 'bio' not in column_names:
|
||||
print("Adding 'bio' column to User table...")
|
||||
cursor.execute("ALTER TABLE user ADD COLUMN bio TEXT")
|
||||
|
||||
# Commit the changes
|
||||
conn.commit()
|
||||
conn.close()
|
||||
print("Database schema updated successfully!")
|
||||
return True
|
||||
|
||||
if __name__ == "__main__":
|
||||
fix_database_schema()
|
||||
81
utils/db_rebuild.py
Executable file
81
utils/db_rebuild.py
Executable file
@@ -0,0 +1,81 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
import sqlite3
|
||||
from datetime import datetime
|
||||
import sys
|
||||
import importlib.util
|
||||
|
||||
# Add the parent directory to path so we can import the app
|
||||
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, parent_dir)
|
||||
|
||||
from app import app, db_path, create_default_categories
|
||||
from models import db, User, Category
|
||||
|
||||
def rebuild_database():
|
||||
"""Completely rebuilds the database by dropping and recreating all tables."""
|
||||
with app.app_context():
|
||||
print(f"Database path: {db_path}")
|
||||
|
||||
# Back up existing database if it exists
|
||||
if os.path.exists(db_path):
|
||||
backup_path = db_path + '.backup'
|
||||
try:
|
||||
import shutil
|
||||
shutil.copy2(db_path, backup_path)
|
||||
print(f"Backed up existing database to {backup_path}")
|
||||
except Exception as e:
|
||||
print(f"Warning: Could not create backup - {str(e)}")
|
||||
|
||||
# Ensure directory exists
|
||||
os.makedirs(os.path.dirname(db_path), exist_ok=True)
|
||||
|
||||
# Drop all tables and recreate them
|
||||
print("Dropping all tables...")
|
||||
db.drop_all()
|
||||
|
||||
print("Creating all tables...")
|
||||
db.create_all()
|
||||
|
||||
# Create admin user
|
||||
print("Creating admin user...")
|
||||
admin = User(
|
||||
username='admin',
|
||||
email='admin@example.com',
|
||||
is_admin=True,
|
||||
created_at=datetime.utcnow()
|
||||
)
|
||||
admin.set_password('admin')
|
||||
db.session.add(admin)
|
||||
|
||||
# Create regular user
|
||||
print("Creating regular user...")
|
||||
user = User(
|
||||
username='user',
|
||||
email='user@example.com',
|
||||
is_admin=False,
|
||||
created_at=datetime.utcnow()
|
||||
)
|
||||
user.set_password('user')
|
||||
db.session.add(user)
|
||||
|
||||
try:
|
||||
# Commit to generate user IDs
|
||||
db.session.commit()
|
||||
print("Users created successfully!")
|
||||
|
||||
# Create default categories
|
||||
print("Creating default categories...")
|
||||
create_default_categories()
|
||||
|
||||
print("Database rebuild completed successfully!")
|
||||
return True
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
print(f"Error during database rebuild: {str(e)}")
|
||||
raise
|
||||
|
||||
if __name__ == "__main__":
|
||||
rebuild_database()
|
||||
120
utils/db_test.py
Executable file
120
utils/db_test.py
Executable file
@@ -0,0 +1,120 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
import sys
|
||||
import sqlite3
|
||||
|
||||
# Add the parent directory to path so we can import the app
|
||||
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, parent_dir)
|
||||
|
||||
from app import app, db_path
|
||||
from models import db, User, Thought, MindMapNode, Category
|
||||
|
||||
def test_database_connection():
|
||||
"""Test if the database exists and can be connected to."""
|
||||
try:
|
||||
if not os.path.exists(db_path):
|
||||
print(f"Database file does not exist: {db_path}")
|
||||
return False
|
||||
|
||||
conn = sqlite3.connect(db_path)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("PRAGMA integrity_check")
|
||||
result = cursor.fetchone()
|
||||
conn.close()
|
||||
|
||||
if result and result[0] == "ok":
|
||||
print(f"Database integrity check passed: {db_path}")
|
||||
return True
|
||||
else:
|
||||
print(f"Database integrity check failed: {result}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"Error testing database connection: {e}")
|
||||
return False
|
||||
|
||||
def test_models():
|
||||
"""Test if all models are properly defined and can be queried."""
|
||||
with app.app_context():
|
||||
try:
|
||||
print("\nTesting User model...")
|
||||
user_count = User.query.count()
|
||||
print(f" Found {user_count} users")
|
||||
|
||||
print("\nTesting Category model...")
|
||||
category_count = Category.query.count()
|
||||
print(f" Found {category_count} categories")
|
||||
|
||||
print("\nTesting MindMapNode model...")
|
||||
node_count = MindMapNode.query.count()
|
||||
print(f" Found {node_count} mindmap nodes")
|
||||
|
||||
print("\nTesting Thought model...")
|
||||
thought_count = Thought.query.count()
|
||||
print(f" Found {thought_count} thoughts")
|
||||
|
||||
if user_count == 0:
|
||||
print("\nWARNING: No users found in the database. You might need to create an admin user.")
|
||||
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"Error testing models: {e}")
|
||||
return False
|
||||
|
||||
def print_database_stats():
|
||||
"""Print database statistics."""
|
||||
with app.app_context():
|
||||
try:
|
||||
stats = []
|
||||
stats.append(("Users", User.query.count()))
|
||||
stats.append(("Categories", Category.query.count()))
|
||||
stats.append(("Mindmap Nodes", MindMapNode.query.count()))
|
||||
stats.append(("Thoughts", Thought.query.count()))
|
||||
|
||||
print("\nDatabase Statistics:")
|
||||
print("-" * 40)
|
||||
for name, count in stats:
|
||||
print(f"{name:<20} : {count}")
|
||||
print("-" * 40)
|
||||
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"Error generating database statistics: {e}")
|
||||
return False
|
||||
|
||||
def run_all_tests():
|
||||
"""Run all database tests."""
|
||||
success = True
|
||||
|
||||
print("=" * 60)
|
||||
print("STARTING DATABASE TESTS")
|
||||
print("=" * 60)
|
||||
|
||||
# Test database connection
|
||||
print("\n1. Testing database connection...")
|
||||
if not test_database_connection():
|
||||
success = False
|
||||
|
||||
# Test models
|
||||
print("\n2. Testing database models...")
|
||||
if not test_models():
|
||||
success = False
|
||||
|
||||
# Print statistics
|
||||
print("\n3. Database statistics:")
|
||||
if not print_database_stats():
|
||||
success = False
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
if success:
|
||||
print("All database tests completed successfully!")
|
||||
else:
|
||||
print("Some database tests failed. Check the output above for details.")
|
||||
print("=" * 60)
|
||||
|
||||
return success
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_all_tests()
|
||||
34
utils/server.py
Executable file
34
utils/server.py
Executable file
@@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
# Add the parent directory to path so we can import the app
|
||||
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, parent_dir)
|
||||
|
||||
from app import app
|
||||
|
||||
def run_development_server(host='127.0.0.1', port=5000, debug=True):
|
||||
"""Run the Flask development server."""
|
||||
try:
|
||||
print(f"Starting development server on http://{host}:{port}")
|
||||
print("Press CTRL+C to stop the server")
|
||||
app.run(host=host, port=port, debug=debug)
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"Error starting development server: {e}")
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(description='Run the development server')
|
||||
parser.add_argument('--host', default='127.0.0.1', help='Host to bind to')
|
||||
parser.add_argument('--port', type=int, default=5000, help='Port to bind to')
|
||||
parser.add_argument('--debug', action='store_true', default=True, help='Enable debug mode')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
run_development_server(host=args.host, port=args.port, debug=args.debug)
|
||||
159
utils/user_manager.py
Executable file
159
utils/user_manager.py
Executable file
@@ -0,0 +1,159 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
import sys
|
||||
from datetime import datetime
|
||||
|
||||
# Add the parent directory to path so we can import the app
|
||||
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, parent_dir)
|
||||
|
||||
from app import app
|
||||
from models import db, User
|
||||
|
||||
def list_users():
|
||||
"""List all users in the database."""
|
||||
with app.app_context():
|
||||
try:
|
||||
users = User.query.all()
|
||||
if not users:
|
||||
print("No users found in the database.")
|
||||
return []
|
||||
|
||||
print("Found {} users:".format(len(users)))
|
||||
print("-" * 60)
|
||||
print("{:<5} {:<20} {:<30} {:<10}".format("ID", "Username", "Email", "Admin"))
|
||||
print("-" * 60)
|
||||
|
||||
for user in users:
|
||||
print("{:<5} {:<20} {:<30} {:<10}".format(
|
||||
user.id, user.username, user.email, "Yes" if user.is_admin else "No"
|
||||
))
|
||||
return users
|
||||
except Exception as e:
|
||||
print(f"Error listing users: {e}")
|
||||
return []
|
||||
|
||||
def create_user(username, email, password, is_admin=False):
|
||||
"""Create a new user in the database."""
|
||||
with app.app_context():
|
||||
try:
|
||||
# Check if user already exists
|
||||
existing_user = User.query.filter_by(username=username).first()
|
||||
if existing_user:
|
||||
print(f"User with username '{username}' already exists.")
|
||||
return False
|
||||
|
||||
# Check if email already exists
|
||||
existing_email = User.query.filter_by(email=email).first()
|
||||
if existing_email:
|
||||
print(f"User with email '{email}' already exists.")
|
||||
return False
|
||||
|
||||
# Create new user
|
||||
user = User(
|
||||
username=username,
|
||||
email=email,
|
||||
is_admin=is_admin,
|
||||
created_at=datetime.utcnow()
|
||||
)
|
||||
user.set_password(password)
|
||||
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
|
||||
print(f"User '{username}' created successfully!")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
print(f"Error creating user: {e}")
|
||||
return False
|
||||
|
||||
def reset_password(username, new_password):
|
||||
"""Reset password for a user."""
|
||||
with app.app_context():
|
||||
try:
|
||||
user = User.query.filter_by(username=username).first()
|
||||
if not user:
|
||||
print(f"User '{username}' not found.")
|
||||
return False
|
||||
|
||||
user.set_password(new_password)
|
||||
db.session.commit()
|
||||
|
||||
print(f"Password for user '{username}' reset successfully!")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
print(f"Error resetting password: {e}")
|
||||
return False
|
||||
|
||||
def delete_user(username):
|
||||
"""Delete a user from the database."""
|
||||
with app.app_context():
|
||||
try:
|
||||
user = User.query.filter_by(username=username).first()
|
||||
if not user:
|
||||
print(f"User '{username}' not found.")
|
||||
return False
|
||||
|
||||
db.session.delete(user)
|
||||
db.session.commit()
|
||||
|
||||
print(f"User '{username}' deleted successfully!")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
print(f"Error deleting user: {e}")
|
||||
return False
|
||||
|
||||
def create_admin_user():
|
||||
"""Create an admin user in the database."""
|
||||
return create_user('admin', 'admin@example.com', 'admin', is_admin=True)
|
||||
|
||||
if __name__ == "__main__":
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(description='User management utility')
|
||||
subparsers = parser.add_subparsers(dest='command', help='Command to execute')
|
||||
|
||||
# List users command
|
||||
list_parser = subparsers.add_parser('list', help='List all users')
|
||||
|
||||
# Create user command
|
||||
create_parser = subparsers.add_parser('create', help='Create a new user')
|
||||
create_parser.add_argument('--username', '-u', required=True, help='Username')
|
||||
create_parser.add_argument('--email', '-e', required=True, help='Email address')
|
||||
create_parser.add_argument('--password', '-p', required=True, help='Password')
|
||||
create_parser.add_argument('--admin', '-a', action='store_true', help='Make user an admin')
|
||||
|
||||
# Reset password command
|
||||
reset_parser = subparsers.add_parser('reset-password', help='Reset a user password')
|
||||
reset_parser.add_argument('--username', '-u', required=True, help='Username')
|
||||
reset_parser.add_argument('--password', '-p', required=True, help='New password')
|
||||
|
||||
# Delete user command
|
||||
delete_parser = subparsers.add_parser('delete', help='Delete a user')
|
||||
delete_parser.add_argument('--username', '-u', required=True, help='Username to delete')
|
||||
|
||||
# Create admin command (shortcut)
|
||||
admin_parser = subparsers.add_parser('create-admin', help='Create the default admin user')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.command == 'list':
|
||||
list_users()
|
||||
elif args.command == 'create':
|
||||
create_user(args.username, args.email, args.password, args.admin)
|
||||
elif args.command == 'reset-password':
|
||||
reset_password(args.username, args.password)
|
||||
elif args.command == 'delete':
|
||||
delete_user(args.username)
|
||||
elif args.command == 'create-admin':
|
||||
create_admin_user()
|
||||
else:
|
||||
parser.print_help()
|
||||
Reference in New Issue
Block a user