SillaAMP_V2/B_main/restore_phone_dashes.py

61 lines
1.9 KiB
Python
Raw Normal View History

#!/usr/bin/env python
"""
기존 Person 데이터의 전화번호를 대시 있는 형태로 되돌리는 스크립트
"""
import os
import sys
import django
import re
# Django 설정
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'A_core.settings')
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
django.setup()
from B_main.models import Person
def format_phone_with_dash(phone):
"""전화번호를 010-XXXX-XXXX 형식으로 변환"""
if not phone:
return phone
# 숫자만 추출
numbers = re.sub(r'[^0-9]', '', phone)
# 11자리인 경우에만 포맷팅
if len(numbers) == 11 and numbers.startswith('010'):
return f"{numbers[:3]}-{numbers[3:7]}-{numbers[7:]}"
elif len(numbers) == 10 and numbers.startswith('010'):
return f"{numbers[:3]}-{numbers[3:6]}-{numbers[6:]}"
return phone
def restore_phone_dashes():
"""기존 Person 데이터의 전화번호를 대시 있는 형태로 되돌리기"""
print("=" * 60)
print("Person 데이터 전화번호 대시 복원")
print("=" * 60)
# 모든 Person 데이터 조회
persons = Person.objects.all()
updated_count = 0
for person in persons:
if person.연락처:
old_phone = person.연락처
new_phone = format_phone_with_dash(old_phone)
if old_phone != new_phone:
print(f"복원: {person.이름} - {old_phone}{new_phone}")
person.연락처 = new_phone
person.save()
updated_count += 1
else:
print(f"변경 없음: {person.이름} - {old_phone}")
else:
print(f"전화번호 없음: {person.이름}")
print(f"\n{updated_count}개의 전화번호가 복원되었습니다.")
if __name__ == '__main__':
restore_phone_dashes()