From d6e9097d8003a31a0f4d2b4acd6ac9afd8057813 Mon Sep 17 00:00:00 2001 From: Pierre-antoine Comby Date: Sat, 2 May 2020 16:13:58 +0200 Subject: [PATCH] Import membership with transactions --- management/commands/import_nk15.py | 66 ++++++++++++++---------------- 1 file changed, 30 insertions(+), 36 deletions(-) diff --git a/management/commands/import_nk15.py b/management/commands/import_nk15.py index e667f04..af7d391 100644 --- a/management/commands/import_nk15.py +++ b/management/commands/import_nk15.py @@ -220,13 +220,41 @@ def import_transaction(cur): obj_dict["first_name"] = actor.club.name obj_dict["last_name"] = actor.club.name else: - raise Exception("You should'nt be there") + raise Exception("Badly formatted Special Transaction You should'nt be there.") tr = SpecialTransaction.objects.create(**obj_dict) if "cheques" in row["description"]: MAP_IDSPECIALTRANSACTION[row["id"]] = tr elif ttype == "adhésion": + montant = row["montant"] + m = re.search("Adhésion (\d\d\d\d)( \+ WEI)?", row["description"]) + year = m.group(1) + wei = m.group(2) + if montant == 0: # old format for SoGé registration (before ) + montant = 400 + # Since BDE and Kfet are distinct, don't import membership transaction and use our custom transactions. - pass + bde_dict = { + "user": MAP_IDBDE[row["idbde"]], + "club": bde, + "date_start": row["date"].date(), # Only date, not time + "fee": 500, + } + kfet_dict = { + "user": MAP_IDBDE[row["idbde"]], + "club": kfet, + "date_start": row["date"].date(), # Only date, not time + "fee": montant - 500 + } + try: + with transaction.atomic(): + bde_membership = Membership.objects.get_or_create(**bde_dict) + kfet_membership = Membership.objects.get_or_create(**kfet_dict) + bde_membership.transaction.created_at = row["date"] + bde_membership.transaction.save() + kfet_membership.transaction.created_at = row["date"] + kfet_membership.transaction.save() + except IntegrityError as e: + raise e elif ttype == "invitation": m = re.search("Invitation (.*?) \((.*?)\)", row["description"]) if m is None: @@ -370,37 +398,6 @@ def import_activity_entries(cur): raise e -@transaction.atomic -def import_memberships(cur): - cur.execute("SELECT * FROM adhesions ORDER by id") - n = cur.rowcount - bde = Club.objects.get(name="BDE") - kfet = Club.objects.get(name="Kfet") - for idx, row in enumerate(cur): - update_line(idx, n, MAP_IDBDE[row["idbde"]].username) - bde_dict = { - "user": MAP_IDBDE[row["idbde"]], - "club": bde, - "date_start": row["date"][10:], # Only date, not time - "fee": 500, - } - kfet_dict = { - "user": MAP_IDBDE[row["idbde"]], - "club": kfet, - "date_start": row["date"][:10], # Only date, not time - "fee": 1500 if row["date"].month in [3, 4, 5, 6, 7] else 3500, - } - try: - with transaction.atomic(): - bde_membership = Membership.objects.get_or_create(**bde_dict) - kfet_membership = Membership.objects.get_or_create(**kfet_dict) - bde_membership.transaction.created_at = row["date"] - bde_membership.transaction.save() - kfet_membership.transaction.created_at = row["date"] - kfet_membership.transaction.save() - except IntegrityError as e: - raise e - @transaction.atomic def import_remittances(cur): @@ -507,9 +504,6 @@ class Command(BaseCommand): if kwargs["transactions"]: import_transaction(cur) self.print_success("transaction imported\n") - if kwargs["memberships"]: - import_memberships(cur) - self.print_success("memberships imported\n") if kwargs["remittances"]: import_remittances(cur) self.print_success("remittances imported\n")