fix: WebDAV year filter uses min year (2025+) not fixed list
Include all year folders from 2025 onwards (2026, 2027, …) and skip or mark anything older.
This commit is contained in:
1 parent
28164ad9f4
commit
d3c26a453e
8 files changed
+35
-41
No files matched your search
@@ -1,4 +1,4 @@
|
||||
"""Add webdav_include_years for year-folder filtering
|
||||
"""Add webdav_min_year for year-folder filtering (2025+)
|
||||
|
||||
Revision ID: 005_webdav_years
|
||||
Revises: 004_webdav
|
||||
@@ -16,8 +16,8 @@ depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column("accounts", sa.Column("webdav_include_years", sa.JSON(), nullable=True))
|
||||
op.add_column("accounts", sa.Column("webdav_min_year", sa.Integer(), nullable=True))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_column("accounts", "webdav_include_years")
|
||||
op.drop_column("accounts", "webdav_min_year")
|
||||
@@ -20,7 +20,7 @@ class AccountBase(BaseModel):
|
||||
webdav_path: str | None = None
|
||||
webdav_user: str | None = None
|
||||
webdav_verify_ssl: bool = True
|
||||
webdav_include_years: list[str] | None = None
|
||||
webdav_min_year: int | None = None
|
||||
delivery_mode: str = "smtp"
|
||||
smtp_host: str | None = None
|
||||
smtp_port: int = 587
|
||||
@@ -117,7 +117,7 @@ class AccountUpdate(BaseModel):
|
||||
webdav_user: str | None = None
|
||||
webdav_password: str | None = None
|
||||
webdav_verify_ssl: bool | None = None
|
||||
webdav_include_years: list[str] | None = None
|
||||
webdav_min_year: int | None = None
|
||||
smtp_host: str | None = None
|
||||
smtp_port: int | None = None
|
||||
smtp_user: str | None = None
|
||||
|
||||
@@ -93,7 +93,7 @@ class Account(Base):
|
||||
webdav_user: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
webdav_password_encrypted: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
webdav_verify_ssl: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
|
||||
webdav_include_years: Mapped[list[Any] | None] = mapped_column(JSON, nullable=True)
|
||||
webdav_min_year: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||||
|
||||
smtp_host: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
smtp_port: Mapped[int] = mapped_column(Integer, default=587, nullable=False)
|
||||
|
||||
@@ -56,7 +56,7 @@ def account_to_dict(account: Account, *, include_secrets: bool = False) -> dict:
|
||||
"webdav_user": account.webdav_user,
|
||||
"webdav_password": mask_password(),
|
||||
"webdav_verify_ssl": account.webdav_verify_ssl,
|
||||
"webdav_include_years": account.webdav_include_years,
|
||||
"webdav_min_year": account.webdav_min_year,
|
||||
"delivery_mode": delivery_mode,
|
||||
"smtp_host": account.smtp_host,
|
||||
"smtp_port": account.smtp_port,
|
||||
@@ -138,7 +138,7 @@ def create_account(db: Session, data: dict) -> Account:
|
||||
else None
|
||||
),
|
||||
webdav_verify_ssl=data.get("webdav_verify_ssl", True),
|
||||
webdav_include_years=data.get("webdav_include_years") if source_type == SourceType.webdav else None,
|
||||
webdav_min_year=data.get("webdav_min_year") if source_type == SourceType.webdav else None,
|
||||
delivery_mode=delivery_mode,
|
||||
smtp_host=data.get("smtp_host"),
|
||||
smtp_port=data.get("smtp_port", 587),
|
||||
@@ -181,7 +181,7 @@ def update_account(db: Session, account_id: int, data: dict) -> Account:
|
||||
"webdav_path",
|
||||
"webdav_user",
|
||||
"webdav_verify_ssl",
|
||||
"webdav_include_years",
|
||||
"webdav_min_year",
|
||||
"smtp_host",
|
||||
"smtp_port",
|
||||
"smtp_user",
|
||||
|
||||
@@ -44,7 +44,7 @@ from forwarder.webdav.client import (
|
||||
list_files,
|
||||
normalize_webdav_folder_path,
|
||||
parse_webdav_message_key,
|
||||
webdav_path_matches_include_years,
|
||||
webdav_path_matches_min_year,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -109,9 +109,9 @@ def sync_webdav_account(
|
||||
):
|
||||
continue
|
||||
|
||||
if not webdav_path_matches_include_years(
|
||||
if not webdav_path_matches_min_year(
|
||||
remote_file.relative_path,
|
||||
account.webdav_include_years,
|
||||
account.webdav_min_year,
|
||||
):
|
||||
continue
|
||||
|
||||
|
||||
@@ -104,19 +104,16 @@ def webdav_year_folder(relative_path: str) -> str | None:
|
||||
return None
|
||||
|
||||
|
||||
def webdav_path_matches_include_years(
|
||||
def webdav_path_matches_min_year(
|
||||
relative_path: str,
|
||||
include_years: list[str] | None,
|
||||
min_year: int | None,
|
||||
) -> bool:
|
||||
if not include_years:
|
||||
if min_year is None:
|
||||
return True
|
||||
allowed = {year.strip() for year in include_years if year and year.strip()}
|
||||
if not allowed:
|
||||
return True
|
||||
year = webdav_year_folder(relative_path)
|
||||
if year is None:
|
||||
year_label = webdav_year_folder(relative_path)
|
||||
if year_label is None:
|
||||
return False
|
||||
return year in allowed
|
||||
return int(year_label) >= min_year
|
||||
|
||||
|
||||
def _client(config: WebdavConfig, *, timeout: float = 60.0) -> httpx.Client:
|
||||
|
||||
@@ -7,7 +7,7 @@ from forwarder.webdav.client import (
|
||||
get_webdav_message_key,
|
||||
normalize_webdav_folder_path,
|
||||
parse_webdav_message_key,
|
||||
webdav_path_matches_include_years,
|
||||
webdav_path_matches_min_year,
|
||||
)
|
||||
|
||||
PROPFIND_SAMPLE = b"""<?xml version="1.0" encoding="utf-8"?>
|
||||
@@ -72,12 +72,13 @@ def test_webdav_message_key_roundtrip():
|
||||
assert parse_webdav_message_key("imap:123") is None
|
||||
|
||||
|
||||
def test_webdav_path_matches_include_years():
|
||||
assert webdav_path_matches_include_years("2025/invoice.pdf", ["2025", "2026"])
|
||||
assert webdav_path_matches_include_years("2026/a.pdf", ["2025", "2026"])
|
||||
assert not webdav_path_matches_include_years("2024/old.pdf", ["2025", "2026"])
|
||||
assert not webdav_path_matches_include_years("misc/file.pdf", ["2025", "2026"])
|
||||
assert webdav_path_matches_include_years("2024/old.pdf", None)
|
||||
def test_webdav_path_matches_min_year():
|
||||
assert webdav_path_matches_min_year("2025/invoice.pdf", 2025)
|
||||
assert webdav_path_matches_min_year("2026/a.pdf", 2025)
|
||||
assert webdav_path_matches_min_year("2027/future.pdf", 2025)
|
||||
assert not webdav_path_matches_min_year("2024/old.pdf", 2025)
|
||||
assert not webdav_path_matches_min_year("misc/file.pdf", 2025)
|
||||
assert webdav_path_matches_min_year("2024/old.pdf", None)
|
||||
|
||||
|
||||
def test_parse_propfind_response():
|
||||
|
||||
@@ -16,7 +16,7 @@ from forwarder.webdav.client import (
|
||||
get_webdav_message_key,
|
||||
list_files,
|
||||
normalize_webdav_folder_path,
|
||||
webdav_path_matches_include_years,
|
||||
webdav_path_matches_min_year,
|
||||
webdav_year_folder,
|
||||
)
|
||||
|
||||
@@ -25,9 +25,10 @@ def main() -> int:
|
||||
parser = argparse.ArgumentParser(description="WebDAV year filter setup")
|
||||
parser.add_argument("account_id", type=int)
|
||||
parser.add_argument(
|
||||
"--allowed-years",
|
||||
default="2025,2026",
|
||||
help="Comma-separated year folder names to keep active (default: 2025,2026)",
|
||||
"--min-year",
|
||||
type=int,
|
||||
default=2025,
|
||||
help="Minimum year folder to keep active (default: 2025 → includes 2025, 2026, …)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--mark-skipped",
|
||||
@@ -37,11 +38,6 @@ def main() -> int:
|
||||
parser.add_argument("--dry-run", action="store_true")
|
||||
args = parser.parse_args()
|
||||
|
||||
allowed = {year.strip() for year in args.allowed_years.split(",") if year.strip()}
|
||||
if not allowed:
|
||||
print("No allowed years specified", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
account = get_account(db, args.account_id)
|
||||
@@ -49,7 +45,6 @@ def main() -> int:
|
||||
files = list_files(config)
|
||||
folder_label = normalize_webdav_folder_path(account.webdav_path)
|
||||
|
||||
allowed_list = sorted(allowed)
|
||||
by_year: Counter[str] = Counter()
|
||||
unknown = 0
|
||||
for remote_file in files:
|
||||
@@ -60,9 +55,10 @@ def main() -> int:
|
||||
by_year[year] += 1
|
||||
|
||||
print(f"Account {account.id} ({account.name}): {len(files)} file(s) listed")
|
||||
print(f"Min year: {args.min_year} (includes {args.min_year}+)")
|
||||
print("By year folder:")
|
||||
for year in sorted(by_year):
|
||||
flag = "KEEP" if year in allowed else "SKIP"
|
||||
flag = "KEEP" if int(year) >= args.min_year else "SKIP"
|
||||
print(f" {year}: {by_year[year]} ({flag})")
|
||||
if unknown:
|
||||
print(f" (no year folder): {unknown}")
|
||||
@@ -72,7 +68,7 @@ def main() -> int:
|
||||
|
||||
marked = 0
|
||||
for remote_file in files:
|
||||
if webdav_path_matches_include_years(remote_file.relative_path, allowed_list):
|
||||
if webdav_path_matches_min_year(remote_file.relative_path, args.min_year):
|
||||
continue
|
||||
message_key = get_webdav_message_key(remote_file.relative_path)
|
||||
if args.dry_run:
|
||||
@@ -93,7 +89,7 @@ def main() -> int:
|
||||
marked += 1
|
||||
|
||||
action = "Would mark" if args.dry_run else "Marked"
|
||||
print(f"{action} {marked} file(s) as forwarded (outside {sorted(allowed)})")
|
||||
print(f"{action} {marked} file(s) as forwarded (before {args.min_year})")
|
||||
return 0
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
Reference in new issue
Block a user