From 72a752fd56c9a43980c2d97a83bc1d9374df807c Mon Sep 17 00:00:00 2001 From: Pierre-antoine Comby Date: Mon, 25 May 2020 12:16:36 +0200 Subject: [PATCH] refactor of Guest and Entry import --- management/commands/import_activities.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/management/commands/import_activities.py b/management/commands/import_activities.py index d7b7c9e..840da02 100644 --- a/management/commands/import_activities.py +++ b/management/commands/import_activities.py @@ -22,10 +22,10 @@ class Command(ImportCommand): @timed @transaction.atomic - def import_activities(self, cur, chunk_size): + def import_activities(self, cur, chunk): cur.execute("SELECT * FROM activites ORDER by id") n = cur.rowcount - bulk_mgr = BulkCreateManager(chunk_size=chunk_size) + bulk_mgr = BulkCreateManager(chunk_size=chunk) activity_type_id = ActivityType.objects.get(name="Pot").pk # Need to be fixed manually kfet = Club.objects.get(name="Kfet") pk_activity = 1 @@ -64,8 +64,8 @@ class Command(ImportCommand): @timed @transaction.atomic - def import_activities_entries(self, cur): - bulk_mgr = BulkCreateManager() + def import_guest(self, cur, chunk): + bulk_mgr = BulkCreateManager(chunk_size=chunk) cur.execute("SELECT * FROM invites ORDER by id") n = cur.rowcount for idx, row in enumerate(cur): @@ -79,6 +79,11 @@ class Command(ImportCommand): } bulk_mgr.add(Guest(**obj_dict)) bulk_mgr.done() + + @timed + @transaction.atomic + def import_activities_entries(self, cur, chunk): + bulk_mgr = BulkCreateManager(chunk_size=chunk) cur.execute("SELECT * FROM entree_activites ORDER by id") n = cur.rowcount for idx, row in enumerate(cur): @@ -86,8 +91,8 @@ class Command(ImportCommand): obj_dict = { "activity_id": MAP_ACTIVITY[row["activite"]], "time": make_aware(row["heure_entree"]), - "note_id": self.MAP_IDBDE[row["responsable"]] if row['est_invite'] else row["idbde"], - "guest_id": row["idbde"] if row['est_invite'] else None, + "note_id": self.MAP_IDBDE[row["responsable"] if row['est_invite'] else row["idbde"]], + "guest_id": self.MAP_IDBDE[row["idbde"]] if row['est_invite'] else None, } bulk_mgr.add(Entry(**obj_dict)) bulk_mgr.done() @@ -102,4 +107,5 @@ class Command(ImportCommand): if kwargs["map"]: self.load_map(kwargs["map"]) self.import_activities(cur, kwargs["chunk"]) - self.import_activities_entries(cur) + self.import_guest(cur, kwargs["chunk"]) + self.import_activities_entries(cur, kwargs["chunk"])