Spaces:
Runtime error
Runtime error
Greg
Claude Opus 4.7 (1M context)
Preparar despliegue en fly.io: monolito dockerizado + seeder demo
6aa6081 Download backend/app/utils/csv_import.py from Talosware/talos-access: direct link, hf CLI and curl.
- Browser
- Download file 1.91 kB
-
https://huggingface.co/spaces/Talosware/talos-access/resolve/main/backend/app/utils/csv_import.py
- Command line
-
hf download hf://spaces/Talosware/talos-access/backend/app/utils/csv_import.py
-
curl -L -o csv_import.py https://huggingface.co/spaces/Talosware/talos-access/resolve/main/backend/app/utils/csv_import.py
1.91 kB
| import csv | |
| import io | |
| from fastapi import HTTPException, status | |
| from typing import List, Tuple, Callable, Any | |
| def generate_csv_template(headers: List[str]) -> str: | |
| output = io.StringIO() | |
| writer = csv.writer(output) | |
| writer.writerow(headers) | |
| return output.getvalue() | |
| def parse_csv_upload(content: str, required_columns: List[str]) -> List[dict]: | |
| reader = csv.DictReader(io.StringIO(content)) | |
| if not reader.fieldnames: | |
| raise HTTPException(status_code=400, detail="CSV vacío o sin encabezados") | |
| missing = [c for c in required_columns if c not in reader.fieldnames] | |
| if missing: | |
| raise HTTPException( | |
| status_code=400, | |
| detail=f"Columnas requeridas faltantes: {', '.join(missing)}. Columnas del archivo: {', '.join(reader.fieldnames)}", | |
| ) | |
| rows = [] | |
| for i, row in enumerate(reader, start=2): | |
| cleaned = {k.strip(): v.strip() if v else "" for k, v in row.items()} | |
| rows.append(cleaned) | |
| return rows | |
| def process_csv_rows( | |
| rows: List[dict], | |
| row_processor: Callable[[dict, int], Tuple[bool, str]], | |
| ) -> dict: | |
| created = 0 | |
| updated = 0 | |
| errors = [] | |
| for i, row in enumerate(rows, start=2): | |
| try: | |
| result = row_processor(row, i) | |
| if isinstance(result, str): | |
| if result == "created": | |
| created += 1 | |
| elif result == "updated": | |
| updated += 1 | |
| else: | |
| errors.append({"row": i, "error": result}) | |
| elif isinstance(result, tuple): | |
| ok, msg = result | |
| if ok: | |
| created += 1 | |
| else: | |
| errors.append({"row": i, "error": msg}) | |
| except Exception as e: | |
| errors.append({"row": i, "error": str(e)}) | |
| return {"created": created, "updated": updated, "errors": errors} | |