Spaces:
Sleeping
Sleeping
Commit ·
b175f72
1
Parent(s): 93ca01e
Fix send-config: store-only, no Gmail dependency
Browse files- backend/api.py +10 -66
backend/api.py
CHANGED
|
@@ -88,84 +88,28 @@ async def put_pdf(pdf_id: str, file: UploadFile = File(...), pdf_name: str = For
|
|
| 88 |
|
| 89 |
|
| 90 |
@app.post("/api/send-config")
|
| 91 |
-
async def send_config(
|
| 92 |
-
|
| 93 |
-
PIPELINE SUBMISSION EMAIL (after rep saves config)
|
| 94 |
-
|
| 95 |
-
REQUIRED payload:
|
| 96 |
-
- pdf_id: str
|
| 97 |
-
- template_id: str
|
| 98 |
-
- config: dict
|
| 99 |
-
|
| 100 |
-
Sends to PIPELINE inbox:
|
| 101 |
-
- PDF_PIPELINE_PIPELINE_NOTIFY_TO
|
| 102 |
|
| 103 |
-
Requirements:
|
| 104 |
-
- Subject includes template_id
|
| 105 |
-
- Body includes pdf_id
|
| 106 |
-
- Attachments: JSON + PDF
|
| 107 |
-
"""
|
| 108 |
pdf_id = (payload.get("pdf_id") or "").strip()
|
| 109 |
template_id = (payload.get("template_id") or "").strip()
|
| 110 |
config = payload.get("config")
|
| 111 |
|
| 112 |
-
if not pdf_id:
|
| 113 |
-
raise HTTPException(status_code=400, detail="Missing pdf_id")
|
| 114 |
-
if not template_id:
|
| 115 |
-
raise HTTPException(status_code=400, detail="Missing template_id")
|
| 116 |
-
if not isinstance(config, dict):
|
| 117 |
-
raise HTTPException(status_code=400, detail="Missing config object")
|
| 118 |
-
|
| 119 |
-
pipeline_to = _get_env_required("PDF_PIPELINE_PIPELINE_NOTIFY_TO")
|
| 120 |
-
notify_from = _get_env_required("PDF_PIPELINE_NOTIFY_FROM")
|
| 121 |
-
trainer_base_url = (os.environ.get("PDF_TRAINER_BASE_URL") or "http://localhost:5173").strip()
|
| 122 |
|
| 123 |
pdf_path = UPLOADS_DIR / f"{pdf_id}.pdf"
|
| 124 |
if not pdf_path.exists():
|
| 125 |
raise HTTPException(status_code=404, detail="PDF not found for pdf_id")
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
subject = f"PDF_TRAINER_CONFIG_SUBMITTED | template_id={template_id}"
|
| 133 |
-
body = (
|
| 134 |
-
"Hi,\n\n"
|
| 135 |
-
"A PDF Trainer configuration was submitted.\n\n"
|
| 136 |
-
f"template_id: {template_id}\n"
|
| 137 |
-
f"pdf_id: {pdf_id}\n"
|
| 138 |
-
f"trainer_link: {trainer_link}\n\n"
|
| 139 |
-
"Attachments:\n"
|
| 140 |
-
f"- trainer_config_{pdf_id}_{template_id}.json\n"
|
| 141 |
-
f"- {pdf_name}\n\n"
|
| 142 |
-
"Thank you,\n"
|
| 143 |
-
"Inserio Automation\n"
|
| 144 |
)
|
| 145 |
|
| 146 |
-
|
| 147 |
-
{"pdf_id": pdf_id, "template_id": template_id, "config": config},
|
| 148 |
-
indent=2,
|
| 149 |
-
).encode("utf-8")
|
| 150 |
-
|
| 151 |
-
attachments = [
|
| 152 |
-
(f"trainer_config_{pdf_id}_{template_id}.json", cfg_bytes),
|
| 153 |
-
(pdf_name, pdf_path.read_bytes()),
|
| 154 |
-
]
|
| 155 |
-
|
| 156 |
-
gmail = _gmail()
|
| 157 |
-
gmail.send_email(
|
| 158 |
-
to_email=pipeline_to,
|
| 159 |
-
from_email=notify_from,
|
| 160 |
-
subject=subject,
|
| 161 |
-
body_text=body,
|
| 162 |
-
attachments=attachments,
|
| 163 |
-
)
|
| 164 |
-
|
| 165 |
-
return {"ok": True}
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
@app.post("/api/notify-unknown")
|
| 169 |
async def notify_unknown(payload: Dict[str, Any]):
|
| 170 |
"""
|
| 171 |
UNKNOWN TEMPLATE NOTIFICATION (rep email)
|
|
|
|
| 88 |
|
| 89 |
|
| 90 |
@app.post("/api/send-config")
|
| 91 |
+
async def send_config(request: Request):
|
| 92 |
+
payload = await request.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
pdf_id = (payload.get("pdf_id") or "").strip()
|
| 95 |
template_id = (payload.get("template_id") or "").strip()
|
| 96 |
config = payload.get("config")
|
| 97 |
|
| 98 |
+
if not pdf_id or not template_id or not isinstance(config, dict):
|
| 99 |
+
raise HTTPException(status_code=400, detail="Missing pdf_id / template_id / config")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
pdf_path = UPLOADS_DIR / f"{pdf_id}.pdf"
|
| 102 |
if not pdf_path.exists():
|
| 103 |
raise HTTPException(status_code=404, detail="PDF not found for pdf_id")
|
| 104 |
|
| 105 |
+
CONFIGS_DIR.mkdir(parents=True, exist_ok=True)
|
| 106 |
+
out_path = CONFIGS_DIR / f"{pdf_id}__{template_id}.json"
|
| 107 |
+
out_path.write_text(
|
| 108 |
+
json.dumps(payload, indent=2),
|
| 109 |
+
encoding="utf-8"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
)
|
| 111 |
|
| 112 |
+
return {"ok": True, "stored": str(out_path)}@app.post("/api/notify-unknown")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
async def notify_unknown(payload: Dict[str, Any]):
|
| 114 |
"""
|
| 115 |
UNKNOWN TEMPLATE NOTIFICATION (rep email)
|