Handle object dict both in all and single model case

This commit is contained in:
PCoder 2021-11-22 19:28:17 +05:30
parent 20b1ac2e7d
commit e9d545fdd1

View file

@ -329,43 +329,7 @@ class Command(BaseCommand):
first = False
continue
_object_dict = {str(self.cols_to_django_fields.get(key)): str(value.lstrip('"').rstrip('"')) for key, value in zip(header, row)}
if model_name == 'Range':
# Reinstate range_name key
_object_dict['range_name'] = _object_dict['range_name_id']
_object_dict.pop('range_name_id')
if model_name == 'Keyword':
_object_dict['keyword'] = _object_dict['keyword_id']
_object_dict.pop('keyword_id')
if model_name == 'Organization' and 'country_id' in _object_dict:
_object_dict['country'] = _object_dict['country_id']
_object_dict.pop('country_id')
if model_name == 'PeopleRange' and 'mountain_range' in _object_dict:
_object_dict['range_id'] = _object_dict['mountain_range']
_object_dict.pop('mountain_range')
if model_name == 'Species' and 'scientific_name_id' in _object_dict:
_object_dict['scientific_name'] = _object_dict['scientific_name_id']
_object_dict.pop('scientific_name_id')
if model_name == 'TaxonRange' and 'taxon_id' in _object_dict:
_object_dict['taxon'] = _object_dict['taxon_id']
_object_dict.pop('taxon_id')
if model_name == 'Person' and 'organization_id' in _object_dict:
print("organization_id=%s" % _object_dict['organization_id'])
if _object_dict['organization_id'] == '' or _object_dict['organization_id'] is None:
_object_dict['organization_id'] = '-1'
else:
_object_dict['organization_id'] = int(float(_object_dict['organization_id']))
for i in ['news_letter', 'birds', 'mammals', 'reptiles', 'amphibians', 'fish', 'insects',
'molluscs', 'crustaceans', 'arachnids', 'angiosperms', 'gymnosperms', 'fungi',
'algae', 'microbes', 'biological_field_sampling', 'data_mining', 'remote_sensing',
'gis', 'spatial_analysis', 'statistical_analysis', 'modelling', 'assessment',
'meta_analysis', 'synthesis', 'qualitative_ssm', 'genetic_analyses', 'field_site',
'transect', 'mountain_top', 'mountain_range', 'landscape', 'regional', 'national',
'_global', 'profile_on_web', 'updated']:
if i in _object_dict:
_object_dict[i] = True if _object_dict[i].lower().strip() == 'true' else False
print(_object_dict)
if _object_dict is None:
print("Object None for %s" % model_name)
_object_dict = handle_object_dict(_object_dict, model_name)
m = _model(**_object_dict)
try:
m.save()
@ -378,6 +342,7 @@ class Command(BaseCommand):
print("Done importing %s" % model_name)
else:
_model = apps.get_model(options.get('app_name', 'app'), options['model_name'])
model_name = options['model_name']
file_path = options.get('path')
csv.register_dialect(
'mydialect',
@ -398,6 +363,48 @@ class Command(BaseCommand):
first = False
continue
_object_dict = {self.cols_to_django_fields.get(key): value.lstrip('"').rstrip('"') for key, value in zip(header, row)}
_object_dict = handle_object_dict(_object_dict, model_name)
m = _model(**_object_dict)
m.save()
print("Done importing %s" % str(_model))
def handle_object_dict(object_dict, model_name):
if model_name == 'Range':
# Reinstate range_name key
object_dict['range_name'] = object_dict['range_name_id']
object_dict.pop('range_name_id')
if model_name == 'Keyword':
object_dict['keyword'] = object_dict['keyword_id']
object_dict.pop('keyword_id')
if model_name == 'Organization' and 'country_id' in object_dict:
object_dict['country'] = object_dict['country_id']
object_dict.pop('country_id')
if model_name == 'PeopleRange' and 'mountain_range' in object_dict:
object_dict['range_id'] = object_dict['mountain_range']
object_dict.pop('mountain_range')
if model_name == 'Species' and 'scientific_name_id' in object_dict:
object_dict['scientific_name'] = object_dict['scientific_name_id']
object_dict.pop('scientific_name_id')
if model_name == 'TaxonRange' and 'taxon_id' in object_dict:
object_dict['taxon'] = object_dict['taxon_id']
object_dict.pop('taxon_id')
if model_name == 'Person' and 'organization_id' in object_dict:
print("organization_id=%s" % object_dict['organization_id'])
if object_dict['organization_id'] == '' or object_dict['organization_id'] is None:
object_dict['organization_id'] = '-1'
else:
object_dict['organization_id'] = int(float(object_dict['organization_id']))
for i in ['news_letter', 'birds', 'mammals', 'reptiles', 'amphibians', 'fish', 'insects',
'molluscs', 'crustaceans', 'arachnids', 'angiosperms', 'gymnosperms', 'fungi',
'algae', 'microbes', 'biological_field_sampling', 'data_mining', 'remote_sensing',
'gis', 'spatial_analysis', 'statistical_analysis', 'modelling', 'assessment',
'meta_analysis', 'synthesis', 'qualitative_ssm', 'genetic_analyses', 'field_site',
'transect', 'mountain_top', 'mountain_range', 'landscape', 'regional', 'national',
'_global', 'profile_on_web', 'updated']:
if i in object_dict:
object_dict[i] = True if object_dict[i].lower().strip() == 'true' else False
print(object_dict)
if object_dict is None:
print("Object None for %s" % model_name)
return object_dict