text: import os
  import secrets
  from typing import Any, Dict, List, Optional, Union
  
  from pydantic import (
      AnyHttpUrl,
      BaseSettings,
      EmailStr,
      HttpUrl,
      PostgresDsn,
      validator,
  )
  
  import stripe
  
  
  class Settings(BaseSettings):
      API_V1_STR: str = "/api/v1"
      SECRET_KEY: str = secrets.token_urlsafe(32)
      # 60 minutes * 24 hours * 8 days = 8 days
      ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8
      SERVER_NAME: str
      SERVER_HOST: Optional[AnyHttpUrl] = None
  
      @validator("SERVER_HOST", pre=True)
      def assemble_server_host(cls, v: Optional[AnyHttpUrl], values: Dict[str, Any]):
          if not v:
              return f'https://{values["SERVER_NAME"]}'
          return v
  
      # BACKEND_CORS_ORIGINS is a JSON-formatted list of origins
      # e.g: '["http://localhost", "http://localhost:4200", "http://localhost:3000", \
      # "http://localhost:8080", "http://local.dockertoolbox.tiangolo.com"]'
      BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = []
  
      @validator("BACKEND_CORS_ORIGINS", pre=True)
      def assemble_cors_origins(cls, v: Union[str, List[str]]) -> Union[List[str], str]:
          if isinstance(v, str) and not v.startswith("["):
              return [i.strip() for i in v.split(",")]
          elif isinstance(v, (list, str)):
              return v
          raise ValueError(v)
  
      PROJECT_NAME: str
      SENTRY_DSN: Optional[HttpUrl] = None
  
      @validator("SENTRY_DSN", pre=True)
      def sentry_dsn_can_be_blank(cls, v: str) -> Optional[str]:
          if v is None:
              return v
          if isinstance(v, str) and len(v) == 0:
              return None
          return v
  
      POSTGRES_SERVER: Optional[str]
      POSTGRES_USER: Optional[str]
      POSTGRES_PASSWORD: Optional[str]
      POSTGRES_DB: Optional[str]
      DATABASE_URL: Optional[PostgresDsn]
  
      @validator("DATABASE_URL", pre=True)
      def assemble_db_connection(cls, v: Optional[str], values: Dict[str, Any]) -> str:
          if v:
              return v
          if not (
              values.get("POSTGRES_USER")
              or values.get("POSTGRES_PASSWORD")
              or values.get("POSTGRES_SERVER")
              or values.get("POSTGRES_DB")
          ):
              raise ValueError()
          return PostgresDsn.build(
              scheme="postgresql",
              user=values.get("POSTGRES_USER"),
              password=values.get("POSTGRES_PASSWORD"),
              host=values.get("POSTGRES_SERVER"),
              path=f"/{values.get('POSTGRES_DB') or ''}",
          )
  
      SMTP_TLS: bool = True
      SMTP_PORT: Optional[int] = None
      SMTP_HOST: Optional[str] = None
      SMTP_USER: Optional[str] = None
      SMTP_PASSWORD: Optional[str] = None
      EMAILS_FROM_EMAIL: Optional[EmailStr] = None
      EMAILS_FROM_NAME: Optional[str] = None
  
      @validator("EMAILS_FROM_NAME")
      def get_project_name(cls, v: Optional[str], values: Dict[str, Any]) -> str:
          if not v:
              return values["PROJECT_NAME"]
          return v
  
      EMAIL_RESET_TOKEN_EXPIRE_HOURS: int = 48
      EMAIL_TEMPLATES_DIR: str = "/app/app/email-templates/compiled"
      EMAILS_ENABLED: bool = True
  
      @validator("EMAILS_ENABLED", pre=True)
      def get_emails_enabled(cls, v: bool, values: Dict[str, Any]) -> bool:
          return bool(
              values.get("SMTP_HOST")
              and values.get("SMTP_PORT")
              and values.get("EMAILS_FROM_EMAIL")
          )
  
      EMAIL_TEST_USER: EmailStr = "test@example.com"  # type: ignore
      FIRST_SUPERUSER: EmailStr
      FIRST_SUPERUSER_PASSWORD: str
      USERS_OPEN_REGISTRATION: bool = True
  
      MMDB_PATH: str = "data/db-ip-country.mmdb"
  
      TRIAL_PERIOD_DAYS: int = 30
  
      STRIPE_API_KEY: Optional[str] = None
      STRIPE_WEBHOOK_SECRET: Optional[str] = None
  
      # Minutes elapsed between two subsequent visits by the same user before it will be considered a new session
      SESSION_TIMEOUT_MINUTES: int = 5
  
      # Accounts and domains are not deleted immediately, but after a period of few days, just in case
      SOFT_DELETE_DURATION_DAYS: int = 14
  
      class Config:
          case_sensitive = True
  
  
  override_settings = {}
  if "POSTGRES_DB" in os.environ:
      # Pydantic settings order is env followed by .env file
      # In this case we override it so we can use a different db name in tests
      override_settings = {"POSTGRES_DB": os.environ["POSTGRES_DB"]}
  
  settings = Settings(**override_settings)
  
  if settings.STRIPE_API_KEY:
      stripe.api_key = settings.STRIPE_API_KEY
  