Compare commits

..

No commits in common. "fc29147c876c33d4cf41a86d46d736ff69d176ff" and "03dc6f98c8cb8eb3879914366eca89a23b9a644a" have entirely different histories.

1 changed files with 22 additions and 37 deletions

View File

@ -67,8 +67,6 @@ class Command(ImportCommand):
def add_arguments(self, parser): def add_arguments(self, parser):
parser.add_argument('-b', '--buttons', action='store_true', help="import buttons") parser.add_argument('-b', '--buttons', action='store_true', help="import buttons")
parser.add_argument('-t', '--transactions', action='store', default=0, help="start id for transaction import") parser.add_argument('-t', '--transactions', action='store', default=0, help="start id for transaction import")
parser.add_argument('-n', '--nosave', action='store_true', default=False, help="Scan only transactions, "
"don't save them")
@timed @timed
def import_buttons(self, cur, chunk_size, import_buttons): def import_buttons(self, cur, chunk_size, import_buttons):
@ -171,7 +169,7 @@ class Command(ImportCommand):
@timed @timed
@transaction.atomic @transaction.atomic
def import_transaction(self, cur, chunk_size, idmin, save=True): def import_transaction(self, cur, chunk_size, idmin):
bulk_mgr = BulkCreateManager(chunk_size=chunk_size) bulk_mgr = BulkCreateManager(chunk_size=chunk_size)
cur.execute( cur.execute(
f"SELECT t.id, t.date AS transac_date, t.type, t.emetteur,\ f"SELECT t.id, t.date AS transac_date, t.type, t.emetteur,\
@ -186,19 +184,10 @@ class Command(ImportCommand):
pk_membership = 1 pk_membership = 1
pk_transaction = 1 pk_transaction = 1
for idx, row in enumerate(cur): for idx, row in enumerate(cur):
if save or idx % chunk_size == 0: self.update_line(idx, n, row["description"])
self.update_line(idx, n, row["description"])
MAP_TRANSACTION[row["id"]] = pk_transaction MAP_TRANSACTION[row["id"]] = pk_transaction
if not save:
pk_transaction += 1
if row["valide"] and (row["type"] == "adhésion" or row["description"].lower() == "inscription"):
note = Note.objects.get(pk=self.MAP_IDBDE[row["emetteur"]])
if not isinstance(note, NoteClub):
pk_transaction += 1
continue
try: try:
date = make_aware(row["transac_date"]) date = make_aware(row["transac_date"])
except (pytz.NonExistentTimeError, pytz.AmbiguousTimeError): except (pytz.NonExistentTimeError, pytz.AmbiguousTimeError):
@ -228,7 +217,7 @@ class Command(ImportCommand):
if row["valide"] and (ttype == "adhésion" or row["description"].lower() == "inscription"): if row["valide"] and (ttype == "adhésion" or row["description"].lower() == "inscription"):
note = Note.objects.get(pk=obj_dict["source_id"]) note = Note.objects.get(pk=obj_dict["source_id"])
if isinstance(note, NoteClub): if isinstance(note, NoteClub):
child_transaction = None # don't bother register clubs child_transaction = None # don't bother register clubs
else: else:
user_id = note.user_id user_id = note.user_id
montant = obj_dict["amount"] montant = obj_dict["amount"]
@ -291,20 +280,6 @@ class Command(ImportCommand):
pk_transaction += 1 pk_transaction += 1
bulk_mgr.done() bulk_mgr.done()
@timed
def set_roles(self):
bulk_mgr = BulkCreateManager(chunk_size=10000)
bde_membership_ids = Membership.objects.filter(club__pk=BDE_PK).values_list('id', flat=True)
kfet_membership_ids = Membership.objects.filter(club__pk=KFET_PK).values_list('id', flat=True)
n = len(bde_membership_ids)
for idx, (m_bde_id, m_kfet_id) in enumerate(zip(bde_membership_ids, kfet_membership_ids)):
self.update_line(idx, n, str(idx))
bulk_mgr.add(
Membership.roles.through(membership_id=m_bde_id, role_id=BDE_ROLE_PK),
Membership.roles.through(membership_id=m_kfet_id, role_id=KFET_ROLE_PK),
)
bulk_mgr.done()
@timed @timed
@transaction.atomic @transaction.atomic
def import_remittances(self, cur, chunk_size): def import_remittances(self, cur, chunk_size):
@ -320,7 +295,7 @@ class Command(ImportCommand):
remittance_dict = { remittance_dict = {
"pk": pk_remittance, "pk": pk_remittance,
"date": make_aware(row["date"]), "date": make_aware(row["date"][:19]),
"remittance_type_id": 1, # Only Bank checks are supported in NK15 "remittance_type_id": 1, # Only Bank checks are supported in NK15
"comment": row["commentaire"], "comment": row["commentaire"],
"closed": row["close"], "closed": row["close"],
@ -343,7 +318,7 @@ class Command(ImportCommand):
if not row["idremise"]: if not row["idremise"]:
continue continue
tr = SpecialTransactionProxy.objects.get_or_create(transaction_id=MAP_TRANSACTION[row["idtransaction"]])[0] tr = SpecialTransactionProxy.objects.get(transaction__id=MAP_TRANSACTION[row["idtransaction"]])
tr.remittance_id = MAP_REMITTANCE[row["idremise"]] tr.remittance_id = MAP_REMITTANCE[row["idremise"]]
tr.save() tr.save()
@ -351,10 +326,21 @@ class Command(ImportCommand):
tr.last_name = row["nom"] tr.last_name = row["nom"]
tr.first_name = row["prenom"] tr.first_name = row["prenom"]
tr.bank = row["banque"] tr.bank = row["banque"]
try: tr.save()
tr.save()
except: @timed
print("Failed to save row: " + str(row)) def set_roles(self):
bulk_mgr = BulkCreateManager(chunk_size=10000)
bde_membership_ids = Membership.objects.filter(club__pk=BDE_PK).values_list('id', flat=True)
kfet_membership_ids = Membership.objects.filter(club__pk=KFET_PK).values_list('id', flat=True)
n = len(bde_membership_ids)
for idx, (m_bde_id, m_kfet_id) in enumerate(zip(bde_membership_ids, kfet_membership_ids)):
self.update_line(idx, n, str(idx))
bulk_mgr.add(
Membership.roles.through(membership_id=m_bde_id, role_id=BDE_ROLE_PK),
Membership.roles.through(membership_id=m_kfet_id, role_id=KFET_ROLE_PK),
)
bulk_mgr.done()
@timed @timed
def handle(self, *args, **kwargs): def handle(self, *args, **kwargs):
@ -367,8 +353,7 @@ class Command(ImportCommand):
if kwargs["map"]: if kwargs["map"]:
self.load_map(kwargs["map"]) self.load_map(kwargs["map"])
self.import_buttons(cur, kwargs["chunk"], kwargs["buttons"]) self.import_buttons(cur, kwargs["chunk"], kwargs["buttons"])
self.import_transaction(cur, kwargs["chunk"], kwargs["transactions"], not kwargs["nosave"]) self.import_transaction(cur, kwargs["chunk"], kwargs["transactions"])
if not kwargs["nosave"]: self.set_roles()
self.set_roles()
self.import_remittances(cur, kwargs["chunk"]) self.import_remittances(cur, kwargs["chunk"])
self.import_checks(cur) self.import_checks(cur)