design final
This commit is contained in:
parent
da5e765d49
commit
a0d2188f6f
1400 changed files with 1748 additions and 207 deletions
149
mailer.py
149
mailer.py
|
|
@ -4,93 +4,90 @@ from io import BytesIO
|
|||
from nio import AsyncClient, UploadResponse, RoomSendResponse
|
||||
|
||||
|
||||
async def send_order(pdf_path: str, analysis: dict, room_id: str, comment: str = ""):
|
||||
async def send_order(pdf_path: str, analysis: dict, room_id: str, name: str, comment: str = ""):
|
||||
"""
|
||||
Sends a print order summary + PDF to the specified Matrix room.
|
||||
"""
|
||||
matrix_user = os.environ.get("MATRIX_USER")
|
||||
matrix_pass = os.environ.get("MATRIX_PASS")
|
||||
homeserver = os.environ.get("MATRIX_HOMESERVER", "http://localhost:8008")
|
||||
homeserver = os.environ.get("MATRIX_HOMESERVER", "http://einszwovier.local:8008")
|
||||
|
||||
if not matrix_user or not matrix_pass:
|
||||
raise RuntimeError("Missing MATRIX_USER or MATRIX_PASS in environment")
|
||||
|
||||
client = AsyncClient(homeserver, matrix_user)
|
||||
login_resp = await client.login(matrix_pass)
|
||||
if getattr(login_resp, "access_token", None) is None:
|
||||
await client.close()
|
||||
raise RuntimeError(f"Failed to login to Matrix: {login_resp}")
|
||||
|
||||
# Build summary text
|
||||
summary_lines = [
|
||||
f"File: {analysis['filename']}",
|
||||
f"Grand total: {analysis['grand_total']} €",
|
||||
f" - Black/White: {analysis['total_area_black']:.2f} m² = {analysis['total_cost_black']} €",
|
||||
f" - Color: {analysis['total_area_color']:.2f} m² = {analysis['total_cost_color']} €",
|
||||
"",
|
||||
"Page details:",
|
||||
]
|
||||
for page in analysis["pages"]:
|
||||
summary_lines.append(
|
||||
f"Page {page['page']}: {page['width_m']*1000:.0f}×{page['height_m']*1000:.0f} mm, "
|
||||
f"{'Color' if page['is_color'] else 'B/W'}, Cost {page['cost']} €"
|
||||
try:
|
||||
login_resp = await client.login(matrix_pass)
|
||||
if getattr(login_resp, "access_token", None) is None:
|
||||
raise RuntimeError(f"Failed to login to Matrix: {login_resp}")
|
||||
|
||||
# Build German summary text
|
||||
summary_lines = [
|
||||
f"📄 Datei: {analysis['filename']}",
|
||||
f"👤 Name: {name}",
|
||||
"",
|
||||
f"💰 Gesamtkosten: {analysis['grand_total']:.2f} €",
|
||||
f" - S/W: {analysis['total_area_black']:.2f} m² = {analysis['total_cost_black']:.2f} €",
|
||||
f" - Farbe: {analysis['total_area_color']:.2f} m² = {analysis['total_cost_color']:.2f} €",
|
||||
"",
|
||||
"📝 Seitenübersicht:"
|
||||
]
|
||||
|
||||
for page in analysis["pages"]:
|
||||
seitenart = "Farbe" if page["is_color"] else "S/W"
|
||||
summary_lines.append(
|
||||
f"Seite {page['page']}: {page['width_m']*1000:.0f}×{page['height_m']*1000:.0f} mm, "
|
||||
f"{seitenart}, Kosten {page['cost']:.2f} €"
|
||||
)
|
||||
|
||||
if comment.strip():
|
||||
summary_lines.append("\n💬 Nutzerkommentar:\n" + comment.strip())
|
||||
|
||||
summary_text = "\n".join(summary_lines)
|
||||
|
||||
# Send summary message
|
||||
text_resp = await client.room_send(
|
||||
room_id=room_id,
|
||||
message_type="m.room.message",
|
||||
content={"msgtype": "m.text", "body": summary_text},
|
||||
)
|
||||
if comment.strip():
|
||||
summary_lines.append("\nUser comment:\n" + comment.strip())
|
||||
summary_text = "\n".join(summary_lines)
|
||||
if not (isinstance(text_resp, RoomSendResponse) and text_resp.event_id):
|
||||
raise RuntimeError(f"Failed to send order summary: {text_resp}")
|
||||
|
||||
# Send text summary
|
||||
text_resp = await client.room_send(
|
||||
room_id=room_id,
|
||||
message_type="m.room.message",
|
||||
content={
|
||||
"msgtype": "m.text",
|
||||
"body": f"New print order submitted:\n\n{summary_text}",
|
||||
},
|
||||
)
|
||||
if not (isinstance(text_resp, RoomSendResponse) and text_resp.event_id):
|
||||
# Upload PDF
|
||||
with open(pdf_path, "rb") as f:
|
||||
pdf_bytes = f.read()
|
||||
|
||||
upload_resp, upload_err = await client.upload(
|
||||
data_provider=BytesIO(pdf_bytes),
|
||||
content_type="application/pdf",
|
||||
filename=os.path.basename(pdf_path),
|
||||
filesize=len(pdf_bytes),
|
||||
)
|
||||
|
||||
if upload_err or not (isinstance(upload_resp, UploadResponse) and upload_resp.content_uri):
|
||||
raise RuntimeError(f"Failed to upload PDF: {upload_err or upload_resp}")
|
||||
|
||||
# Send PDF as file message
|
||||
file_resp = await client.room_send(
|
||||
room_id=room_id,
|
||||
message_type="m.room.message",
|
||||
content={
|
||||
"msgtype": "m.file",
|
||||
"body": os.path.basename(pdf_path),
|
||||
"url": upload_resp.content_uri,
|
||||
},
|
||||
)
|
||||
if not (isinstance(file_resp, RoomSendResponse) and file_resp.event_id):
|
||||
raise RuntimeError(f"Failed to send PDF message: {file_resp}")
|
||||
|
||||
print(f"✅ Text summary and PDF sent to room {room_id}")
|
||||
|
||||
finally:
|
||||
await client.logout()
|
||||
await client.close()
|
||||
raise RuntimeError(f"Failed to send order summary: {text_resp}")
|
||||
|
||||
# Upload PDF
|
||||
with open(pdf_path, "rb") as f:
|
||||
pdf_bytes = f.read()
|
||||
|
||||
upload_resp, upload_err = await client.upload(
|
||||
data_provider=BytesIO(pdf_bytes),
|
||||
content_type="application/pdf",
|
||||
filename=os.path.basename(pdf_path),
|
||||
filesize=len(pdf_bytes),
|
||||
)
|
||||
|
||||
if upload_err:
|
||||
await client.logout()
|
||||
await client.close()
|
||||
raise RuntimeError(f"Failed to upload PDF: {upload_err}")
|
||||
|
||||
if not (isinstance(upload_resp, UploadResponse) and upload_resp.content_uri):
|
||||
await client.logout()
|
||||
await client.close()
|
||||
raise RuntimeError(f"Failed to upload PDF: {upload_resp}")
|
||||
|
||||
# Send PDF as separate file message
|
||||
file_resp = await client.room_send(
|
||||
room_id=room_id,
|
||||
message_type="m.room.message",
|
||||
content={
|
||||
"msgtype": "m.file",
|
||||
"body": os.path.basename(pdf_path),
|
||||
"url": upload_resp.content_uri,
|
||||
},
|
||||
)
|
||||
|
||||
if not (isinstance(file_resp, RoomSendResponse) and file_resp.event_id):
|
||||
await client.logout()
|
||||
await client.close()
|
||||
raise RuntimeError(f"Failed to send PDF message: {file_resp}")
|
||||
|
||||
await client.logout()
|
||||
await client.close()
|
||||
print(f"✅ Text summary and PDF sent to room {room_id}")
|
||||
|
||||
|
||||
def send_order_sync(pdf_path: str, analysis: dict, room_id: str, comment: str = ""):
|
||||
asyncio.run(send_order(pdf_path, analysis, room_id, comment))
|
||||
def send_order_sync(pdf_path: str, analysis: dict, room_id: str, name: str, comment: str = ""):
|
||||
asyncio.run(send_order(pdf_path, analysis, room_id, name, comment))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue