From 60a6b3c704ffc9db7a9a569009da0c365ebfe261 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Mon, 27 Apr 2020 01:52:06 +0200 Subject: [PATCH] Import memberships --- management/commands/import_nk15.py | 52 ++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/management/commands/import_nk15.py b/management/commands/import_nk15.py index 31d7246..1d60a2f 100644 --- a/management/commands/import_nk15.py +++ b/management/commands/import_nk15.py @@ -1,15 +1,14 @@ #!/usr/env/bin python3 +import json +import datetime +import re + from django.core.management.base import BaseCommand from django.core.management import call_command import psycopg2 as pg import psycopg2.extras as pge from django.db import transaction - -import json -import datetime -import re - from django.core.exceptions import ValidationError from django.utils.timezone import make_aware from django.db import IntegrityError @@ -19,7 +18,7 @@ from note.models import Note from note.models import Alias from note.models import TemplateCategory, TransactionTemplate, \ Transaction, RecurrentTransaction, SpecialTransaction -from member.models import Club +from member.models import Club, Membership """ Script d'import de la nk15: @@ -213,7 +212,8 @@ def import_transaction(cur): raise Exception("You should'nt be there") SpecialTransaction.objects.create(**obj_dict) elif ttype == "adhésion": - print("adhesion not supported yet") + # Since BDE and Kfet are distinct, don't import membership transaction and use our custom transactions. + pass elif ttype == "invitation": m = re.search("Invitation (.*?) \((.*?)\)", row["description"]) if m is None: @@ -357,6 +357,38 @@ 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 + + class Command(BaseCommand): """ Command for importing the database of NK15. @@ -372,6 +404,7 @@ class Command(BaseCommand): parser.add_argument('-t', '--transactions', action='store_true', help="import transaction") parser.add_argument('-al', '--aliases', action='store_true', help="import aliases") parser.add_argument('-ac', '--activities', action='store_true', help="import activities") + parser.add_argument('-M', '--memberships', action='store_true', help="import memberships") parser.add_argument('-s', '--save', action='store', help="save mapping of idbde") parser.add_argument('-m', '--map', action='store', help="import mapping of idbde") parser.add_argument('-d', '--nk15db', action='store', default='nk15', help='NK15 database name') @@ -408,9 +441,14 @@ class Command(BaseCommand): if kwargs["activities"]: import_activities(cur) self.print_success("activities imported\n") + import_activity_entries(cur) + self.print_success("activity entries imported\n") if kwargs["aliases"]: import_aliases(cur) self.print_success("aliases imported\n") if kwargs["transactions"]: import_transaction(cur) self.print_success("transaction imported\n") + if kwargs["memberships"]: + import_memberships(cur) + self.print_success("memberships imported\n")