feat: calculate 15% limit for book(s), blocking submission if over the limit
This commit is contained in:
39
app/main.py
39
app/main.py
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user