Add web/core/normalize_subjects.py
This commit is contained in:
parent
aa9a0e8784
commit
46c0e4cc18
47
web/core/normalize_subjects.py
Normal file
47
web/core/normalize_subjects.py
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
from django.core.management.base import BaseCommand
|
||||||
|
from core.models import Entry
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
help = "Normalize subjects (split by commas, unify delimiters like ; and -)"
|
||||||
|
|
||||||
|
def add_arguments(self, parser):
|
||||||
|
parser.add_argument(
|
||||||
|
"--dry-run",
|
||||||
|
action="store_true",
|
||||||
|
help="Preview changes without saving"
|
||||||
|
)
|
||||||
|
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
dry_run = options["dry_run"]
|
||||||
|
changes = 0
|
||||||
|
|
||||||
|
for entry in Entry.objects.all():
|
||||||
|
subj = entry.subject or ""
|
||||||
|
normalized = self.normalize_subjects(subj)
|
||||||
|
|
||||||
|
if subj != normalized:
|
||||||
|
changes += 1
|
||||||
|
self.stdout.write(
|
||||||
|
f"Would change: {subj} → {normalized}"
|
||||||
|
)
|
||||||
|
if not dry_run:
|
||||||
|
entry.subject = normalized
|
||||||
|
entry.save(update_fields=["subject"])
|
||||||
|
|
||||||
|
if dry_run:
|
||||||
|
self.stdout.write(self.style.WARNING(f"[DRY RUN] {changes} subjects would be changed."))
|
||||||
|
else:
|
||||||
|
self.stdout.write(self.style.SUCCESS(f"Updated {changes} subjects."))
|
||||||
|
|
||||||
|
def normalize_subjects(self, text: str) -> str:
|
||||||
|
if not text:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
# Unify delimiters into commas
|
||||||
|
cleaned = text.replace(";", ",").replace("|", ",").replace("/", ",").replace(" - ", ",").replace("-", ",")
|
||||||
|
|
||||||
|
# Split, strip spaces, remove empties
|
||||||
|
parts = [p.strip() for p in cleaned.split(",") if p.strip()]
|
||||||
|
|
||||||
|
# Rejoin with a standard ", " separator
|
||||||
|
return ", ".join(parts)
|
||||||
Loading…
Reference in New Issue
Block a user