feat: add photos to daily reports
This commit is contained in:
@@ -15,6 +15,7 @@ from sqlalchemy.orm import Session, joinedload
|
||||
from app.models.agreement import Agreement, AgreementItem
|
||||
from app.models.boq import BoqDocument, Section, System, WorkItem
|
||||
from app.models.daily_report import DailyReport, DailyReportItem
|
||||
from app.models.daily_report_photo import DailyReportPhoto
|
||||
from app.models.ks2 import KS2, KS2Item, KS2ItemSource
|
||||
from app.models.ks3 import KS3, KS3KS2
|
||||
from app.models.payment import Payment, PaymentAllocation
|
||||
@@ -46,8 +47,12 @@ 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", "document_artifacts.json")
|
||||
# Old Task 025 archives may lack these files; import treats them as empty.
|
||||
OPTIONAL_FILES = (
|
||||
"work_journal_entries.json",
|
||||
"document_artifacts.json",
|
||||
"daily_report_photos.json",
|
||||
)
|
||||
EXPORT_FILES = REQUIRED_FILES[:-1] + OPTIONAL_FILES + ("checksums.json",)
|
||||
|
||||
|
||||
@@ -198,6 +203,13 @@ def collect_project(db: Session, project_id: int) -> dict[str, Any]:
|
||||
.order_by(DocumentArtifact.id)
|
||||
.all()
|
||||
)
|
||||
# Daily Report photos belong to the primary report and travel with it.
|
||||
daily_report_photos = (
|
||||
db.query(DailyReportPhoto)
|
||||
.filter(DailyReportPhoto.project_id == project_id)
|
||||
.order_by(DailyReportPhoto.id)
|
||||
.all()
|
||||
)
|
||||
|
||||
return {
|
||||
"project": project,
|
||||
@@ -209,6 +221,7 @@ def collect_project(db: Session, project_id: int) -> dict[str, Any]:
|
||||
"agreement_items": agreement_items,
|
||||
"daily_reports": daily_reports,
|
||||
"daily_report_items": daily_report_items,
|
||||
"daily_report_photos": daily_report_photos,
|
||||
"ks2": ks2_docs,
|
||||
"ks2_items": ks2_items,
|
||||
"ks2_item_sources": ks2_item_sources,
|
||||
@@ -291,6 +304,11 @@ def _validate_graph(graph: dict[str, Any]) -> None:
|
||||
errors.append(f"daily_report_item:{i.id} references missing daily_report")
|
||||
if i.work_item_id not in work_item_ids:
|
||||
errors.append(f"daily_report_item:{i.id} references missing work_item")
|
||||
for p in graph.get("daily_report_photos", []):
|
||||
if p.daily_report_id not in report_ids:
|
||||
errors.append(f"daily_report_photo:{p.id} references missing daily_report")
|
||||
if p.project_id != pid:
|
||||
errors.append(f"daily_report_photo:{p.id} belongs to another project")
|
||||
for k in graph["ks2"]:
|
||||
if k.project_id != pid:
|
||||
errors.append(f"ks2:{k.id} belongs to another project")
|
||||
@@ -347,6 +365,7 @@ def serialize_project(graph: dict[str, Any]) -> dict[str, list | dict]:
|
||||
"agreement_item": _key_map(graph["agreement_items"], "agreement_item"),
|
||||
"daily_report": _key_map(graph["daily_reports"], "daily_report"),
|
||||
"daily_report_item": _key_map(graph["daily_report_items"], "daily_report_item"),
|
||||
"daily_report_photo": _key_map(graph.get("daily_report_photos", []), "daily_report_photo"),
|
||||
"ks2": _key_map(graph["ks2"], "ks2"),
|
||||
"ks2_item": _key_map(graph["ks2_items"], "ks2_item"),
|
||||
"ks2_item_source": _key_map(graph["ks2_item_sources"], "ks2_item_source"),
|
||||
@@ -526,6 +545,38 @@ def serialize_project(graph: dict[str, Any]) -> dict[str, list | dict]:
|
||||
)
|
||||
for i in graph["daily_report_items"]
|
||||
]
|
||||
daily_report_photo_records = []
|
||||
photo_blobs: dict[str, bytes] = {}
|
||||
for photo in graph.get("daily_report_photos", []):
|
||||
photo_export_id = ref("daily_report", photo.daily_report_id)
|
||||
if photo_export_id is None:
|
||||
raise ProjectExportError(
|
||||
f"Photo {photo.id} references unknown DailyReport {photo.daily_report_id}"
|
||||
)
|
||||
try:
|
||||
from app.services.daily_report_photo_service import (
|
||||
photo_file_bytes,
|
||||
)
|
||||
file_bytes = photo_file_bytes(photo)
|
||||
except Exception as exc:
|
||||
raise ProjectExportError(
|
||||
f"Photo file missing for daily_report_photo:{photo.id} "
|
||||
f"({photo.storage_key})"
|
||||
) from exc
|
||||
archive_path = f"photos/{photo_export_id.replace(':', '_')}/{photo.id:08d}.bin"
|
||||
photo_blobs[archive_path] = file_bytes
|
||||
daily_report_photo_records.append({
|
||||
"export_id": f"daily_report_photo:{photo.id:08d}",
|
||||
"source_id": photo.id,
|
||||
"daily_report_export_id": photo_export_id,
|
||||
"original_name": photo.original_name,
|
||||
"content_type": photo.content_type,
|
||||
"file_size": photo.file_size,
|
||||
"content_hash": photo.content_hash,
|
||||
"sort_order": photo.sort_order,
|
||||
"created_at": _iso(photo.created_at),
|
||||
"archive_path": archive_path,
|
||||
})
|
||||
ks2 = [
|
||||
record(
|
||||
"ks2",
|
||||
@@ -710,6 +761,7 @@ def serialize_project(graph: dict[str, Any]) -> dict[str, list | dict]:
|
||||
"agreement_items": len(agreement_items),
|
||||
"daily_reports": len(daily_reports),
|
||||
"daily_report_items": len(daily_report_items),
|
||||
"daily_report_photos": len(daily_report_photo_records),
|
||||
"ks2": len(ks2),
|
||||
"ks2_items": len(ks2_items),
|
||||
"ks2_item_sources": len(ks2_item_sources),
|
||||
@@ -740,6 +792,7 @@ def serialize_project(graph: dict[str, Any]) -> dict[str, list | dict]:
|
||||
"agreement_items.json": agreement_items,
|
||||
"daily_reports.json": daily_reports,
|
||||
"daily_report_items.json": daily_report_items,
|
||||
"daily_report_photos.json": daily_report_photo_records,
|
||||
"ks2.json": ks2,
|
||||
"ks2_items.json": ks2_items,
|
||||
"ks2_item_sources.json": ks2_item_sources,
|
||||
@@ -750,6 +803,7 @@ def serialize_project(graph: dict[str, Any]) -> dict[str, list | dict]:
|
||||
"work_journal_entries.json": work_journal_entries,
|
||||
"document_artifacts.json": document_artifacts_records,
|
||||
}
|
||||
artifact_blobs.update(photo_blobs)
|
||||
return {**files, "__artifact_blobs__": artifact_blobs}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user