Import membership with transactions

This commit is contained in:
Pierre-antoine Comby 2020-05-02 16:13:58 +02:00
parent 6f0bfb175b
commit d6e9097d80
1 changed files with 30 additions and 36 deletions

View File

@ -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")