taxes_az/taxes_az/fix_adi.py

107 lines
3.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import json
def fix_adi_fields(json_path, dry_run=True):
"""Переименовывает поле 'adi' с типом Select в 'ad_select'"""
try:
with open(json_path, 'r', encoding='utf-8') as f:
data = json.load(f)
if 'fields' not in data:
return False
# Ищем оба поля
adi_field = None
adi_turk_field = None
for idx, field in enumerate(data['fields']):
if 'fieldname' in field:
if field['fieldname'] == 'adi':
adi_field = (idx, field)
elif field['fieldname'] == 'adı':
adi_turk_field = (idx, field)
# Если оба поля найдены
if adi_field and adi_turk_field:
idx, field = adi_field
# Проверяем что это Select
if field.get('fieldtype') == 'Select':
print(f"\n📄 DocType: {data.get('name', 'Unknown')}")
print(f" Файл: {json_path}")
print(f" Найдено поле 'adi' с типом Select")
print(f" ➜ Переименование: 'adi''ad_select'")
if not dry_run:
data['fields'][idx]['fieldname'] = 'ad_select'
with open(json_path, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=1)
print(f" ✓ Изменения сохранены")
return True
else:
print(f"\n⚠️ DocType: {data.get('name', 'Unknown')}")
print(f" Файл: {json_path}")
print(f" Найдены оба поля, но 'adi' НЕ Select (тип: {field.get('fieldtype')})")
print(f" Пропускаю...")
return False
return False
except Exception as e:
print(f"❌ Ошибка при обработке {json_path}: {e}")
return False
# Путь к приложению
app_path = '/home/frappe/frappe-bench/apps/taxes_az/taxes_az/taxes_az'
doctype_path = os.path.join(app_path, 'doctype')
if not os.path.exists(doctype_path):
print(f"❌ Путь не найден: {doctype_path}")
exit(1)
print("=" * 60)
print("РЕЖИМ ПРОСМОТРА (dry run)")
print("=" * 60)
fixed_count = 0
for root, dirs, files in os.walk(doctype_path):
for file in files:
if file.endswith('.json'):
json_path = os.path.join(root, file)
if fix_adi_fields(json_path, dry_run=True):
fixed_count += 1
if fixed_count > 0:
print("\n" + "=" * 60)
print(f"Найдено {fixed_count} файлов для изменения")
print("=" * 60)
response = input("\n❓ Применить изменения? (yes/no): ")
if response.lower() in ['yes', 'y', 'да', 'д']:
print("\n" + "=" * 60)
print("ПРИМЕНЕНИЕ ИЗМЕНЕНИЙ")
print("=" * 60)
fixed_count = 0
for root, dirs, files in os.walk(doctype_path):
for file in files:
if file.endswith('.json'):
json_path = os.path.join(root, file)
if fix_adi_fields(json_path, dry_run=False):
fixed_count += 1
print("\n" + "=" * 60)
print(f"✓ Исправлено {fixed_count} файлов!")
print("=" * 60)
print("\n📋 Следующие шаги:")
print(" 1. bench clear-cache")
print(" 2. bench migrate")
else:
print("\n❌ Отменено. Изменения не применены.")
else:
print("\n✓ Файлов для изменения не найдено.")