From fa0ac5899994cf388631a733b68b883ae29b4d7d Mon Sep 17 00:00:00 2001 From: Pierre-antoine Comby Date: Sat, 11 Apr 2020 00:33:04 +0200 Subject: [PATCH] better print --- management/commands/import_nk15.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/management/commands/import_nk15.py b/management/commands/import_nk15.py index 40be259..27a11f9 100644 --- a/management/commands/import_nk15.py +++ b/management/commands/import_nk15.py @@ -47,7 +47,7 @@ def update_line(n,N, content): n = str(n) N = str(N) n.rjust(len(N)) - print(f"({n}/{N}) {content:10.10}", end="\r") + print(f"\r ({n}/{N}) {content:10.10}",end="") @transaction.atomic def import_comptes(cur): @@ -152,15 +152,21 @@ def import_boutons(cur): @transaction.atomic def import_transaction(cur): - cur.execute("SELECT * FROM transactions LEFT JOIN adhesions ON transactions.id = adhesions.idtransaction ORDER BY transaction.id;") + cur.execute("SELECT * FROM transactions LEFT JOIN adhesions ON transactions.id = adhesions.idtransaction ORDER BY transactions.id;") N = cur.rowcount for idx, row in enumerate(cur): - update_line(idx,N,row["label"]) + update_line(idx,N,row["description"]) + # some date are set to None, use the previous one + date = row["date"] + if date is None: + date = old_date + else: + old_date = date obj_dict = { # "pk": row["id"], "destination_id" : MAP_IDBDE[row["destinataire"]], "source_id": MAP_IDBDE[row["emetteur"]], - "created_at":make_aware(row["date"]), + "created_at":make_aware(date), "amount":row["montant"], "quantity":row["quantite"], "reason":row["description"], @@ -251,24 +257,25 @@ class Command(BaseCommand): def handle(self, *args, **kwargs): global MAP_IDBDE nk15db, nk15user = kwargs['nk15db'], kwargs['nk15user'] - #reset database. - call_command("migrate") - call_command("loaddata","initial") - self.print_success("reset nk20 database") # connecting to nk15 database conn = pg.connect(database=nk15db,user=nk15user) cur = conn.cursor(cursor_factory = pge.DictCursor) if kwargs["comptes"]: + #reset database. + call_command("migrate") + call_command("loaddata","initial") + self.print_success("reset nk20 database") import_comptes(cur) self.print_success("comptes table imported") elif kwargs["map"]: filename = kwargs["map"] - with open(filename,'w') as fp: + with open(filename,'r') as fp: MAP_IDBDE = json.load(fp) + MAP_IDBDE = {int(k):int(v) for k,v in MAP_IDBDE.items()} if kwargs["save"]: filename = kwargs["save"] - with open(filename,'r') as fp: + with open(filename,'w') as fp: json.dump(MAP_IDBDE,fp,sort_keys=True, indent=2) # /!\ need a prober MAP_IDBDE