From f9bcbabb8693fd433a4803e2221472640b86305e Mon Sep 17 00:00:00 2001 From: MarekWo Date: Tue, 24 Mar 2026 08:26:07 +0100 Subject: [PATCH] fix: use Flask current_app for DB access in read_status and contacts_cache MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 'from app.main import db' gets None because python -m app.main loads the module as __main__, creating a separate module instance from app.main. Use current_app.db (Flask app context) instead — same pattern as api.py. Co-Authored-By: Claude Opus 4.6 --- app/contacts_cache.py | 6 +++--- app/read_status.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/contacts_cache.py b/app/contacts_cache.py index 0e7383d..5c5faf8 100644 --- a/app/contacts_cache.py +++ b/app/contacts_cache.py @@ -11,6 +11,7 @@ parse_advert_payload(). import logging import math import struct +from flask import current_app logger = logging.getLogger(__name__) @@ -18,9 +19,8 @@ _TYPE_LABELS = {0: 'COM', 1: 'COM', 2: 'REP', 3: 'ROOM', 4: 'SENS'} def _get_db(): - """Get database instance (deferred import to avoid circular imports).""" - from app.main import db - return db + """Get database instance from Flask app context.""" + return getattr(current_app, 'db', None) def get_all_contacts() -> list: diff --git a/app/read_status.py b/app/read_status.py index a4a2f2a..af81ed3 100644 --- a/app/read_status.py +++ b/app/read_status.py @@ -7,14 +7,14 @@ All data is stored in the read_status table of the SQLite database. """ import logging +from flask import current_app logger = logging.getLogger(__name__) def _get_db(): - """Get database instance (deferred import to avoid circular imports).""" - from app.main import db - return db + """Get database instance from Flask app context.""" + return getattr(current_app, 'db', None) def load_read_status():