Spaces:
Sleeping
Sleeping
Commit ·
089f46d
1
Parent(s): e3b7e75
Remove PDF existence requirement from API handlers
Browse files- backend/api.py +6 -82
backend/api.py
CHANGED
|
@@ -87,14 +87,12 @@ async def put_pdf(pdf_id: str, file: UploadFile = File(...), pdf_name: str = For
|
|
| 87 |
@app.post("/api/send-config")
|
| 88 |
async def send_config(request: Request):
|
| 89 |
"""
|
| 90 |
-
Store config
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
- pdf_id: str
|
| 94 |
-
- template_id: str
|
| 95 |
-
- config: dict
|
| 96 |
"""
|
| 97 |
payload = await request.json()
|
|
|
|
| 98 |
pdf_id = (payload.get("pdf_id") or "").strip()
|
| 99 |
template_id = (payload.get("template_id") or "").strip()
|
| 100 |
config = payload.get("config")
|
|
@@ -106,84 +104,15 @@ async def send_config(request: Request):
|
|
| 106 |
if not isinstance(config, dict):
|
| 107 |
raise HTTPException(status_code=400, detail="Missing config object")
|
| 108 |
|
| 109 |
-
# PDF must exist in API uploads (worker uploads it first)
|
| 110 |
-
# Always store config, even if PDF is missing in this container
|
| 111 |
CONFIGS_DIR.mkdir(parents=True, exist_ok=True)
|
| 112 |
out_path = CONFIGS_DIR / f"{pdf_id}__{template_id}.json"
|
| 113 |
-
out_path.write_text(json.dumps({"pdf_id": pdf_id, "template_id": template_id, "config": config}, indent=2), encoding="utf-8")
|
| 114 |
-
|
| 115 |
-
pdf_path = UPLOADS_DIR / f"{pdf_id}.pdf"
|
| 116 |
-
pdf_exists = pdf_path.exists()
|
| 117 |
-
name_path = UPLOADS_DIR / f"{pdf_id}.name.txt"
|
| 118 |
-
pdf_name = name_path.read_text(encoding="utf-8").strip() if name_path.exists() else f"{pdf_id}.pdf"
|
| 119 |
-
|
| 120 |
-
# 1) STORE config on disk (source of truth)
|
| 121 |
-
CONFIGS_DIR.mkdir(parents=True, exist_ok=True)
|
| 122 |
-
out_path = CONFIGS_DIR / f"trainer_config_{pdf_id}_{template_id}.json"
|
| 123 |
out_path.write_text(
|
| 124 |
json.dumps({"pdf_id": pdf_id, "template_id": template_id, "config": config}, indent=2),
|
| 125 |
encoding="utf-8",
|
| 126 |
)
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
email_err = ""
|
| 131 |
-
try:
|
| 132 |
-
pipeline_to = _get_env_required("PDF_PIPELINE_PIPELINE_NOTIFY_TO")
|
| 133 |
-
notify_from = _get_env_required("PDF_PIPELINE_NOTIFY_FROM")
|
| 134 |
-
|
| 135 |
-
# creds/token can be path OR raw json text; we only support path in HF container
|
| 136 |
-
creds = (os.environ.get("PDF_PIPELINE_GMAIL_CREDENTIALS_JSON") or "").strip()
|
| 137 |
-
token = (os.environ.get("PDF_PIPELINE_GMAIL_TOKEN_JSON") or "").strip()
|
| 138 |
-
|
| 139 |
-
if not creds or not token or not Path(creds).exists() or not Path(token).exists():
|
| 140 |
-
raise RuntimeError("Gmail OAuth files not present in container (expected file paths)")
|
| 141 |
-
|
| 142 |
-
subject = f"PDF_TRAINER_CONFIG_SUBMITTED | template_id={template_id}"
|
| 143 |
-
trainer_base_url = (os.environ.get("PDF_TRAINER_BASE_URL") or "").strip()
|
| 144 |
-
trainer_link = f"{trainer_base_url.rstrip('/')}/?pdf_id={pdf_id}" if trainer_base_url else f"(trainer url not set) pdf_id={pdf_id}"
|
| 145 |
-
|
| 146 |
-
body = (
|
| 147 |
-
"Hi,\n\n"
|
| 148 |
-
"A PDF Trainer configuration was submitted.\n\n"
|
| 149 |
-
f"template_id: {template_id}\n"
|
| 150 |
-
f"pdf_id: {pdf_id}\n"
|
| 151 |
-
f"trainer_link: {trainer_link}\n\n"
|
| 152 |
-
"Attachments:\n"
|
| 153 |
-
f"- {out_path.name}\n"
|
| 154 |
-
f"- {pdf_name}\n\n"
|
| 155 |
-
"Thank you,\n"
|
| 156 |
-
"Inserio Automation\n"
|
| 157 |
-
)
|
| 158 |
-
|
| 159 |
-
cfg_bytes = out_path.read_bytes()
|
| 160 |
-
attachments = [
|
| 161 |
-
(out_path.name, cfg_bytes),
|
| 162 |
-
(pdf_name, pdf_path.read_bytes()),
|
| 163 |
-
]
|
| 164 |
-
|
| 165 |
-
gmail = _gmail()
|
| 166 |
-
gmail.send_email(
|
| 167 |
-
to_email=pipeline_to,
|
| 168 |
-
from_email=notify_from,
|
| 169 |
-
subject=subject,
|
| 170 |
-
body_text=body,
|
| 171 |
-
attachments=attachments,
|
| 172 |
-
)
|
| 173 |
-
email_ok = True
|
| 174 |
-
|
| 175 |
-
except Exception as e:
|
| 176 |
-
email_err = str(e)
|
| 177 |
-
|
| 178 |
-
return {
|
| 179 |
-
"ok": True,
|
| 180 |
-
"stored": str(out_path),
|
| 181 |
-
"pdf_present": True,
|
| 182 |
-
"email_ok": email_ok,
|
| 183 |
-
"email_error": email_err,
|
| 184 |
-
}
|
| 185 |
-
|
| 186 |
-
|
| 187 |
@app.post("/api/notify-unknown")
|
| 188 |
async def notify_unknown(payload: Dict[str, Any]):
|
| 189 |
"""
|
|
@@ -211,11 +140,6 @@ async def notify_unknown(payload: Dict[str, Any]):
|
|
| 211 |
rep_to = _get_env_required("PDF_PIPELINE_NOTIFY_TO")
|
| 212 |
notify_from = _get_env_required("PDF_PIPELINE_NOTIFY_FROM")
|
| 213 |
trainer_base_url = (os.environ.get("PDF_TRAINER_BASE_URL") or "http://localhost:5173").strip()
|
| 214 |
-
|
| 215 |
-
pdf_path = UPLOADS_DIR / f"{pdf_id}.pdf"
|
| 216 |
-
if not pdf_path.exists():
|
| 217 |
-
raise HTTPException(status_code=404, detail="PDF not found for pdf_id")
|
| 218 |
-
|
| 219 |
name_path = UPLOADS_DIR / f"{pdf_id}.name.txt"
|
| 220 |
pdf_name = name_path.read_text(encoding="utf-8").strip() if name_path.exists() else f"{pdf_id}.pdf"
|
| 221 |
|
|
|
|
| 87 |
@app.post("/api/send-config")
|
| 88 |
async def send_config(request: Request):
|
| 89 |
"""
|
| 90 |
+
Store config JSON for later pipelines.
|
| 91 |
+
Do NOT require PDF to exist.
|
| 92 |
+
Do NOT send email from this API container (no Gmail creds in HF).
|
|
|
|
|
|
|
|
|
|
| 93 |
"""
|
| 94 |
payload = await request.json()
|
| 95 |
+
|
| 96 |
pdf_id = (payload.get("pdf_id") or "").strip()
|
| 97 |
template_id = (payload.get("template_id") or "").strip()
|
| 98 |
config = payload.get("config")
|
|
|
|
| 104 |
if not isinstance(config, dict):
|
| 105 |
raise HTTPException(status_code=400, detail="Missing config object")
|
| 106 |
|
|
|
|
|
|
|
| 107 |
CONFIGS_DIR.mkdir(parents=True, exist_ok=True)
|
| 108 |
out_path = CONFIGS_DIR / f"{pdf_id}__{template_id}.json"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
out_path.write_text(
|
| 110 |
json.dumps({"pdf_id": pdf_id, "template_id": template_id, "config": config}, indent=2),
|
| 111 |
encoding="utf-8",
|
| 112 |
)
|
| 113 |
|
| 114 |
+
pdf_path = UPLOADS_DIR / f"{pdf_id}.pdf"
|
| 115 |
+
return {"ok": True, "stored": str(out_path), "pdf_exists": pdf_path.exists()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
@app.post("/api/notify-unknown")
|
| 117 |
async def notify_unknown(payload: Dict[str, Any]):
|
| 118 |
"""
|
|
|
|
| 140 |
rep_to = _get_env_required("PDF_PIPELINE_NOTIFY_TO")
|
| 141 |
notify_from = _get_env_required("PDF_PIPELINE_NOTIFY_FROM")
|
| 142 |
trainer_base_url = (os.environ.get("PDF_TRAINER_BASE_URL") or "http://localhost:5173").strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
name_path = UPLOADS_DIR / f"{pdf_id}.name.txt"
|
| 144 |
pdf_name = name_path.read_text(encoding="utf-8").strip() if name_path.exists() else f"{pdf_id}.pdf"
|
| 145 |
|