Update web/core/views.py

This commit is contained in:
Joshua Laymon 2025-08-22 03:10:50 +00:00
parent ecb8e6657c
commit 666f6dd82a

View File

@ -336,70 +336,96 @@ def import_wizard(request):
] ]
if request.method == "POST": if request.method == "POST":
form = ImportForm(request.POST, request.FILES) form = ImportForm(request.POST, request.FILES)
if form.is_valid(): if form.is_valid():
try:
raw = form.cleaned_data["file"].read()
import io, csv as _csv
# Decode once (BOM-safe)
text = raw.decode("utf-8-sig", errors="replace")
# Try to sniff a dialect; fall back to Excel-style CSV
try: try:
raw = form.cleaned_data["file"].read() first_line = text.splitlines()[0] if text else ""
dialect = _csv.Sniffer().sniff(first_line) if first_line else _csv.excel
except Exception:
dialect = _csv.excel
# --- super-defensive header peek (never raises) --- rdr = _csv.reader(io.StringIO(text), dialect)
import io, csv as _csv rows = list(rdr)
sio = io.StringIO(raw.decode("utf-8-sig", errors="replace")) if not rows:
raise ValueError("The CSV file appears to be empty.")
header_ok = False # Expected header (DB field order)
try: expected = [
rdr = _csv.reader(sio) "Subject", "Illustration", "Application", "Scripture", "Source",
first_row = next(rdr, []) "Talk Title", "Talk Number", "Code", "Date", "Date Edited",
]
expected_norm = [h.lower() for h in expected]
def _clean(s): # Header cleaner: fixes r:"Talk Title", stray quotes, spaces, case
s = "" if s is None else str(s) def _clean_header(s):
# strip quotes and odd prefixes like r:"Talk Title" s = "" if s is None else str(s)
s = s.strip().strip("'").strip('"') s = s.strip()
if s.lower().startswith("r:"): if s.lower().startswith("r:") or s.lower().startswith("r="):
s = s[2:].lstrip() s = s[2:].lstrip()
return s.strip().lower() if (len(s) >= 2) and (s[0] == s[-1]) and s[0] in ('"', "'"):
s = s[1:-1]
return s.strip().lower()
norm = [_clean(c) for c in first_row] first = rows[0]
norm_first = [_clean_header(c) for c in first]
expected = [h.strip().lower() for h in _EXPECTED_HEADERS] # If first row isnt our header but length matches, inject one
header_ok = (norm == expected) header_ok = (norm_first == expected_norm)
except Exception: if not header_ok and len(first) == len(expected):
header_ok = False rows.insert(0, expected)
finally: elif not header_ok and len(first) != len(expected):
# Rewind so the real importer reads the full file # Try common alternate delimiters if column count is off
sio.seek(0) for delim in (";", "\t"):
rdr2 = _csv.reader(io.StringIO(text), delimiter=delim)
test_rows = list(rdr2)
if test_rows and len(test_rows[0]) == len(expected):
rows = test_rows
first = rows[0]
norm_first = [_clean_header(c) for c in first]
header_ok = (norm_first == expected_norm)
if not header_ok:
rows.insert(0, expected)
break
# Make sure utils knows the expected headers # Re-encode a sanitized CSV for the existing importer
from . import utils as core_utils out = io.StringIO()
if not hasattr(core_utils, "EXPECTED_HEADERS"): w = _csv.writer(out)
core_utils.EXPECTED_HEADERS = _EXPECTED_HEADERS for r in rows:
w.writerow(r)
fixed_raw = out.getvalue().encode("utf-8")
# Hand off to your robust importer # Keep utils in sync for importer variants that read EXPECTED_HEADERS
report = import_csv_bytes( from . import utils as core_utils
raw, core_utils.EXPECTED_HEADERS = expected
dry_run=form.cleaned_data["dry_run"],
)
# Attach header check info # Hand off to the robust importer you already have
report = report or {} report = import_csv_bytes(fixed_raw, dry_run=form.cleaned_data["dry_run"]) or {}
report["header_ok"] = header_ok report["header_ok"] = header_ok
if not header_ok: if not header_ok:
messages.warning( messages.warning(
request,
"The first row does not match the expected header; assuming the file has no header."
)
return render(
request, request,
"import_result.html", "The first row didnt match the expected header; a clean header was injected automatically."
{"report": report, "dry_run": form.cleaned_data["dry_run"]},
) )
except Exception as e:
messages.error(request, f"Import failed: {e}")
else:
form = ImportForm()
# 👇 stays inside the function return render(
return render(request, "import_wizard.html", {"form": form}) request,
"import_result.html",
{"report": report, "dry_run": form.cleaned_data["dry_run"]},
)
except Exception as e:
messages.error(request, f"Import failed: {e}")
else:
form = ImportForm()
return render(request, "import_wizard.html", {"form": form})
@login_required @login_required