extending abs_path in db and test coverage
This commit is contained in:
30
web/db.py
30
web/db.py
@@ -117,6 +117,18 @@ class CachedPage(Base):
|
||||
|
||||
listing = relationship("JobListing", back_populates="cached_pages")
|
||||
|
||||
@property
|
||||
def abs_path(self) -> Optional[str]:
|
||||
"""Return the absolute filesystem path for this cached page.
|
||||
|
||||
The DB stores `file_path` relative to the configured cache dir. This
|
||||
helper centralizes resolution so callers can use `cached_page.abs_path`.
|
||||
"""
|
||||
fp = getattr(self, 'file_path', None)
|
||||
if not fp:
|
||||
return None
|
||||
return os.path.join(os.path.abspath(get_cache_dir()), fp)
|
||||
|
||||
|
||||
class UserInteraction(Base):
|
||||
__tablename__ = "user_interactions"
|
||||
@@ -323,16 +335,18 @@ def db_get_all_cached_pages() -> List[Dict[str, Any]]:
|
||||
with _ensure_session() as session:
|
||||
rows = session.execute(text(
|
||||
"SELECT file_path, url_guess, last_modified, size_bytes, job_id FROM cached_pages")).fetchall()
|
||||
return [
|
||||
{
|
||||
"file_path": row[0],
|
||||
out = []
|
||||
for row in rows:
|
||||
fp = row[0]
|
||||
out.append({
|
||||
"file_path": fp,
|
||||
"file_path_abs": db_get_cached_abs_path(fp) if fp else None,
|
||||
"url_guess": row[1],
|
||||
"last_modified": row[2],
|
||||
"size_bytes": row[3],
|
||||
"job_id": row[4],
|
||||
}
|
||||
for row in rows
|
||||
]
|
||||
})
|
||||
return out
|
||||
|
||||
|
||||
def db_get_cache_url(url: str):
|
||||
@@ -346,8 +360,10 @@ def db_get_cache_url(url: str):
|
||||
"SELECT file_path, url_guess, last_modified, size_bytes, job_id FROM cached_pages WHERE url_guess = :u"), {"u": url}).fetchone()
|
||||
if not row:
|
||||
return None
|
||||
fp = row[0]
|
||||
return {
|
||||
"file_path": row[0],
|
||||
"file_path": fp,
|
||||
"file_path_abs": db_get_cached_abs_path(fp) if fp else None,
|
||||
"url_guess": row[1],
|
||||
"last_modified": row[2],
|
||||
"size_bytes": row[3],
|
||||
|
||||
Reference in New Issue
Block a user