Skip to content

Commit

Permalink
Refactor main import function
Browse files Browse the repository at this point in the history
  • Loading branch information
tudoramariei committed Feb 15, 2024
1 parent 7463c4f commit 1387895
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions backend/importer/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,15 @@ def run_import(import_obj):
f"with a batch size of {settings.IMPORT_BATCH_SIZE}"
)

import_data_into_db(import_data, import_model)

import_obj.status = ImportStatusChoices.DONE
import_obj.save()


def import_data_into_db(import_data, import_model):
batch_number = 0
items_imported = 0

errors = []
for i in range(0, len(import_data), settings.IMPORT_BATCH_SIZE):
import_batch = import_data[i : i + settings.IMPORT_BATCH_SIZE]
try:
Expand All @@ -104,13 +109,11 @@ def run_import(import_obj):

import_model.objects.create(**item)
except IntegrityError as e:
errors.append(e)
logger.exception(f"Error importing item #{items_imported + 1} {item}: {e}")

batch_number += 1

import_obj.status = ImportStatusChoices.DONE
import_obj.save()
logger.info(f"Imported {items_imported} items into {import_model}")


def process_raw_data(import_obj: ImportJob, import_model: ModelBase, raw_data: List[Dict]) -> List[Dict]:
Expand Down

0 comments on commit 1387895

Please sign in to comment.