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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Script để xóa tất cả dữ liệu không liên quan đến 4 file legal documents được chỉ định.
|
| 4 |
+
Chỉ giữ lại:
|
| 5 |
+
1. 1. BIÊN SOẠN THÔNG TƯ 02.docx
|
| 6 |
+
2. 264-QD_TW_644732 sửa đổi bổ sung QĐ 69 về kỷ luật đảng viên.doc
|
| 7 |
+
3. QD-69-TW về kỷ luật đảng viên.pdf
|
| 8 |
+
4. THÔNG TƯ 02 VỀ XỬ LÝ ĐIỀU LỆNH TRONG CAND.docx
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
from __future__ import annotations
|
| 12 |
+
|
| 13 |
+
import os
|
| 14 |
+
import sys
|
| 15 |
+
from pathlib import Path
|
| 16 |
+
|
| 17 |
+
ROOT_DIR = Path(__file__).resolve().parents[2]
|
| 18 |
+
BACKEND_DIR = ROOT_DIR / "backend"
|
| 19 |
+
|
| 20 |
+
# Add backend directory to sys.path for Django
|
| 21 |
+
if str(BACKEND_DIR) not in sys.path:
|
| 22 |
+
sys.path.insert(0, str(BACKEND_DIR))
|
| 23 |
+
|
| 24 |
+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hue_portal.hue_portal.settings")
|
| 25 |
+
|
| 26 |
+
import django
|
| 27 |
+
django.setup()
|
| 28 |
+
|
| 29 |
+
from django.db import transaction
|
| 30 |
+
from hue_portal.core.models import (
|
| 31 |
+
Fine,
|
| 32 |
+
Office,
|
| 33 |
+
Procedure,
|
| 34 |
+
Advisory,
|
| 35 |
+
LegalDocument,
|
| 36 |
+
LegalSection,
|
| 37 |
+
LegalDocumentImage,
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# Danh sách các file được giữ lại (theo code hoặc original_filename)
|
| 42 |
+
KEEP_DOCUMENT_CODES = [
|
| 43 |
+
"QD-69-TW",
|
| 44 |
+
"TT-02-CAND",
|
| 45 |
+
"TT-02-BIEN-SOAN",
|
| 46 |
+
"264-QD-TW",
|
| 47 |
+
]
|
| 48 |
+
|
| 49 |
+
KEEP_FILENAMES = [
|
| 50 |
+
"QD-69-TW về kỷ luật đảng viên.pdf",
|
| 51 |
+
"THÔNG TƯ 02 VỀ XỬ LÝ ĐIỀU LỆNH TRONG CAND.docx",
|
| 52 |
+
"1. BIÊN SOẠN THÔNG TƯ 02.docx",
|
| 53 |
+
"264-QD_TW_644732 sửa đổi bổ sung QĐ 69 về kỷ luật đảng viên.doc",
|
| 54 |
+
]
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def get_keep_document_ids() -> set[int]:
|
| 58 |
+
"""Lấy IDs của các LegalDocument cần giữ lại."""
|
| 59 |
+
keep_ids = set()
|
| 60 |
+
|
| 61 |
+
# Tìm theo code
|
| 62 |
+
for code in KEEP_DOCUMENT_CODES:
|
| 63 |
+
docs = LegalDocument.objects.filter(code=code)
|
| 64 |
+
for doc in docs:
|
| 65 |
+
keep_ids.add(doc.id)
|
| 66 |
+
print(f"✅ Giữ lại document: {doc.code} - {doc.title}")
|
| 67 |
+
|
| 68 |
+
# Tìm theo original_filename
|
| 69 |
+
for filename in KEEP_FILENAMES:
|
| 70 |
+
docs = LegalDocument.objects.filter(original_filename__icontains=filename.split("/")[-1])
|
| 71 |
+
for doc in docs:
|
| 72 |
+
keep_ids.add(doc.id)
|
| 73 |
+
if doc.id not in keep_ids:
|
| 74 |
+
print(f"✅ Giữ lại document: {doc.code} - {doc.title} (theo filename)")
|
| 75 |
+
|
| 76 |
+
return keep_ids
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def cleanup_unrelated_data(dry_run: bool = False) -> None:
|
| 80 |
+
"""Xóa tất cả dữ liệu không liên quan đến 4 file được chỉ định."""
|
| 81 |
+
print("=" * 60)
|
| 82 |
+
print("🧹 Dọn dẹp dữ liệu không liên quan")
|
| 83 |
+
print("=" * 60)
|
| 84 |
+
|
| 85 |
+
if dry_run:
|
| 86 |
+
print("⚠️ DRY RUN MODE - Không thực sự xóa dữ liệu")
|
| 87 |
+
print()
|
| 88 |
+
|
| 89 |
+
# Lấy IDs của documents cần giữ lại
|
| 90 |
+
keep_doc_ids = get_keep_document_ids()
|
| 91 |
+
print(f"\n📋 Sẽ giữ lại {len(keep_doc_ids)} document(s)")
|
| 92 |
+
|
| 93 |
+
if not keep_doc_ids:
|
| 94 |
+
print("⚠️ Không tìm thấy document nào cần giữ lại!")
|
| 95 |
+
print(" Có thể các file chưa được load vào database.")
|
| 96 |
+
print(" Chạy: python backend/scripts/load_legal_documents.py")
|
| 97 |
+
return
|
| 98 |
+
|
| 99 |
+
with transaction.atomic():
|
| 100 |
+
# 1. Xóa tất cả Fines
|
| 101 |
+
fines_count = Fine.objects.count()
|
| 102 |
+
if not dry_run:
|
| 103 |
+
Fine.objects.all().delete()
|
| 104 |
+
print(f"🗑️ {'Sẽ xóa' if dry_run else 'Đã xóa'} {fines_count} Fine(s)")
|
| 105 |
+
|
| 106 |
+
# 2. Xóa tất cả Procedures
|
| 107 |
+
procedures_count = Procedure.objects.count()
|
| 108 |
+
if not dry_run:
|
| 109 |
+
Procedure.objects.all().delete()
|
| 110 |
+
print(f"🗑️ {'Sẽ xóa' if dry_run else 'Đã xóa'} {procedures_count} Procedure(s)")
|
| 111 |
+
|
| 112 |
+
# 3. Xóa tất cả Advisories
|
| 113 |
+
advisories_count = Advisory.objects.count()
|
| 114 |
+
if not dry_run:
|
| 115 |
+
Advisory.objects.all().delete()
|
| 116 |
+
print(f"🗑️ {'Sẽ xóa' if dry_run else 'Đã xóa'} {advisories_count} Advisory(ies)")
|
| 117 |
+
|
| 118 |
+
# 4. Xóa tất cả Offices
|
| 119 |
+
offices_count = Office.objects.count()
|
| 120 |
+
if not dry_run:
|
| 121 |
+
Office.objects.all().delete()
|
| 122 |
+
print(f"🗑️ {'Sẽ xóa' if dry_run else 'Đã xóa'} {offices_count} Office(s)")
|
| 123 |
+
|
| 124 |
+
# 5. Xóa LegalDocumentImage của documents không được giữ lại
|
| 125 |
+
images_to_delete = LegalDocumentImage.objects.exclude(document_id__in=keep_doc_ids)
|
| 126 |
+
images_count = images_to_delete.count()
|
| 127 |
+
if not dry_run:
|
| 128 |
+
images_to_delete.delete()
|
| 129 |
+
print(f"🗑️ {'Sẽ xóa' if dry_run else 'Đã xóa'} {images_count} LegalDocumentImage(s)")
|
| 130 |
+
|
| 131 |
+
# 6. Xóa LegalSection của documents không được giữ lại
|
| 132 |
+
sections_to_delete = LegalSection.objects.exclude(document_id__in=keep_doc_ids)
|
| 133 |
+
sections_count = sections_to_delete.count()
|
| 134 |
+
if not dry_run:
|
| 135 |
+
sections_to_delete.delete()
|
| 136 |
+
print(f"🗑️ {'Sẽ xóa' if dry_run else 'Đã xóa'} {sections_count} LegalSection(s)")
|
| 137 |
+
|
| 138 |
+
# 7. Xóa LegalDocument không được giữ lại
|
| 139 |
+
docs_to_delete = LegalDocument.objects.exclude(id__in=keep_doc_ids)
|
| 140 |
+
docs_count = docs_to_delete.count()
|
| 141 |
+
if not dry_run:
|
| 142 |
+
# Liệt kê các document sẽ bị xóa
|
| 143 |
+
print(f"\n📄 Các document sẽ bị xóa ({docs_count}):")
|
| 144 |
+
for doc in docs_to_delete:
|
| 145 |
+
print(f" - {doc.code}: {doc.title}")
|
| 146 |
+
docs_to_delete.delete()
|
| 147 |
+
print(f"🗑️ {'Sẽ xóa' if dry_run else 'Đã xóa'} {docs_count} LegalDocument(s)")
|
| 148 |
+
|
| 149 |
+
if dry_run:
|
| 150 |
+
print("\n⚠️ DRY RUN - Không có dữ liệu nào bị xóa thực sự")
|
| 151 |
+
print(" Chạy lại không có --dry-run để thực sự xóa")
|
| 152 |
+
else:
|
| 153 |
+
print("\n✅ Hoàn tất dọn dẹp!")
|
| 154 |
+
print(f" Giữ lại {len(keep_doc_ids)} document(s)")
|
| 155 |
+
print("\n📝 Bước tiếp theo:")
|
| 156 |
+
print(" 1. Regenerate embeddings: python backend/scripts/generate_embeddings.py")
|
| 157 |
+
print(" 2. Rebuild FAISS index: python backend/scripts/build_faiss_index.py")
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
def main():
|
| 161 |
+
import argparse
|
| 162 |
+
|
| 163 |
+
parser = argparse.ArgumentParser(description="Xóa dữ liệu không liên quan đến 4 file legal documents")
|
| 164 |
+
parser.add_argument(
|
| 165 |
+
"--dry-run",
|
| 166 |
+
action="store_true",
|
| 167 |
+
help="Chỉ hiển thị sẽ xóa gì, không thực sự xóa",
|
| 168 |
+
)
|
| 169 |
+
args = parser.parse_args()
|
| 170 |
+
|
| 171 |
+
cleanup_unrelated_data(dry_run=args.dry_run)
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
if __name__ == "__main__":
|
| 175 |
+
main()
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
|