feat: Implement email functionality with FastAPI-Mail and Mailhog

- Updated docker-compose.yml to include Mailhog service for email testing.
- Added environment variables for email configuration in docker-compose.
- Updated requirements.txt to include fastapi-mail for email support.
- Created templates for password reset, email verification, and welcome emails.
- Developed mail utility module to handle sending emails using FastAPI-Mail.
- Added documentation for email testing with Mailhog.
This commit is contained in:
Christian Krakau-Louis
2025-04-14 07:23:16 +02:00
parent 583f5aac42
commit 5cf3f944b1
22 changed files with 1425 additions and 157 deletions
+49 -44
View File
@@ -1,22 +1,40 @@
from sqlalchemy import text
from sqlalchemy import text, inspect
from .db import engine
def table_exists(conn, table_name):
"""Check if a table exists in the database."""
result = conn.execute(text(f"""
SELECT COUNT(*) as count
FROM information_schema.tables
WHERE table_schema = DATABASE()
AND table_name = '{table_name}'
"""))
return result.scalar() > 0
def column_exists(conn, table_name, column_name):
"""Check if a column exists in a table."""
result = conn.execute(text(f"""
SELECT COUNT(*) as count
FROM information_schema.columns
WHERE table_schema = DATABASE()
AND table_name = '{table_name}'
AND column_name = '{column_name}'
"""))
return result.scalar() > 0
def apply_migrations():
"""Apply all pending database migrations."""
# Check and add OAuth columns to users table
try:
with engine.connect() as conn:
# Check if the OAuth columns exist
result = conn.execute(text("""
SELECT COUNT(*) as count
FROM information_schema.columns
WHERE table_schema = DATABASE()
AND table_name = 'users'
AND column_name = 'is_oauth_user'
"""))
# Make sure tables exist before trying to alter them
if not table_exists(conn, 'users'):
print("Users table doesn't exist yet. Skipping OAuth columns migration.")
return
if result.fetchone()[0] == 0:
# Check if the OAuth columns exist
if not column_exists(conn, 'users', 'is_oauth_user'):
print("Adding OAuth columns to users table...")
# Add the OAuth columns
@@ -34,15 +52,7 @@ def apply_migrations():
print("OAuth columns already exist in users table.")
# Check if the is_admin column exists in the users table
result = conn.execute(text("""
SELECT COUNT(*) as count
FROM information_schema.columns
WHERE table_schema = DATABASE()
AND table_name = 'users'
AND column_name = 'is_admin'
"""))
if result.fetchone()[0] == 0:
if not column_exists(conn, 'users', 'is_admin'):
print("Adding is_admin column to users table...")
# Add the is_admin column to users table
@@ -56,32 +66,27 @@ def apply_migrations():
else:
print("is_admin column already exists in users table.")
# Check if the owner_id column exists in the teams table
result = conn.execute(text("""
SELECT COUNT(*) as count
FROM information_schema.columns
WHERE table_schema = DATABASE()
AND table_name = 'teams'
AND column_name = 'owner_id'
"""))
if result.fetchone()[0] == 0:
print("Adding owner_id column to teams table...")
# Add the owner_id column to teams table
conn.execute(text("""
ALTER TABLE teams
ADD COLUMN owner_id INT NULL,
ADD CONSTRAINT fk_teams_owner
FOREIGN KEY (owner_id) REFERENCES users(id)
ON DELETE SET NULL
"""))
conn.commit()
print("owner_id column added successfully to teams table.")
# Only proceed with teams table if it exists
if table_exists(conn, 'teams'):
# Check if the owner_id column exists in the teams table
if not column_exists(conn, 'teams', 'owner_id'):
print("Adding owner_id column to teams table...")
# Add the owner_id column to teams table
conn.execute(text("""
ALTER TABLE teams
ADD COLUMN owner_id INT NULL,
ADD CONSTRAINT fk_teams_owner
FOREIGN KEY (owner_id) REFERENCES users(id)
ON DELETE SET NULL
"""))
conn.commit()
print("owner_id column added successfully to teams table.")
else:
print("owner_id column already exists in teams table.")
else:
print("owner_id column already exists in teams table.")
print("Teams table doesn't exist yet. Skipping teams migrations.")
except Exception as e:
print(f"Error applying migrations: {str(e)}")
raise