Update web/core/views.py
This commit is contained in:
parent
f8c29935ff
commit
cc6461ced2
@ -340,11 +340,10 @@ def import_wizard(request):
|
|||||||
try:
|
try:
|
||||||
raw = form.cleaned_data["file"].read()
|
raw = form.cleaned_data["file"].read()
|
||||||
|
|
||||||
# --- Decode once (BOM-safe) ---
|
|
||||||
import io, csv as _csv
|
import io, csv as _csv
|
||||||
text = raw.decode("utf-8-sig", errors="replace")
|
text = raw.decode("utf-8-sig", errors="replace")
|
||||||
|
|
||||||
# --- Try to sniff a dialect; fall back to Excel CSV ---
|
# --- Try to sniff dialect ---
|
||||||
try:
|
try:
|
||||||
first_line = text.splitlines()[0] if text else ""
|
first_line = text.splitlines()[0] if text else ""
|
||||||
dialect = _csv.Sniffer().sniff(first_line) if first_line else _csv.excel
|
dialect = _csv.Sniffer().sniff(first_line) if first_line else _csv.excel
|
||||||
@ -370,14 +369,13 @@ def import_wizard(request):
|
|||||||
|
|
||||||
first = rows[0]
|
first = rows[0]
|
||||||
norm_first = [_clean_header(c) for c in first]
|
norm_first = [_clean_header(c) for c in first]
|
||||||
|
|
||||||
header_ok = (norm_first == expected_norm)
|
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):
|
if not header_ok and len(first) == len(EXPECTED):
|
||||||
rows.insert(0, EXPECTED)
|
rows.insert(0, EXPECTED)
|
||||||
elif not header_ok and len(first) != len(EXPECTED):
|
elif not header_ok and len(first) != len(EXPECTED):
|
||||||
# Retry with common alternate delimiters
|
# Retry with alternate delimiters
|
||||||
for delim in (";", "\t"):
|
for delim in (";", "\t"):
|
||||||
rows2 = list(_csv.reader(io.StringIO(text), delimiter=delim))
|
rows2 = list(_csv.reader(io.StringIO(text), delimiter=delim))
|
||||||
if rows2 and len(rows2[0]) == len(EXPECTED):
|
if rows2 and len(rows2[0]) == len(EXPECTED):
|
||||||
@ -389,44 +387,28 @@ def import_wizard(request):
|
|||||||
rows.insert(0, EXPECTED)
|
rows.insert(0, EXPECTED)
|
||||||
break
|
break
|
||||||
|
|
||||||
# Re-encode sanitized CSV for the existing importer
|
# Re-encode sanitized CSV for importer
|
||||||
out = io.StringIO()
|
out = io.StringIO()
|
||||||
w = _csv.writer(out)
|
w = _csv.writer(out)
|
||||||
for r in rows:
|
for r in rows:
|
||||||
w.writerow(r)
|
w.writerow(r)
|
||||||
fixed_raw = out.getvalue().encode("utf-8")
|
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
|
from . import utils as core_utils
|
||||||
core_utils.EXPECTED_HEADERS = EXPECTED
|
core_utils.EXPECTED_HEADERS = EXPECTED
|
||||||
|
|
||||||
# Run your robust importer
|
# Run robust importer
|
||||||
# ... inside import_wizard, after:
|
report = import_csv_bytes(fixed_raw, dry_run=form.cleaned_data["dry_run"]) or {}
|
||||||
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)
|
# Normalize preview
|
||||||
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
|
|
||||||
report.setdefault("columns", EXPECTED)
|
report.setdefault("columns", EXPECTED)
|
||||||
if report.get("preview"):
|
preview = report.get("preview") or []
|
||||||
if isinstance(report["preview"][0], dict):
|
if preview and isinstance(preview[0], dict):
|
||||||
cols = report["columns"]
|
cols = report["columns"]
|
||||||
report["preview"] = [
|
report["preview"] = [[row.get(c, "") for c in cols] for row in preview]
|
||||||
[row.get(c, "") for c in cols] for row in report["preview"]
|
|
||||||
]
|
report["header_ok"] = header_ok
|
||||||
|
|
||||||
if not header_ok:
|
if not header_ok:
|
||||||
messages.warning(
|
messages.warning(
|
||||||
@ -439,13 +421,14 @@ if preview and isinstance(preview[0], dict):
|
|||||||
"import_result.html",
|
"import_result.html",
|
||||||
{"report": report, "dry_run": form.cleaned_data["dry_run"]},
|
{"report": report, "dry_run": form.cleaned_data["dry_run"]},
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
messages.error(request, f"Import failed: {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})
|
return render(request, "import_wizard.html", {"form": form})
|
||||||
|
|
||||||
# GET
|
# GET → show empty form
|
||||||
form = ImportForm()
|
form = ImportForm()
|
||||||
return render(request, "import_wizard.html", {"form": form})
|
return render(request, "import_wizard.html", {"form": form})
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user