fix: resolve ruff linting issues and improve code formatting
This commit is contained in:
+17
-13
@@ -6,7 +6,6 @@ import io
|
||||
import boto3
|
||||
import zipfile
|
||||
import tempfile
|
||||
import importlib.util
|
||||
from botocore.exceptions import ClientError
|
||||
from typing import List
|
||||
from fastapi import FastAPI, Request, Form
|
||||
@@ -20,12 +19,14 @@ import logging
|
||||
# Import the functions from gen_op.py
|
||||
from app.gen_op import generer_pdf_exercices_en_memoire
|
||||
|
||||
|
||||
# Custom filter to exclude health check logs
|
||||
class HealthCheckFilter(logging.Filter):
|
||||
def filter(self, record: logging.LogRecord) -> bool:
|
||||
# Exclude health check requests from logs
|
||||
return "GET /health " not in record.getMessage()
|
||||
|
||||
|
||||
# Apply the filter to Uvicorn's access logger
|
||||
logging.getLogger("uvicorn.access").addFilter(HealthCheckFilter())
|
||||
|
||||
@@ -167,6 +168,7 @@ class ExerciseRequest(BaseModel):
|
||||
max_table: int
|
||||
num_exercises: int = 15
|
||||
|
||||
|
||||
class OperationExerciseRequest(BaseModel):
|
||||
num_exercises: int = 20
|
||||
|
||||
@@ -315,20 +317,22 @@ def create_math_exercises_pdf(
|
||||
def create_operation_exercises_pdf(num_exercises: int = 20) -> str:
|
||||
"""Crée un fichier PDF avec des exercices d'opérations et l'upload sur S3"""
|
||||
import datetime
|
||||
|
||||
|
||||
# Add timestamp to filename
|
||||
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
pdf_filename = f"exercices_operations_{num_exercises}_exercices_{timestamp}.pdf"
|
||||
|
||||
|
||||
# Generate PDF in memory using gen-op functions
|
||||
pdf_data = generer_pdf_exercices_en_memoire(num_exercises)
|
||||
|
||||
|
||||
# Upload to S3
|
||||
upload_success = upload_to_s3(pdf_data, S3_BUCKET_NAME, pdf_filename, "application/pdf")
|
||||
|
||||
upload_success = upload_to_s3(
|
||||
pdf_data, S3_BUCKET_NAME, pdf_filename, "application/pdf"
|
||||
)
|
||||
|
||||
if not upload_success:
|
||||
raise Exception("Failed to upload PDF to S3")
|
||||
|
||||
|
||||
return pdf_filename
|
||||
|
||||
|
||||
@@ -479,7 +483,7 @@ async def bulk_download(filenames: str = Form(...)):
|
||||
try:
|
||||
# Parse the JSON string to get the list of filenames
|
||||
filename_list = json.loads(filenames)
|
||||
|
||||
|
||||
# Create a temporary zip file
|
||||
with tempfile.NamedTemporaryFile(suffix=".zip", delete=False) as tmp_zip:
|
||||
with zipfile.ZipFile(tmp_zip.name, "w") as zipf:
|
||||
@@ -487,16 +491,16 @@ async def bulk_download(filenames: str = Form(...)):
|
||||
file_data = download_from_s3(S3_BUCKET_NAME, filename)
|
||||
if file_data:
|
||||
zipf.writestr(filename, file_data)
|
||||
|
||||
|
||||
# Read the zip file
|
||||
with open(tmp_zip.name, "rb") as f:
|
||||
zip_data = f.read()
|
||||
|
||||
|
||||
# Clean up temporary file
|
||||
import os
|
||||
|
||||
|
||||
os.unlink(tmp_zip.name)
|
||||
|
||||
|
||||
# Return streaming response with zip data
|
||||
zip_filename = f"math_exercises_{len(filename_list)}_files.zip"
|
||||
return StreamingResponse(
|
||||
@@ -518,5 +522,5 @@ async def health_check():
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
|
||||
|
||||
uvicorn.run(app, host="0.0.0.0", port=8000)
|
||||
|
||||
Reference in New Issue
Block a user