From f9bac69aad32cba4e4acfe987a96a23472bf928e Mon Sep 17 00:00:00 2001 From: Pierre-antoine Comby Date: Sun, 10 May 2020 14:57:36 +0200 Subject: [PATCH] pretty code and f-strings --- management/commands/import_nk15.py | 49 ++++++++++++++++++------------ 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/management/commands/import_nk15.py b/management/commands/import_nk15.py index 21e1e44..882f3e8 100644 --- a/management/commands/import_nk15.py +++ b/management/commands/import_nk15.py @@ -19,8 +19,14 @@ from django.contrib.auth.models import User from activity.models import ActivityType, Activity, Guest, Entry, GuestTransaction from note.models import Note from note.models import Alias -from note.models import TemplateCategory, TransactionTemplate, \ - Transaction, RecurrentTransaction, SpecialTransaction +from note.models import ( + TemplateCategory, + TransactionTemplate, + Transaction, + RecurrentTransaction, + MembershipTransaction, + SpecialTransaction, +) from member.models import Club, Membership from treasury.models import RemittanceType, Remittance, SpecialTransactionProxy @@ -152,7 +158,7 @@ def import_boutons(cur): qs = Club.objects.filter(note__note_ptr=MAP_IDBDE[row["destinataire"]]).values('name') note_name = qs[0]["name"] # rename button name - obj_dict["name"] = "{} {}".format(obj_dict["name"], note_name) + obj_dict["name"] = f"{obj_dict_name['name']} {note_name}" button = TransactionTemplate.objects.create(**obj_dict) else: raise e @@ -164,20 +170,25 @@ def import_transaction(cur): idmin = 58770 bde = Club.objects.get(name="BDE") kfet = Club.objects.get(name="Kfet") - cur.execute("SELECT t.date AS transac_date, t.type, t.emetteur, t.destinataire, t.quantite, t.montant, t.description, t.valide, t.cantinvalidate, t.categorie\ - a.idbde,a.annee, a.wei, a.date AS adh_date, a.section - FROM transactions AS t\ - LEFT JOIN adhesions AS a ON t.id = a.idtransaction\ - WHERE transactions.id> {}\ - ORDER BY transactions.id;".format(idmin)) + cur.execute( + "SELECT t.date AS transac_date, t.type, t.emetteur,\ + t.destinataire,t.quantite, t.montant, t.description,\ + t.valide, t.cantinvalidate, t.categorie \ + a.idbde, a.annee, a.wei, a.date AS adh_date, a.section\ + FROM transactions AS t \ + LEFT JOIN adhesions AS a ON t.id = a.idtransaction \ + WHERE transactions.id> {} \ + ORDER BY transactions.id;".format(idmin) + ) n = cur.rowcount for idx, row in enumerate(cur): update_line(idx, n, row["description"]) try: date = make_aware(row["transac_date"]) except (pytz.NonExistentTimeError, pytz.AmbiguousTimeError): - date = make_aware(row["transac_date"] + datetime.timedelta(hours=1) - + date = make_aware(row["transac_date"] + datetime.timedelta(hours=1)) + + # standart transaction object obj_dict = { # "pk": row["id"], "destination_id": MAP_IDBDE[row["destinataire"]], @@ -256,20 +267,19 @@ def import_transaction(cur): except IntegrityError as e: raise e elif ttype == "invitation": - m = re.search("Invitation (.*?) \((.*?)\)", row["description"]) + m = re.search(r"Invitation (.*?) \((.*?)\)", row["description"]) if m is None: - raise IntegrityError("Invitation is not well formated: {} (must be 'Invitation ACTIVITY_NAME (NAME)')" - .format(row["description"])) + raise IntegrityError(f"Invitation is not well formated: {row['description']} (must be 'Invitation ACTIVITY_NAME (NAME)')") activity_name = m.group(1) guest_name = m.group(2) if activity_name not in MAP_NAMEACTIVITY: - raise IntegrityError("Activity {} is not found".format(activity_name,)) + raise IntegrityError(f"Activity {activity_name} is not found") activity = MAP_NAMEACTIVITY[activity_name] if guest_name not in MAP_NAMEGUEST: - raise IntegrityError("Guest {} is not found".format(guest_name,)) + raise IntegrityError(f"Guest {guest_name} is not found") guest = None for g in MAP_NAMEGUEST[guest_name]: @@ -277,7 +287,7 @@ def import_transaction(cur): guest = g break if guest is None: - raise IntegrityError("Guest {} didn't go to the activity {}".format(guest_name, activity_name,)) + raise IntegrityError("Guest {guest_name} didn't go to the activity {activity_name}") obj_dict["guest"] = guest @@ -297,7 +307,7 @@ def import_aliases(cur): obj_dict = { "note_id": MAP_IDBDE[row["idbde"]], "name": alias_name_good, - "normalized_name": Alias.normalize(alias_name_good) + "normalized_name": Alias.normalize(alias_name_good), } try: with transaction.atomic(): @@ -321,7 +331,7 @@ def import_activities(cur): update_line(idx, n, row["alias"]) organizer = Club.objects.filter(name=row["signature"]) if organizer.exists(): - # Try to find the club that organizes the activity. If not founded, assume that is Kfet (fix manually) + # Try to find the club that organizes the activity. If not founded, assume it's Kfet (fix manually) organizer = organizer.get() else: organizer = kfet @@ -398,7 +408,6 @@ def import_activity_entries(cur): raise e - @transaction.atomic def import_remittances(cur): cur.execute("SELECT * FROM remises ORDER by id")