refactor of Guest and Entry import

This commit is contained in:
Pierre-antoine Comby 2020-05-25 12:16:36 +02:00
parent 2996c9702f
commit 72a752fd56
1 changed files with 13 additions and 7 deletions

View File

@ -22,10 +22,10 @@ class Command(ImportCommand):
@timed @timed
@transaction.atomic @transaction.atomic
def import_activities(self, cur, chunk_size): def import_activities(self, cur, chunk):
cur.execute("SELECT * FROM activites ORDER by id") cur.execute("SELECT * FROM activites ORDER by id")
n = cur.rowcount 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 activity_type_id = ActivityType.objects.get(name="Pot").pk # Need to be fixed manually
kfet = Club.objects.get(name="Kfet") kfet = Club.objects.get(name="Kfet")
pk_activity = 1 pk_activity = 1
@ -64,8 +64,8 @@ class Command(ImportCommand):
@timed @timed
@transaction.atomic @transaction.atomic
def import_activities_entries(self, cur): def import_guest(self, cur, chunk):
bulk_mgr = BulkCreateManager() bulk_mgr = BulkCreateManager(chunk_size=chunk)
cur.execute("SELECT * FROM invites ORDER by id") cur.execute("SELECT * FROM invites ORDER by id")
n = cur.rowcount n = cur.rowcount
for idx, row in enumerate(cur): for idx, row in enumerate(cur):
@ -79,6 +79,11 @@ class Command(ImportCommand):
} }
bulk_mgr.add(Guest(**obj_dict)) bulk_mgr.add(Guest(**obj_dict))
bulk_mgr.done() 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") cur.execute("SELECT * FROM entree_activites ORDER by id")
n = cur.rowcount n = cur.rowcount
for idx, row in enumerate(cur): for idx, row in enumerate(cur):
@ -86,8 +91,8 @@ class Command(ImportCommand):
obj_dict = { obj_dict = {
"activity_id": MAP_ACTIVITY[row["activite"]], "activity_id": MAP_ACTIVITY[row["activite"]],
"time": make_aware(row["heure_entree"]), "time": make_aware(row["heure_entree"]),
"note_id": self.MAP_IDBDE[row["responsable"]] if row['est_invite'] else row["idbde"], "note_id": self.MAP_IDBDE[row["responsable"] if row['est_invite'] else row["idbde"]],
"guest_id": row["idbde"] if row['est_invite'] else None, "guest_id": self.MAP_IDBDE[row["idbde"]] if row['est_invite'] else None,
} }
bulk_mgr.add(Entry(**obj_dict)) bulk_mgr.add(Entry(**obj_dict))
bulk_mgr.done() bulk_mgr.done()
@ -102,4 +107,5 @@ class Command(ImportCommand):
if kwargs["map"]: if kwargs["map"]:
self.load_map(kwargs["map"]) self.load_map(kwargs["map"])
self.import_activities(cur, kwargs["chunk"]) self.import_activities(cur, kwargs["chunk"])
self.import_activities_entries(cur) self.import_guest(cur, kwargs["chunk"])
self.import_activities_entries(cur, kwargs["chunk"])