fix: include work journal in project export/import with old-archive compatibility

This commit is contained in:
Арсентьев Александр
2026-09-28 17:45:39 +03:00
parent b5fa601e93
commit fac2816622
7 changed files with 594 additions and 8 deletions
+34 -1
View File
@@ -19,6 +19,7 @@ from app.models.ks2 import KS2, KS2Item, KS2ItemSource
from app.models.ks3 import KS3, KS3KS2
from app.models.payment import Payment, PaymentAllocation
from app.models.project import Project
from app.models.work_journal import WorkJournalEntry
FORMAT_NAME = "work_volume_tracker"
FORMAT_VERSION = 1
@@ -44,6 +45,10 @@ REQUIRED_FILES = (
"checksums.json",
)
# Old Task 025 archives may lack this file; import treats it as an empty journal.
OPTIONAL_FILES = ("work_journal_entries.json",)
EXPORT_FILES = REQUIRED_FILES[:-1] + OPTIONAL_FILES + ("checksums.json",)
class ProjectExportError(RuntimeError):
"""Raised when the project cannot be exported safely."""
@@ -173,6 +178,12 @@ def collect_project(db: Session, project_id: int) -> dict[str, Any]:
.order_by(PaymentAllocation.id)
.all()
)
work_journal_entries = (
db.query(WorkJournalEntry)
.filter(WorkJournalEntry.project_id == project_id)
.order_by(WorkJournalEntry.id)
.all()
)
return {
"project": project,
@@ -191,6 +202,7 @@ def collect_project(db: Session, project_id: int) -> dict[str, Any]:
"ks3_ks2": ks3_ks2,
"payments": payments,
"payment_allocations": payment_allocations,
"work_journal_entries": work_journal_entries,
}
@@ -292,6 +304,9 @@ def _validate_graph(graph: dict[str, Any]) -> None:
errors.append(f"payment_allocation:{a.id} references missing payment")
if a.ks3_id not in ks3_ids:
errors.append(f"payment_allocation:{a.id} references missing ks3")
for entry in graph.get("work_journal_entries", []):
if entry.project_id != pid:
errors.append(f"work_journal_entry:{entry.id} belongs to another project")
if errors:
raise ProjectExportError(
@@ -323,6 +338,7 @@ def serialize_project(graph: dict[str, Any]) -> dict[str, list | dict]:
"ks3_ks2": _key_map(graph["ks3_ks2"], "ks3_ks2"),
"payment": _key_map(graph["payments"], "payment"),
"payment_allocation": _key_map(graph["payment_allocations"], "payment_allocation"),
"work_journal_entry": _key_map(graph.get("work_journal_entries", []), "work_journal_entry"),
}
def ref(kind: str, value: int | None) -> str | None:
@@ -584,6 +600,21 @@ def serialize_project(graph: dict[str, Any]) -> dict[str, list | dict]:
)
for a in graph["payment_allocations"]
]
work_journal_entries = [
record(
"work_journal_entry",
e,
{
"type": e.type,
"text": e.text,
"status": e.status,
"due_date": _iso(e.due_date),
"created_at": _iso(e.created_at),
"updated_at": _iso(e.updated_at),
},
)
for e in graph.get("work_journal_entries", [])
]
counts = {
"boq_documents": len(boq_documents),
@@ -601,6 +632,7 @@ def serialize_project(graph: dict[str, Any]) -> dict[str, list | dict]:
"ks3_ks2": len(ks3_ks2),
"payments": len(payments),
"payment_allocations": len(payment_allocations),
"work_journal_entries": len(work_journal_entries),
}
manifest = {
"format": FORMAT_NAME,
@@ -629,6 +661,7 @@ def serialize_project(graph: dict[str, Any]) -> dict[str, list | dict]:
"ks3_ks2.json": ks3_ks2,
"payments.json": payments,
"payment_allocations.json": payment_allocations,
"work_journal_entries.json": work_journal_entries,
}
@@ -640,7 +673,7 @@ def build_archive(files: dict[str, Any]) -> tuple[bytes, str]:
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w", compression=zipfile.ZIP_DEFLATED) as zf:
for name in REQUIRED_FILES:
for name in EXPORT_FILES:
if name not in parts:
raise ProjectExportError(f"В архиве отсутствует обязательный файл {name}")
zf.writestr(name, parts[name])