diff --git a/src/api/main.py b/src/api/main.py index 5cffa6a..4e861c1 100644 --- a/src/api/main.py +++ b/src/api/main.py @@ -22,7 +22,7 @@ from starlette.middleware.sessions import SessionMiddleware import db from api.auth import get_current_user, resolve_account_for_user from api.schemas import ContactListResponse, ContactOut, SyncRunOut -from config import TIMEZONE, Config +from config import Config from mailer import build_message, fetch_birthdays_for_date, send_message logging.basicConfig(level=Config.LOG_LEVEL, format="%(asctime)s [%(levelname)s] %(message)s") @@ -41,7 +41,7 @@ def _fmt_ts(dt) -> str | None: if dt.tzinfo is None: from datetime import timezone dt = dt.replace(tzinfo=timezone.utc) - return dt.astimezone(TIMEZONE).strftime("%d.%m.%Y %H:%M:%S") + return dt.astimezone(Config.TIMEZONE).strftime("%d.%m.%Y %H:%M:%S") def _row_to_contact_out(row: dict) -> dict: @@ -137,7 +137,7 @@ def get_contact(contact_id: int, current_user: str = Depends(get_current_user)): @app.get("/api/contacts/birthdays/today", response_model=list[ContactOut]) def birthdays_today(current_user: str = Depends(get_current_user)): account_name, is_admin = resolve_account_for_user(current_user) - today = datetime.now(TIMEZONE).date() + today = datetime.now(Config.TIMEZONE).date() with db.get_connection() as conn: where_clause, params = _account_filter_clause(account_name) @@ -253,8 +253,8 @@ def web_dashboard( "upcoming_birthdays": upcoming_birthdays, "last_sync": last_sync, "last_sync_with_changes": last_sync_with_changes, - "current_year": datetime.now(TIMEZONE).date().year, - "today": datetime.now(TIMEZONE).date(), + "current_year": datetime.now(Config.TIMEZONE).date().year, + "today": datetime.now(Config.TIMEZONE).date(), }, ) diff --git a/src/db.py b/src/db.py index 6e6e012..bda05dc 100644 --- a/src/db.py +++ b/src/db.py @@ -10,7 +10,7 @@ from datetime import datetime, timezone import pymysql from pymysql.cursors import DictCursor -from config import TIMEZONE, Config +from config import Config logger = logging.getLogger(__name__) @@ -219,7 +219,7 @@ def search_contacts_without_social(conn, account: str | None) -> list[dict]: def get_upcoming_birthdays(conn, account: str | None, days: int = 7) -> list[dict]: where_clause, params = _account_filter_clause(account) - today = datetime.now(TIMEZONE).date() + today = datetime.now(Config.TIMEZONE).date() with conn.cursor() as cur: cur.execute( f"""SELECT id, full_name, given_name, middle_name, family_name, diff --git a/src/mailer.py b/src/mailer.py index 7e240b3..b79181f 100644 --- a/src/mailer.py +++ b/src/mailer.py @@ -12,7 +12,7 @@ from datetime import date, datetime from email.message import EmailMessage import db -from config import TIMEZONE, Config +from config import Config logging.basicConfig(level=Config.LOG_LEVEL, format="%(asctime)s [%(levelname)s] %(message)s") logger = logging.getLogger("mailer") @@ -33,18 +33,18 @@ def fetch_birthdays_for_date(conn, target_date: date) -> list[dict]: def fetch_todays_birthdays(conn) -> list[dict]: - return fetch_birthdays_for_date(conn, datetime.now(TIMEZONE).date()) + return fetch_birthdays_for_date(conn, datetime.now(Config.TIMEZONE).date()) def already_sent_today(conn) -> bool: - today = datetime.now(TIMEZONE).date() + today = datetime.now(Config.TIMEZONE).date() with conn.cursor() as cur: cur.execute("SELECT 1 FROM birthday_mail_log WHERE sent_date = %s", (today,)) return cur.fetchone() is not None def log_sent(conn, count: int): - today = datetime.now(TIMEZONE).date() + today = datetime.now(Config.TIMEZONE).date() with conn.cursor() as cur: cur.execute( "INSERT INTO birthday_mail_log (sent_date, contacts_count) VALUES (%s, %s)", @@ -54,7 +54,7 @@ def log_sent(conn, count: int): def build_message(birthdays: list[dict], target_date: date | None = None) -> EmailMessage: - today = target_date or datetime.now(TIMEZONE).date() + today = target_date or datetime.now(Config.TIMEZONE).date() msg = EmailMessage() msg["From"] = Config.MAIL_FROM msg["To"] = Config.MAIL_TO diff --git a/src/scheduler.py b/src/scheduler.py index f3a4ff6..506fa6b 100644 --- a/src/scheduler.py +++ b/src/scheduler.py @@ -19,7 +19,7 @@ import time from datetime import datetime, timedelta import db -from config import TIMEZONE +from config import Config logging.basicConfig( level=os.environ.get("LOG_LEVEL", "INFO"), @@ -63,7 +63,7 @@ def run_sync(): run_script("Sync", "/app/sync.py") try: with open("/tmp/last_sync_ok", "w") as f: - f.write(datetime.now(TIMEZONE).isoformat()) + f.write(datetime.now(Config.TIMEZONE).isoformat()) except OSError: pass @@ -75,7 +75,7 @@ def run_mailer(): def next_run_time(hour: int) -> datetime: - now = datetime.now(TIMEZONE) + now = datetime.now(Config.TIMEZONE) target = now.replace(hour=hour, minute=0, second=0, microsecond=0) if target <= now: target += timedelta(days=1) @@ -104,11 +104,11 @@ def main(): logger.info("Starte ersten Sync-Lauf...") run_sync() - last_sync = datetime.now(TIMEZONE) + last_sync = datetime.now(Config.TIMEZONE) next_mailer = next_run_time(MAIL_SEND_HOUR) while not _shutdown: - now = datetime.now(TIMEZONE) + now = datetime.now(Config.TIMEZONE) if now >= last_sync + timedelta(minutes=SYNC_INTERVAL_MINUTES): run_sync()