[34288f42] Keine Zusammenfassung angegeben.

Keine Zusammenfassung angegeben.
This commit is contained in:
2026-04-14 14:09:58 +00:00
parent 0cca30a956
commit 1a3568f69e
14 changed files with 347 additions and 48 deletions

View File

@@ -1274,7 +1274,10 @@ async def generate_siblings_list(job_id: str, account_type: str, event_type_name
final_storage = os.path.join("/tmp", output_pdf_name)
shutil.copy(output_pdf_path, final_storage)
return FileResponse(path=final_storage, filename=output_pdf_name, media_type="application/pdf")
# Since the frontend has trouble triggering a blob download, return a JSON with a download link
download_url = f"/api/jobs/download-qr/{job_id}/{output_pdf_name}"
return JSONResponse(content={"status": "success", "download_url": download_url, "filename": output_pdf_name})
except HTTPException as he:
raise he
@@ -1284,3 +1287,102 @@ async def generate_siblings_list(job_id: str, account_type: str, event_type_name
finally:
if driver: driver.quit()
@app.post("/api/jobs/{job_id}/siblings-qr-cards")
async def generate_siblings_qr_endpoint(
job_id: str,
account_type: str,
pdf_file: UploadFile = File(...),
db: Session = Depends(get_db)
):
logger.info(f"API Request: Generate siblings QR cards for job {job_id}")
username = os.getenv(f"{account_type.upper()}_USER")
password = os.getenv(f"{account_type.upper()}_PW")
with tempfile.TemporaryDirectory() as temp_dir:
input_pdf_path = os.path.join(temp_dir, "input.pdf")
with open(input_pdf_path, "wb") as buffer:
shutil.copyfileobj(pdf_file.file, buffer)
driver = setup_driver(download_path=temp_dir)
try:
if not login(driver, username, password):
raise HTTPException(status_code=401, detail="Login failed.")
job_url = f"https://app.fotograf.de/config_jobs_settings/index/{job_id}"
driver.get(job_url)
wait = WebDriverWait(driver, 30)
personen_tab = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "[data-qa-id='link:photo-jobs-tabs-names_list']")))
driver.execute_script("arguments[0].click();", personen_tab)
export_btn = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, SELECTORS["export_dropdown"])))
driver.execute_script("arguments[0].scrollIntoView(true);", export_btn)
time.sleep(1)
driver.execute_script("arguments[0].click();", export_btn)
time.sleep(2)
try:
csv_btn = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, SELECTORS["export_csv_link"])))
driver.execute_script("arguments[0].click();", csv_btn)
except TimeoutException:
raise HTTPException(status_code=500, detail="CSV Export Button nicht gefunden.")
timeout = 45
start_time = time.time()
csv_file = None
while time.time() - start_time < timeout:
files = os.listdir(temp_dir)
csv_files = [f for f in files if f.endswith('.csv')]
if csv_files:
csv_file = os.path.join(temp_dir, csv_files[0])
break
time.sleep(1)
if not csv_file:
raise HTTPException(status_code=500, detail="CSV Download fehlgeschlagen.")
output_pdf_name = f"Geschwister_QR_{job_id}.pdf"
output_pdf_path = os.path.join(temp_dir, output_pdf_name)
from siblings_logic import get_sibling_families_from_csv
# Fetch Calendly events to exclude those who already have a meeting
api_token = os.getenv("CALENDLY_TOKEN")
from qr_generator import get_calendly_events_raw
try:
calendly_events = get_calendly_events_raw(api_token, event_type_name=None)
except:
calendly_events = []
families = get_sibling_families_from_csv(csv_file, calendly_events=calendly_events)
if not families:
raise HTTPException(status_code=404, detail="Keine Geschwisterkinder für QR-Karten gefunden.")
from qr_generator import generate_siblings_qr_overlay
generate_siblings_qr_overlay(input_pdf_path, output_pdf_path, families)
final_storage = os.path.join("/tmp", output_pdf_name)
shutil.copy(output_pdf_path, final_storage)
# Since the frontend has trouble triggering a blob download, return a JSON with a download link
download_url = f"/api/jobs/download-qr/{job_id}/{output_pdf_name}"
return JSONResponse(content={"status": "success", "download_url": download_url, "filename": output_pdf_name})
except HTTPException as he:
raise he
except Exception as e:
logger.exception("Error generating siblings QR cards")
raise HTTPException(status_code=500, detail=str(e))
finally:
if driver: driver.quit()
@app.get("/api/jobs/download-qr/{job_id}/{filename}")
async def download_generated_qr(job_id: str, filename: str):
file_path = os.path.join("/tmp", filename)
if os.path.exists(file_path):
return FileResponse(path=file_path, filename=filename, media_type="application/pdf")
raise HTTPException(status_code=404, detail="File not found")