Update project 11 FRONT MVP phase 2 complete

This commit is contained in:
2026-03-06 16:22:41 +00:00
parent d19660b50c
commit 08c5a8387f
6 changed files with 76 additions and 29 deletions

View File

@@ -1,22 +1,20 @@
"""
Standalone worker: слушает очередь RabbitMQ 'ticket_events',
генерирует PDF-билет через reportlab, загружает в MinIO,
генерирует PDF-билет через core.pdf_generator, загружает в MinIO,
сохраняет pdf_url в PostgreSQL.
"""
import asyncio
import io
import json
import logging
import os
from typing import Any
import aio_pika
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from core.minio import ensure_bucket_exists, upload_pdf
from core.pdf_generator import generate_qr_ticket
from database.models import Ticket
logging.basicConfig(
@@ -33,27 +31,6 @@ DATABASE_URL: str = os.getenv(
QUEUE_NAME: str = "ticket_events"
def _build_pdf(ticket_id: int, seat_id: int, user_id: int) -> bytes:
"""Генерирует PDF-билет в памяти и возвращает байты."""
buffer = io.BytesIO()
pdf = canvas.Canvas(buffer, pagesize=A4)
width, height = A4
pdf.setFont("Helvetica-Bold", 24)
pdf.drawCentredString(width / 2, height - 80, "TICKET")
pdf.setFont("Helvetica", 14)
pdf.drawCentredString(width / 2, height - 130, f"Ticket ID: {ticket_id}")
pdf.drawCentredString(width / 2, height - 160, f"Seat ID: {seat_id}")
pdf.drawCentredString(width / 2, height - 190, f"User ID: {user_id}")
pdf.setFont("Helvetica-Oblique", 10)
pdf.drawCentredString(width / 2, 40, "Thank you for your purchase!")
pdf.save()
return buffer.getvalue()
async def _handle_ticket_paid(
payload: dict[str, Any],
db_session: AsyncSession,
@@ -75,10 +52,10 @@ async def _handle_ticket_paid(
return
log.info("Generating PDF for ticket %s", ticket_id)
pdf_bytes = _build_pdf(
pdf_bytes = generate_qr_ticket(
ticket_id=ticket.id,
seat_id=ticket.seat_id,
user_id=ticket.user_id or 0,
seat_id=ticket.seat_id,
)
object_name = f"tickets/ticket_{ticket_id}.pdf"