gault-millau/apps/transfer/utils.py
littlewolf a7513e6ca0 Add transfer cleaner
Add clean to Country, Region and City
2019-11-21 18:34:22 +03:00

38 lines
1.4 KiB
Python

from os.path import exists
from importlib.machinery import SourceFileLoader
from django.apps import apps
from django.conf import settings
def transfer_objects(data_type):
for app in apps.get_app_configs():
if exists(f"{app.path}/transfer_data.py"):
card_module = SourceFileLoader("transfer", f"{app.path}/transfer_data.py").load_module()
if not hasattr(card_module, "data_types") \
or not isinstance(card_module.data_types, dict) \
or len(card_module.data_types) < 1:
continue
for module_data_type, transfer_funcs in card_module.data_types.items():
if data_type == module_data_type:
for transfer_func in transfer_funcs:
print(f"========================== FUNCTION {transfer_func.__name__} ================================")
transfer_func()
def clean_old_records(model, conditions):
error_file = open(f"{settings.PROJECT_ROOT}/apps/transfer/clear.error.txt", "w")
try:
old_records = model.objects.filter(**conditions)
except Exception as e:
error_file.write(f"Cannot find model objects: {e}")
return
for old_record in old_records:
try:
old_record.delete()
except Exception as e:
error_file.write(f"Cannot delete object {old_record}: {e}")