mirror of
https://github.com/skoelle/icloud-contacts-sync.git
synced 2026-09-17 15:30:24 +00:00
timezone config fix
This commit is contained in:
+5
-5
@@ -22,7 +22,7 @@ from starlette.middleware.sessions import SessionMiddleware
|
|||||||
import db
|
import db
|
||||||
from api.auth import get_current_user, resolve_account_for_user
|
from api.auth import get_current_user, resolve_account_for_user
|
||||||
from api.schemas import ContactListResponse, ContactOut, SyncRunOut
|
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
|
from mailer import build_message, fetch_birthdays_for_date, send_message
|
||||||
|
|
||||||
logging.basicConfig(level=Config.LOG_LEVEL, format="%(asctime)s [%(levelname)s] %(message)s")
|
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:
|
if dt.tzinfo is None:
|
||||||
from datetime import timezone
|
from datetime import timezone
|
||||||
dt = dt.replace(tzinfo=timezone.utc)
|
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:
|
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])
|
@app.get("/api/contacts/birthdays/today", response_model=list[ContactOut])
|
||||||
def birthdays_today(current_user: str = Depends(get_current_user)):
|
def birthdays_today(current_user: str = Depends(get_current_user)):
|
||||||
account_name, is_admin = resolve_account_for_user(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:
|
with db.get_connection() as conn:
|
||||||
where_clause, params = _account_filter_clause(account_name)
|
where_clause, params = _account_filter_clause(account_name)
|
||||||
@@ -253,8 +253,8 @@ def web_dashboard(
|
|||||||
"upcoming_birthdays": upcoming_birthdays,
|
"upcoming_birthdays": upcoming_birthdays,
|
||||||
"last_sync": last_sync,
|
"last_sync": last_sync,
|
||||||
"last_sync_with_changes": last_sync_with_changes,
|
"last_sync_with_changes": last_sync_with_changes,
|
||||||
"current_year": datetime.now(TIMEZONE).date().year,
|
"current_year": datetime.now(Config.TIMEZONE).date().year,
|
||||||
"today": datetime.now(TIMEZONE).date(),
|
"today": datetime.now(Config.TIMEZONE).date(),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ from datetime import datetime, timezone
|
|||||||
import pymysql
|
import pymysql
|
||||||
from pymysql.cursors import DictCursor
|
from pymysql.cursors import DictCursor
|
||||||
|
|
||||||
from config import TIMEZONE, Config
|
from config import Config
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
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]:
|
def get_upcoming_birthdays(conn, account: str | None, days: int = 7) -> list[dict]:
|
||||||
where_clause, params = _account_filter_clause(account)
|
where_clause, params = _account_filter_clause(account)
|
||||||
today = datetime.now(TIMEZONE).date()
|
today = datetime.now(Config.TIMEZONE).date()
|
||||||
with conn.cursor() as cur:
|
with conn.cursor() as cur:
|
||||||
cur.execute(
|
cur.execute(
|
||||||
f"""SELECT id, full_name, given_name, middle_name, family_name,
|
f"""SELECT id, full_name, given_name, middle_name, family_name,
|
||||||
|
|||||||
+5
-5
@@ -12,7 +12,7 @@ from datetime import date, datetime
|
|||||||
from email.message import EmailMessage
|
from email.message import EmailMessage
|
||||||
|
|
||||||
import db
|
import db
|
||||||
from config import TIMEZONE, Config
|
from config import Config
|
||||||
|
|
||||||
logging.basicConfig(level=Config.LOG_LEVEL, format="%(asctime)s [%(levelname)s] %(message)s")
|
logging.basicConfig(level=Config.LOG_LEVEL, format="%(asctime)s [%(levelname)s] %(message)s")
|
||||||
logger = logging.getLogger("mailer")
|
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]:
|
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:
|
def already_sent_today(conn) -> bool:
|
||||||
today = datetime.now(TIMEZONE).date()
|
today = datetime.now(Config.TIMEZONE).date()
|
||||||
with conn.cursor() as cur:
|
with conn.cursor() as cur:
|
||||||
cur.execute("SELECT 1 FROM birthday_mail_log WHERE sent_date = %s", (today,))
|
cur.execute("SELECT 1 FROM birthday_mail_log WHERE sent_date = %s", (today,))
|
||||||
return cur.fetchone() is not None
|
return cur.fetchone() is not None
|
||||||
|
|
||||||
|
|
||||||
def log_sent(conn, count: int):
|
def log_sent(conn, count: int):
|
||||||
today = datetime.now(TIMEZONE).date()
|
today = datetime.now(Config.TIMEZONE).date()
|
||||||
with conn.cursor() as cur:
|
with conn.cursor() as cur:
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"INSERT INTO birthday_mail_log (sent_date, contacts_count) VALUES (%s, %s)",
|
"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:
|
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 = EmailMessage()
|
||||||
msg["From"] = Config.MAIL_FROM
|
msg["From"] = Config.MAIL_FROM
|
||||||
msg["To"] = Config.MAIL_TO
|
msg["To"] = Config.MAIL_TO
|
||||||
|
|||||||
+5
-5
@@ -19,7 +19,7 @@ import time
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
import db
|
import db
|
||||||
from config import TIMEZONE
|
from config import Config
|
||||||
|
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
level=os.environ.get("LOG_LEVEL", "INFO"),
|
level=os.environ.get("LOG_LEVEL", "INFO"),
|
||||||
@@ -63,7 +63,7 @@ def run_sync():
|
|||||||
run_script("Sync", "/app/sync.py")
|
run_script("Sync", "/app/sync.py")
|
||||||
try:
|
try:
|
||||||
with open("/tmp/last_sync_ok", "w") as f:
|
with open("/tmp/last_sync_ok", "w") as f:
|
||||||
f.write(datetime.now(TIMEZONE).isoformat())
|
f.write(datetime.now(Config.TIMEZONE).isoformat())
|
||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -75,7 +75,7 @@ def run_mailer():
|
|||||||
|
|
||||||
|
|
||||||
def next_run_time(hour: int) -> datetime:
|
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)
|
target = now.replace(hour=hour, minute=0, second=0, microsecond=0)
|
||||||
if target <= now:
|
if target <= now:
|
||||||
target += timedelta(days=1)
|
target += timedelta(days=1)
|
||||||
@@ -104,11 +104,11 @@ def main():
|
|||||||
logger.info("Starte ersten Sync-Lauf...")
|
logger.info("Starte ersten Sync-Lauf...")
|
||||||
run_sync()
|
run_sync()
|
||||||
|
|
||||||
last_sync = datetime.now(TIMEZONE)
|
last_sync = datetime.now(Config.TIMEZONE)
|
||||||
next_mailer = next_run_time(MAIL_SEND_HOUR)
|
next_mailer = next_run_time(MAIL_SEND_HOUR)
|
||||||
|
|
||||||
while not _shutdown:
|
while not _shutdown:
|
||||||
now = datetime.now(TIMEZONE)
|
now = datetime.now(Config.TIMEZONE)
|
||||||
|
|
||||||
if now >= last_sync + timedelta(minutes=SYNC_INTERVAL_MINUTES):
|
if now >= last_sync + timedelta(minutes=SYNC_INTERVAL_MINUTES):
|
||||||
run_sync()
|
run_sync()
|
||||||
|
|||||||
Reference in New Issue
Block a user