Add field converter
This commit is contained in:
parent
47a4d28cf0
commit
4d908d0bbb
|
|
@ -2,8 +2,10 @@ from os.path import exists
|
|||
from importlib.machinery import SourceFileLoader
|
||||
from django.apps import apps
|
||||
from collections import OrderedDict
|
||||
import importlib
|
||||
from pprint import pprint
|
||||
|
||||
|
||||
def transfer_objects(data_type):
|
||||
models_list = {}
|
||||
|
||||
|
|
@ -67,19 +69,9 @@ def get_fields_map(card, queryset):
|
|||
for legacy_object in queryset.iterator():
|
||||
app_queryset_dict = {}
|
||||
for app_field, legacy_field in fields_list.items():
|
||||
if isinstance(legacy_field, tuple):
|
||||
if isinstance(legacy_field[0], tuple):
|
||||
# Группа полей с аргументами
|
||||
pass
|
||||
else:
|
||||
print(app_field)
|
||||
print(legacy_field)
|
||||
else:
|
||||
try:
|
||||
app_queryset_dict[app_field] = getattr(legacy_object, legacy_field)
|
||||
except Exception as e:
|
||||
print(f"Attribute {legacy_field} not found in {legacy_object}: {e}")
|
||||
return
|
||||
app_value = convert_field_from_legacy_to_app(legacy_object, legacy_field)
|
||||
if app_value is not None:
|
||||
app_queryset_dict[app_field] = app_value
|
||||
|
||||
app_queryset_list.append(app_queryset_dict)
|
||||
|
||||
|
|
@ -92,7 +84,45 @@ def get_fields_map(card, queryset):
|
|||
# pprint(len(queryset))
|
||||
print("==============================")
|
||||
|
||||
return card
|
||||
return app_queryset_list
|
||||
|
||||
|
||||
# Convert field
|
||||
def convert_field_from_legacy_to_app(legacy_object, legacy_field):
|
||||
result = None
|
||||
if isinstance(legacy_field, tuple):
|
||||
if isinstance(legacy_field[0], tuple):
|
||||
# legacy_attributes = {}
|
||||
# for legacy_attribute in legacy_field:
|
||||
print(legacy_field)
|
||||
else:
|
||||
if hasattr(legacy_object, legacy_field[0]):
|
||||
try:
|
||||
legacy_data = getattr(legacy_object, legacy_field[0])
|
||||
except Exception as e:
|
||||
raise AttributeError(f"Attribute {legacy_field} not found in {legacy_object}: {e}")
|
||||
|
||||
field_module = legacy_field[-1].split(".")
|
||||
try:
|
||||
field_type = getattr(importlib.import_module(".".join(field_module[:-1])), field_module[-1])
|
||||
except Exception as e:
|
||||
raise ValueError(f"Cannot set new field type for field {legacy_field}: {e}")
|
||||
|
||||
if len(legacy_field) == 3:
|
||||
result = field_type(**{legacy_field[1]: legacy_data})
|
||||
elif len(legacy_field) == 2:
|
||||
result = field_type(legacy_data)
|
||||
|
||||
else:
|
||||
print(f"Attribute {legacy_field} not found in {legacy_object.__dict__}")
|
||||
|
||||
else:
|
||||
if hasattr(legacy_object, legacy_field):
|
||||
result = getattr(legacy_object, legacy_field)
|
||||
else:
|
||||
print(f"Attribute {legacy_field} not found in {legacy_object.__dict__}")
|
||||
|
||||
return result
|
||||
|
||||
|
||||
# Get legacy model data
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user