Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ pytest~=7.4.4
gitpython
PyYAML~=6.0.1
setuptools~=68.2.0
fastapi
uvicorn
python-multipart

63 changes: 63 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from fastapi import FastAPI, UploadFile, File, Form, Request
from fastapi.responses import FileResponse, HTMLResponse
from fastapi.templating import Jinja2Templates
import shutil
import os
from template_builder import TemplateBuilder

app = FastAPI()
templates = Jinja2Templates(directory="src/templates")


@app.get("/", response_class=HTMLResponse)
async def read_form(request: Request):
# Display the HTML upload form page
return templates.TemplateResponse(
"upload.html", {"request": request}
)


@app.post("/generate-template/")
async def generate_template(
template_parts_list_file: UploadFile = File(...),
project_name: str = Form(...)
):

working_dir = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# Temporarily save the uploaded CSV file
input_filename = os.path.join(
working_dir, f"temp_{template_parts_list_file.filename}"
)
with open(input_filename, "wb") as buffer:
shutil.copyfileobj(
template_parts_list_file.file, buffer
)

# Define the output JSON filename
output_filename = os.path.join(
working_dir, f"{project_name}.json"
)

try:
original_cwd = os.getcwd()
os.chdir(working_dir)
# Generate the JSON with TemplateBuilder
TemplateBuilder(input_filename, output_filename)

# Return the generated JSON file
return FileResponse(
path=output_filename,
filename=f"{project_name}.json",
media_type="application/json"
)

except Exception as e:
# Return an error message if something fails
return {"error": str(e)}

finally:
os.chdir(original_cwd)
# Clean up the temporary CSV file
if os.path.exists(input_filename):
os.remove(input_filename)
87 changes: 87 additions & 0 deletions src/templates/upload.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Générateur de Template</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f7f7f7;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}

.container {
background-color: white;
padding: 30px 40px;
border-radius: 10px;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
text-align: center;
}

h1 {
margin-bottom: 20px;
color: #333;
}

label {
display: block;
margin: 15px 0 5px;
font-weight: bold;
}

input[type="text"],
input[type="file"] {
padding: 10px;
width: 100%;
max-width: 300px;
margin: auto;
border: 1px solid #ccc;
border-radius: 5px;
}

button {
margin-top: 20px;
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}

button:hover {
background-color: #45a049;
}

a button {
background-color: #007BFF;
margin-left: 10px;
}

a button:hover {
background-color: #0069d9;
}
</style>
</head>
<body>
<div class="container">
<h1>Générateur de Template</h1>
<form action="/generate-template/" method="post" enctype="multipart/form-data">
<label for="project_name">Nom du projet :</label>
<input type="text" id="project_name" name="project_name" required>

<label for="template_parts_list_file">Fichier CSV :</label>
<input type="file" name="template_parts_list_file" accept=".csv" required>

<button type="submit">Générer le Template</button>
</form>

<a href="https://int.cle.cnrs.fr/login.php" target="_blank">
<button type="button">Visualiser le Template</button>
</a>
</div>
</body>
</html>