todoapp-us6a2a/app/core/config.py
Automated Action ccf2bb983b Fix Pydantic Settings validation error for extra environment variables
- Added CONTENT field to Settings model with Optional type
- Set extra='ignore' in Config to allow extra environment variables
- Added the same config to custom BaseSettings implementation for consistency
- Added missing import for Optional type
2025-06-02 09:21:34 +00:00

55 lines
1.7 KiB
Python

import os
from pathlib import Path
from typing import Optional
from pydantic import BaseModel
# Try to use pydantic_settings if available, otherwise fallback to a simple BaseModel
try:
from pydantic_settings import BaseSettings
except ImportError:
# Fallback for environments without pydantic_settings
# In Pydantic v2, BaseSettings was moved to pydantic_settings package
class BaseSettings(BaseModel):
"""
Simplified BaseSettings implementation for compatibility.
"""
class Config:
env_file = ".env"
case_sensitive = True
extra = "ignore" # Allow extra fields from environment variables
def __init__(self, **kwargs):
# Load environment variables
env_values = {}
for field_name, _ in self.__annotations__.items():
env_val = os.environ.get(field_name)
if env_val is not None:
env_values[field_name] = env_val
# Override with any provided values
env_values.update(kwargs)
super().__init__(**env_values)
class Settings(BaseSettings):
PROJECT_NAME: str = "Todo API"
API_V1_STR: str = "/api/v1"
# Database settings
DB_DIR: Path = Path("/app") / "storage" / "db"
DB_NAME: str = "db.sqlite"
# Environment variables that can be accessed via API
CONTENT: Optional[str] = None
class Config:
env_file = ".env"
case_sensitive = True
extra = "ignore" # Allow extra fields from environment variables
settings = Settings()
# Ensure DB directory exists
settings.DB_DIR.mkdir(parents=True, exist_ok=True)