feat: calculate 15% limit for book(s), blocking submission if over the limit

This commit is contained in:
2025-11-19 15:12:39 +01:00
parent 51e3e2a39c
commit a2cca9f977
4 changed files with 298 additions and 1 deletions

View File

@@ -4,8 +4,9 @@ import smtplib
from email.mime.text import MIMEText
from xml.etree.ElementTree import Element, SubElement, tostring
from bibapi import catalogue
from fastapi import FastAPI, Form, HTTPException, Request, status
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
@@ -15,6 +16,9 @@ templates = Jinja2Templates(directory="app/templates")
# Serve static files (CSS, images)
app.mount("/static", StaticFiles(directory="app/static"), name="static")
# Initialize catalogue for signature validation
cat = catalogue.Catalogue()
# add somewhere near the top-level constants
EMAIL_REGEX = re.compile(r"^[^@\s]+@[^@\s]+\.[^@\s]+$")
@@ -44,6 +48,39 @@ async def elsa_form(request: Request):
return templates.TemplateResponse("elsa_mono_form.html", {"request": request})
@app.get("/api/validate-signature")
async def validate_signature(signature: str):
"""Validate a book signature and return total pages"""
try:
book_result = cat.get_book_with_data(signature)
if book_result and hasattr(book_result, "pages") and book_result.pages:
# Try to extract numeric page count
pages_str = str(book_result.pages)
# Extract first number from pages string (e.g., "245 S." -> 245)
match = re.search(r"(\d+)", pages_str)
if match:
total_pages = int(match.group(1))
return JSONResponse(
{"valid": True, "total_pages": total_pages, "signature": signature}
)
return JSONResponse(
{
"valid": False,
"error": "Signatur nicht gefunden oder keine Seitenzahl verfügbar",
"signature": signature,
}
)
except Exception as e:
return JSONResponse(
{
"valid": False,
"error": f"Fehler bei der Validierung: {str(e)}",
"signature": signature,
}
)
@app.post("/submit")
async def handle_form(
request: Request,