|
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() |
|
|
|
|
|
app.mount("/images", StaticFiles(directory="images"), name="images") |
|
|
|
|
|
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) |
|
|