push changes

This commit is contained in:
Mateusz Gruszczyński
2026-03-20 09:20:17 +01:00
parent 2a59c4bf3e
commit fca05e7a94

View File

@@ -1,21 +1,17 @@
import os
from pathlib import Path
from urllib.parse import quote_plus
from dotenv import load_dotenv
BASE_DIR = Path(__file__).resolve().parent
load_dotenv(BASE_DIR / '.env')
def _normalize_sqlalchemy_db_url(raw: str | None) -> str:
if raw:
raw = raw.strip()
if raw.startswith('postgres://'):
return raw.replace('postgres://', 'postgresql+psycopg://', 1)
if raw.startswith('sqlite:///') and not raw.startswith('sqlite:////'):
rel = raw.replace('sqlite:///', '', 1)
return f"sqlite:///{(BASE_DIR / rel).resolve()}"
return raw
def _q(value: str) -> str:
return quote_plus(value or '')
def _build_db_url() -> str:
db_engine = os.getenv('DB_ENGINE', 'sqlite').strip().lower()
db_host = os.getenv('DB_HOST', 'localhost').strip()
db_port = os.getenv('DB_PORT', '').strip()
@@ -28,11 +24,17 @@ def _normalize_sqlalchemy_db_url(raw: str | None) -> str:
if db_engine in ('pgsql', 'postgres', 'postgresql'):
port = db_port or '5432'
return f"postgresql+psycopg://{db_user}:{db_password}@{db_host}:{port}/{db_name}"
return (
f"postgresql+psycopg://{_q(db_user)}:{_q(db_password)}"
f"@{db_host}:{port}/{_q(db_name)}"
)
if db_engine == 'mysql':
port = db_port or '3306'
return f"mysql+pymysql://{db_user}:{db_password}@{db_host}:{port}/{db_name}"
return (
f"mysql+pymysql://{_q(db_user)}:{_q(db_password)}"
f"@{db_host}:{port}/{_q(db_name)}"
)
raise ValueError(f"Nieobsługiwany DB_ENGINE: {db_engine}")
@@ -57,7 +59,7 @@ def _normalize_redis_url(raw: str | None) -> str:
class Config:
SECRET_KEY = os.getenv('SECRET_KEY', 'change-me-please')
APP_MASTER_KEY = os.getenv('APP_MASTER_KEY', SECRET_KEY)
SQLALCHEMY_DATABASE_URI = _normalize_sqlalchemy_db_url(os.getenv('DATABASE_URL'))
SQLALCHEMY_DATABASE_URI = _build_db_url()
SQLALCHEMY_TRACK_MODIFICATIONS = False
ARCHIVE_PATH = _path_from_env('ARCHIVE_PATH', BASE_DIR / 'storage' / 'archive')
PDF_PATH = _path_from_env('PDF_PATH', BASE_DIR / 'storage' / 'pdf')
@@ -77,8 +79,6 @@ class Config:
SESSION_COOKIE_HTTPONLY = True
REMEMBER_COOKIE_HTTPONLY = True
SESSION_COOKIE_SAMESITE = 'Lax'
#CEIDG_API_URL = os.getenv('CEIDG_API_URL', 'https://dane.biznes.gov.pl/api/ceidg/v2/firmy')
#CEIDG_TEST_API_URL = os.getenv('CEIDG_TEST_API_URL', 'https://test-dane.biznes.gov.pl/api/ceidg/v2/firmy')
APP_FOOTER_TEXT = 'KSeF Manager · linuxiarz.pl · Mateusz Gruszczyński'
@@ -87,4 +87,4 @@ class TestConfig(Config):
WTF_CSRF_ENABLED = False
SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'
REDIS_URL = 'memory://'
RATELIMIT_STORAGE_URI = 'memory://'
RATELIMIT_STORAGE_URI = 'memory://'