Update web/core/views.py

This commit is contained in:
Joshua Laymon 2025-08-13 23:54:50 +00:00
parent f5534328d3
commit c2943a59f8

View File

@ -4,12 +4,14 @@ from django.contrib.auth.decorators import login_required, user_passes_test
from django.http import HttpResponse from django.http import HttpResponse
from django.contrib import messages from django.contrib import messages
from django.db.models import Q from django.db.models import Q
from django.views.decorators.http import require_http_methods
from datetime import date, timedelta from datetime import date, timedelta
import csv import csv
from .models import Entry from .models import Entry
from .forms import ImportForm, EntryForm from .forms import ImportForm, EntryForm
from .utils import terms, has_wildcards, wildcard_to_regex, import_csv_bytes from .utils import terms, has_wildcards, wildcard_to_regex, import_csv_bytes
from .scripture_normalizer import normalize_scripture_field # <-- NEW
# Order + labels used in the Search UI # Order + labels used in the Search UI
FIELD_ORDER = [ FIELD_ORDER = [
@ -381,4 +383,78 @@ def stats_page(request):
"heights": heights, "heights": heights,
"top_subjects": top_subjects, "top_subjects": top_subjects,
}, },
)
# ========= NEW: Scripture Normalizer endpoint =========
@login_required
@user_passes_test(is_admin)
@require_http_methods(["GET", "POST"])
def normalize_scripture(request):
"""
GET -> dry-run preview (summary + first 100 examples)
POST -> apply changes to all entries' scripture_raw (batched)
Optional ?limit= for preview subset.
"""
apply = request.method == "POST"
limit = int(request.GET.get("limit", "0") or "0")
qs = Entry.objects.all().order_by("id")
if limit:
qs = qs[:limit]
changed = 0
warnings_total = 0
preview = []
if apply:
# write in batches to keep transactions short
from django.db import transaction
batch, pending = 500, []
for e in qs.iterator():
original = (e.scripture_raw or "").strip()
normalized, warns = normalize_scripture_field(original)
warnings_total += len(warns)
if normalized != original:
changed += 1
preview.append((e.id, original, normalized))
e.scripture_raw = normalized
pending.append(e)
if len(pending) >= batch:
with transaction.atomic():
for obj in pending:
obj.save(update_fields=["scripture_raw"])
pending.clear()
if pending:
with transaction.atomic():
for obj in pending:
obj.save(update_fields=["scripture_raw"])
else:
# dry-run only
for e in qs.iterator():
original = (e.scripture_raw or "").strip()
normalized, warns = normalize_scripture_field(original)
warnings_total += len(warns)
if normalized != original:
changed += 1
preview.append((e.id, original, normalized))
preview = preview[:100] # keep the table reasonable
messages.info(
request,
f"{'Applied' if apply else 'Dryrun'}: {changed} entries "
f"{'changed' if apply else 'would change'}; {warnings_total} warnings."
)
return render(
request,
"normalize_result.html",
{
"applied": apply,
"changed": changed,
"warnings_total": warnings_total,
"preview": preview,
"limit": limit,
},
) )