Update web/core/views.py
This commit is contained in:
parent
5fb0d70049
commit
5077ecffca
@ -14,6 +14,7 @@ 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
|
from .scripture_normalizer import normalize_scripture_field # <-- NEW
|
||||||
from .source_normalizer import normalize_source_field # NEW
|
from .source_normalizer import normalize_source_field # NEW
|
||||||
|
from .subject_normalizer import normalize_subject_field # NEW
|
||||||
|
|
||||||
# Order + labels used in the Search UI
|
# Order + labels used in the Search UI
|
||||||
FIELD_ORDER = [
|
FIELD_ORDER = [
|
||||||
@ -639,3 +640,74 @@ def stats_page(request):
|
|||||||
"book_distribution": book_distribution,
|
"book_distribution": book_distribution,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
@login_required
|
||||||
|
@user_passes_test(is_admin)
|
||||||
|
@require_http_methods(["GET", "POST"])
|
||||||
|
def normalize_subjects(request):
|
||||||
|
"""
|
||||||
|
GET -> dry-run preview (summary + first 100 examples)
|
||||||
|
POST -> apply changes to all entries' subject (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.subject or "").strip()
|
||||||
|
normalized, warns = normalize_subject_field(original)
|
||||||
|
warnings_total += len(warns)
|
||||||
|
if normalized != original:
|
||||||
|
changed += 1
|
||||||
|
preview.append((e.id, original, normalized))
|
||||||
|
e.subject = normalized
|
||||||
|
pending.append(e)
|
||||||
|
if len(pending) >= batch:
|
||||||
|
with transaction.atomic():
|
||||||
|
for obj in pending:
|
||||||
|
obj.save(update_fields=["subject"])
|
||||||
|
pending.clear()
|
||||||
|
if pending:
|
||||||
|
from django.db import transaction
|
||||||
|
with transaction.atomic():
|
||||||
|
for obj in pending:
|
||||||
|
obj.save(update_fields=["subject"])
|
||||||
|
else:
|
||||||
|
# dry-run only
|
||||||
|
for e in qs.iterator():
|
||||||
|
original = (e.subject or "").strip()
|
||||||
|
normalized, warns = normalize_subject_field(original)
|
||||||
|
warnings_total += len(warns)
|
||||||
|
if normalized != original:
|
||||||
|
changed += 1
|
||||||
|
preview.append((e.id, original, normalized))
|
||||||
|
|
||||||
|
preview = preview[:100] # keep table reasonable
|
||||||
|
|
||||||
|
messages.info(
|
||||||
|
request,
|
||||||
|
f"{'Applied' if apply else 'Dry-run'}: {changed} entries "
|
||||||
|
f"{'changed' if apply else 'would change'}; {warnings_total} warnings."
|
||||||
|
)
|
||||||
|
return render(
|
||||||
|
request,
|
||||||
|
"normalize_subjects_result.html",
|
||||||
|
{
|
||||||
|
"applied": apply,
|
||||||
|
"changed": changed,
|
||||||
|
"warnings_total": warnings_total,
|
||||||
|
"preview": preview,
|
||||||
|
"limit": limit,
|
||||||
|
},
|
||||||
|
)
|
||||||
Loading…
Reference in New Issue
Block a user