File size: 2,276 Bytes
b568de0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
from fastapi import FastAPI, Form, Request
from fastapi.responses import RedirectResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
import os
import random
import requests
GOOGLE_SHEETS_URL = 'https://script.google.com/macros/s/AKfycbxsrkX8g7HkcmrX_t6YiHMcj_e51ZAxLaLC7OlcAjFP1LI1XoJKAOl0xmAKdl_05IiY/exec'
app = FastAPI()
# Montar la carpeta de imágenes como archivos estáticos
app.mount("/images", StaticFiles(directory="images"), name="images")
# Configurar Jinja2 templates
templates = Jinja2Templates(directory="templates")
@app.get("/")
async def index(request: Request):
images_folder = 'images'
images = os.listdir(images_folder) if os.path.exists(images_folder) else []
image_name = random.choice(images) if images else ''
return templates.TemplateResponse("index.html", {"request": request, "image_name": image_name})
@app.post("/submit")
async def submit(
dni: str = Form(...),
primer_apellido: str = Form(...),
segundo_apellido: str = Form(...),
pre_nombres: str = Form(...),
fecha_nacimiento: str = Form(...),
ubigeo: str = Form(...),
sexo: str = Form(...),
estado_civil: str = Form(...),
fecha_inscripcion: str = Form(...),
fecha_emision: str = Form(...),
fecha_caducidad: str = Form(...),
image_name: str = Form(...),
tiempo: str = Form(...)
):
data = {
'dni': dni,
'primer_apellido': primer_apellido,
'segundo_apellido': segundo_apellido,
'pre_nombres': pre_nombres,
'fecha_nacimiento': fecha_nacimiento,
'ubigeo': ubigeo,
'sexo': sexo,
'estado_civil': estado_civil,
'fecha_inscripcion': fecha_inscripcion,
'fecha_emision': fecha_emision,
'fecha_caducidad': fecha_caducidad,
'image_name': image_name,
'tiempo': tiempo
}
try:
response = requests.post(GOOGLE_SHEETS_URL, json=data)
if response.status_code == 200:
print("Datos enviados a Google Sheets correctamente.")
else:
print("Error al enviar a Google Sheets:", response.text)
except Exception as e:
print("Excepción al enviar a Google Sheets:", e)
return RedirectResponse(url="/", status_code=303)
|