e62ece1c06
- FastAPI app with CORS, lifespan handlers - Pydantic Settings config (DB, Redis, CORS, SMTP, JWT, Rentman) - SQLAlchemy async engine + session (DeclarativeBase) - 6 DB models: EquipmentCache, RentalRequest, RentalRequestItem, Contact, AdminUser, SyncLog - Pydantic schemas: EquipmentItem, EquipmentDetail, PaginatedEquipment, ContactCreate, ContactResponse - Redis cache helper: set/get/delete_pattern, rate limiting, equipment key builders - Equipment router: list (search/category/sort/pagination), detail, categories – all cached - Contact router: POST with Pydantic validation + rate limiting (5/min) - Health router: GET /api/health with DB + Redis status - 28 pytest tests (all pass, 90% coverage) - Dockerfile, requirements.txt, pytest.ini, test_report.md
40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
"""Contact form endpoint with rate limiting."""
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Request, status
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.cache import check_rate_limit
|
|
from app.database import get_db
|
|
from app.models.contact import Contact
|
|
from app.schemas.contact import ContactCreate, ContactResponse
|
|
|
|
router = APIRouter(prefix="/contact", tags=["contact"])
|
|
|
|
|
|
@router.post("", response_model=ContactResponse, status_code=status.HTTP_201_CREATED)
|
|
async def create_contact(
|
|
payload: ContactCreate,
|
|
request: Request,
|
|
db: AsyncSession = Depends(get_db),
|
|
) -> ContactResponse:
|
|
"""Submit a contact form message with rate limiting."""
|
|
client_ip = request.client.host if request.client else "unknown"
|
|
allowed = await check_rate_limit(f"contact:{client_ip}", max_requests=5, window=60)
|
|
if not allowed:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
|
|
detail="Rate limit exceeded. Please try again later.",
|
|
)
|
|
|
|
contact = Contact(
|
|
name=payload.name,
|
|
email=str(payload.email),
|
|
phone=payload.phone,
|
|
message=payload.message,
|
|
privacy_consent=payload.privacy_consent,
|
|
)
|
|
db.add(contact)
|
|
await db.commit()
|
|
|
|
return ContactResponse(success=True, message="Kontakt-Anfrage erfolgreich gesendet.")
|