From cc6461ced26ab3d6d1c007402d490eb2cd2abcf9 Mon Sep 17 00:00:00 2001 From: Joshua Laymon Date: Fri, 22 Aug 2025 13:35:58 +0000 Subject: [PATCH] Update web/core/views.py --- web/core/views.py | 51 ++++++++++++++++------------------------------- 1 file changed, 17 insertions(+), 34 deletions(-) diff --git a/web/core/views.py b/web/core/views.py index f14efcb..83effff 100644 --- a/web/core/views.py +++ b/web/core/views.py @@ -340,11 +340,10 @@ def import_wizard(request): try: raw = form.cleaned_data["file"].read() - # --- Decode once (BOM-safe) --- import io, csv as _csv text = raw.decode("utf-8-sig", errors="replace") - # --- Try to sniff a dialect; fall back to Excel CSV --- + # --- Try to sniff dialect --- try: first_line = text.splitlines()[0] if text else "" dialect = _csv.Sniffer().sniff(first_line) if first_line else _csv.excel @@ -370,14 +369,13 @@ def import_wizard(request): first = rows[0] norm_first = [_clean_header(c) for c in first] - header_ok = (norm_first == expected_norm) - # If not header but column count matches, inject expected header. + # If no header but same column count, inject expected if not header_ok and len(first) == len(EXPECTED): rows.insert(0, EXPECTED) elif not header_ok and len(first) != len(EXPECTED): - # Retry with common alternate delimiters + # Retry with alternate delimiters for delim in (";", "\t"): rows2 = list(_csv.reader(io.StringIO(text), delimiter=delim)) if rows2 and len(rows2[0]) == len(EXPECTED): @@ -389,44 +387,28 @@ def import_wizard(request): rows.insert(0, EXPECTED) break - # Re-encode sanitized CSV for the existing importer + # Re-encode sanitized CSV for importer out = io.StringIO() w = _csv.writer(out) for r in rows: w.writerow(r) fixed_raw = out.getvalue().encode("utf-8") - # Keep utils in sync for import_csv_bytes variants + # Keep utils in sync from . import utils as core_utils core_utils.EXPECTED_HEADERS = EXPECTED - # Run your robust importer -# ... inside import_wizard, after: -report = import_csv_bytes(fixed_raw, dry_run=form.cleaned_data["dry_run"]) or {} + # Run robust importer + report = import_csv_bytes(fixed_raw, dry_run=form.cleaned_data["dry_run"]) or {} -# Ensure we have a canonical column list (matching your CSV order) -expected = [ - "Subject", "Illustration", "Application", "Scripture", "Source", - "Talk Title", "Talk Number", "Code", "Date", "Date Edited", -] -report.setdefault("columns", expected) - -# If the importer returned preview rows as dicts, convert them to list-of-lists -preview = report.get("preview") or [] -if preview and isinstance(preview[0], dict): - cols = report["columns"] - report["preview"] = [[(row.get(c) or "") for c in cols] for row in preview] - - report["header_ok"] = header_ok - - # Normalize preview for the template: list of rows + columns list + # Normalize preview report.setdefault("columns", EXPECTED) - if report.get("preview"): - if isinstance(report["preview"][0], dict): - cols = report["columns"] - report["preview"] = [ - [row.get(c, "") for c in cols] for row in report["preview"] - ] + preview = report.get("preview") or [] + if preview and isinstance(preview[0], dict): + cols = report["columns"] + report["preview"] = [[row.get(c, "") for c in cols] for row in preview] + + report["header_ok"] = header_ok if not header_ok: messages.warning( @@ -439,13 +421,14 @@ if preview and isinstance(preview[0], dict): "import_result.html", {"report": report, "dry_run": form.cleaned_data["dry_run"]}, ) + except Exception as e: messages.error(request, f"Import failed: {e}") - # If form invalid or error: fall through to show the form again + # Invalid form or exception → show form again return render(request, "import_wizard.html", {"form": form}) - # GET + # GET → show empty form form = ImportForm() return render(request, "import_wizard.html", {"form": form})