2020-02-24 13:19:40 +00:00
|
|
|
#!/usr/env/bin python3
|
|
|
|
|
2020-04-26 23:52:06 +00:00
|
|
|
import json
|
|
|
|
import datetime
|
|
|
|
import re
|
2020-05-02 14:08:40 +00:00
|
|
|
import pytz
|
2020-04-26 23:52:06 +00:00
|
|
|
|
2020-04-26 23:19:56 +00:00
|
|
|
import psycopg2 as pg
|
2020-02-24 13:19:40 +00:00
|
|
|
import psycopg2.extras as pge
|
2020-05-02 14:08:40 +00:00
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from django.core.management import call_command
|
2020-02-24 13:19:40 +00:00
|
|
|
from django.db import transaction
|
|
|
|
from django.core.exceptions import ValidationError
|
2020-02-25 13:18:39 +00:00
|
|
|
from django.utils.timezone import make_aware
|
2020-02-24 13:19:40 +00:00
|
|
|
from django.db import IntegrityError
|
|
|
|
from django.contrib.auth.models import User
|
2020-05-02 14:08:40 +00:00
|
|
|
|
2020-04-26 23:19:56 +00:00
|
|
|
from activity.models import ActivityType, Activity, Guest, Entry, GuestTransaction
|
|
|
|
from note.models import Note
|
2020-02-24 13:19:40 +00:00
|
|
|
from note.models import Alias
|
2020-05-10 12:57:36 +00:00
|
|
|
from note.models import (
|
|
|
|
TemplateCategory,
|
|
|
|
TransactionTemplate,
|
|
|
|
Transaction,
|
|
|
|
RecurrentTransaction,
|
|
|
|
MembershipTransaction,
|
|
|
|
SpecialTransaction,
|
|
|
|
)
|
2020-04-26 23:52:06 +00:00
|
|
|
from member.models import Club, Membership
|
2020-04-27 00:08:01 +00:00
|
|
|
from treasury.models import RemittanceType, Remittance, SpecialTransactionProxy
|
2020-02-24 13:19:40 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
Script d'import de la nk15:
|
|
|
|
TODO: import transactions
|
|
|
|
TODO: import adhesion
|
|
|
|
TODO: import activite
|
2020-02-25 13:19:34 +00:00
|
|
|
TODO: import ...
|
2020-02-24 13:19:40 +00:00
|
|
|
|
|
|
|
"""
|
2020-04-10 16:13:26 +00:00
|
|
|
M_DURATION = 396
|
2020-04-26 23:19:56 +00:00
|
|
|
M_START = datetime.date(2019, 8, 31)
|
|
|
|
M_END = datetime.date(2020, 9, 30)
|
2020-02-24 13:19:40 +00:00
|
|
|
|
2020-04-26 23:19:56 +00:00
|
|
|
MAP_IDBDE = {
|
|
|
|
-4: 2, # Carte Bancaire
|
|
|
|
-3: 4, # Virement
|
|
|
|
-2: 1, # Especes
|
|
|
|
-1: 3, # Chèque
|
|
|
|
0: 5, # BDE
|
2020-04-10 16:13:26 +00:00
|
|
|
}
|
2020-02-24 13:19:40 +00:00
|
|
|
|
2020-04-26 23:19:56 +00:00
|
|
|
MAP_IDACTIVITY = {}
|
|
|
|
MAP_NAMEACTIVITY = {}
|
|
|
|
MAP_NAMEGUEST = {}
|
2020-04-27 00:08:01 +00:00
|
|
|
MAP_IDSPECIALTRANSACTION = {}
|
2020-04-26 23:19:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
def update_line(n, total, content):
|
2020-04-10 16:13:26 +00:00
|
|
|
n = str(n)
|
2020-04-26 23:19:56 +00:00
|
|
|
total = str(total)
|
|
|
|
n.rjust(len(total))
|
|
|
|
print(f"\r ({n}/{total}) {content:10.10}", end="")
|
|
|
|
|
2020-02-24 13:19:40 +00:00
|
|
|
|
|
|
|
@transaction.atomic
|
2020-04-10 16:13:26 +00:00
|
|
|
def import_comptes(cur):
|
2020-02-24 13:19:40 +00:00
|
|
|
cur.execute("SELECT * FROM comptes WHERE idbde > 0 ORDER BY idbde;")
|
|
|
|
pkclub = 3
|
2020-04-26 23:19:56 +00:00
|
|
|
n = cur.rowcount
|
2020-04-10 16:13:26 +00:00
|
|
|
for idx, row in enumerate(cur):
|
2020-04-26 23:19:56 +00:00
|
|
|
update_line(idx, n, row["pseudo"])
|
2020-02-24 13:19:40 +00:00
|
|
|
if row["type"] == "personne":
|
2020-04-26 23:19:56 +00:00
|
|
|
# sanitize password
|
2020-05-10 12:48:14 +00:00
|
|
|
if row["passwd"] != "*|*" and not row["deleted"]:
|
2020-04-26 23:19:56 +00:00
|
|
|
passwd_nk15 = "$".join(["custom_nk15", "1", row["passwd"]])
|
2020-02-24 13:19:40 +00:00
|
|
|
else:
|
|
|
|
passwd_nk15 = ''
|
|
|
|
try:
|
|
|
|
obj_dict = {
|
|
|
|
"username": row["pseudo"],
|
|
|
|
"password": passwd_nk15,
|
|
|
|
"first_name": row["nom"],
|
|
|
|
"last_name": row["prenom"],
|
2020-04-26 23:19:56 +00:00
|
|
|
"email": row["mail"],
|
|
|
|
"is_active": True, # temporary
|
2020-02-24 13:19:40 +00:00
|
|
|
}
|
2020-04-10 16:13:26 +00:00
|
|
|
|
2020-02-24 13:19:40 +00:00
|
|
|
user = User.objects.create(**obj_dict)
|
2020-04-10 16:13:26 +00:00
|
|
|
profile = user.profile
|
|
|
|
profile.phone_number = row['tel']
|
|
|
|
profile.address = row['adresse']
|
|
|
|
profile.paid = row['normalien']
|
|
|
|
profile.registration_valid = True
|
|
|
|
profile.email_confirmed = True
|
|
|
|
user.save()
|
|
|
|
profile.save()
|
|
|
|
# sanitize duplicate aliases (nk12)
|
2020-02-24 13:19:40 +00:00
|
|
|
except ValidationError as e:
|
|
|
|
if e.code == 'same_alias':
|
2020-04-26 23:19:56 +00:00
|
|
|
user.username = row["pseudo"] + str(row["idbde"])
|
2020-04-10 16:13:26 +00:00
|
|
|
user.save()
|
2020-02-24 13:19:40 +00:00
|
|
|
else:
|
2020-04-26 23:19:56 +00:00
|
|
|
raise e
|
2020-03-09 18:00:19 +00:00
|
|
|
# profile and note created via signal.
|
2020-04-26 23:19:56 +00:00
|
|
|
|
2020-02-24 13:19:40 +00:00
|
|
|
note = user.note
|
2020-04-26 23:19:56 +00:00
|
|
|
date = row.get("last_negatif", None)
|
|
|
|
if date is not None:
|
2020-02-25 13:18:39 +00:00
|
|
|
note.last_negative = make_aware(date)
|
2020-02-24 13:19:40 +00:00
|
|
|
note.balance = row["solde"]
|
2020-04-10 16:13:26 +00:00
|
|
|
note.save()
|
2020-04-26 23:19:56 +00:00
|
|
|
else: # club
|
2020-02-24 13:19:40 +00:00
|
|
|
obj_dict = {
|
2020-04-26 23:19:56 +00:00
|
|
|
"pk": pkclub,
|
2020-02-24 13:19:40 +00:00
|
|
|
"name": row["pseudo"],
|
|
|
|
"email": row["mail"],
|
2020-03-09 18:00:19 +00:00
|
|
|
"membership_duration": M_DURATION,
|
|
|
|
"membership_start": M_START,
|
|
|
|
"membership_end": M_END,
|
2020-04-10 16:13:26 +00:00
|
|
|
"membership_fee_paid": 0,
|
2020-04-26 23:19:56 +00:00
|
|
|
"membership_fee_unpaid": 0,
|
2020-02-24 13:19:40 +00:00
|
|
|
}
|
2020-04-26 23:19:56 +00:00
|
|
|
club, c = Club.objects.get_or_create(**obj_dict)
|
|
|
|
pkclub += 1
|
2020-02-24 13:19:40 +00:00
|
|
|
note = club.note
|
|
|
|
note.balance = row["solde"]
|
2020-04-10 16:13:26 +00:00
|
|
|
club.save()
|
|
|
|
note.save()
|
2020-04-26 23:19:56 +00:00
|
|
|
|
2020-04-18 14:02:49 +00:00
|
|
|
MAP_IDBDE[row["idbde"]] = note.note_ptr_id
|
2020-02-24 13:19:40 +00:00
|
|
|
|
2020-04-26 23:19:56 +00:00
|
|
|
|
2020-02-24 13:19:40 +00:00
|
|
|
@transaction.atomic
|
2020-04-10 16:13:26 +00:00
|
|
|
def import_boutons(cur):
|
2020-02-24 13:19:40 +00:00
|
|
|
cur.execute("SELECT * FROM boutons;")
|
2020-04-26 23:19:56 +00:00
|
|
|
n = cur.rowcount
|
2020-04-10 16:13:26 +00:00
|
|
|
for idx, row in enumerate(cur):
|
2020-04-26 23:19:56 +00:00
|
|
|
update_line(idx, n, row["label"])
|
2020-02-24 13:19:40 +00:00
|
|
|
cat, created = TemplateCategory.objects.get_or_create(name=row["categorie"])
|
2020-03-14 10:41:08 +00:00
|
|
|
if created:
|
|
|
|
cat.save()
|
2020-02-24 13:19:40 +00:00
|
|
|
obj_dict = {
|
|
|
|
"pk": row["id"],
|
|
|
|
"name": row["label"],
|
|
|
|
"amount": row["montant"],
|
2020-04-10 16:13:26 +00:00
|
|
|
"destination_id": MAP_IDBDE[row["destinataire"]],
|
2020-02-24 13:19:40 +00:00
|
|
|
"category": cat,
|
2020-04-26 23:19:56 +00:00
|
|
|
"display": row["affiche"],
|
2020-02-24 13:19:40 +00:00
|
|
|
"description": row["description"],
|
|
|
|
}
|
|
|
|
try:
|
2020-04-26 23:19:56 +00:00
|
|
|
with transaction.atomic(): # required for error management
|
2020-02-24 13:19:40 +00:00
|
|
|
button = TransactionTemplate.objects.create(**obj_dict)
|
|
|
|
except IntegrityError as e:
|
2020-03-14 10:41:08 +00:00
|
|
|
# button with the same name is not possible in NK20.
|
2020-02-24 13:19:40 +00:00
|
|
|
if "unique" in e.args[0]:
|
2020-04-18 14:02:49 +00:00
|
|
|
qs = Club.objects.filter(note__note_ptr=MAP_IDBDE[row["destinataire"]]).values('name')
|
2020-02-24 13:19:40 +00:00
|
|
|
note_name = qs[0]["name"]
|
2020-04-26 23:19:56 +00:00
|
|
|
# rename button name
|
2020-05-10 12:57:36 +00:00
|
|
|
obj_dict["name"] = f"{obj_dict_name['name']} {note_name}"
|
2020-02-24 13:19:40 +00:00
|
|
|
button = TransactionTemplate.objects.create(**obj_dict)
|
|
|
|
else:
|
2020-04-26 23:19:56 +00:00
|
|
|
raise e
|
2020-02-24 13:19:40 +00:00
|
|
|
button.save()
|
|
|
|
|
2020-04-26 23:19:56 +00:00
|
|
|
|
2020-02-24 13:19:40 +00:00
|
|
|
@transaction.atomic
|
2020-04-10 16:13:26 +00:00
|
|
|
def import_transaction(cur):
|
2020-04-26 23:19:56 +00:00
|
|
|
idmin = 58770
|
2020-05-02 14:08:40 +00:00
|
|
|
bde = Club.objects.get(name="BDE")
|
|
|
|
kfet = Club.objects.get(name="Kfet")
|
2020-05-10 12:57:36 +00:00
|
|
|
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)
|
|
|
|
)
|
2020-04-26 23:19:56 +00:00
|
|
|
n = cur.rowcount
|
2020-04-10 16:13:26 +00:00
|
|
|
for idx, row in enumerate(cur):
|
2020-04-26 23:19:56 +00:00
|
|
|
update_line(idx, n, row["description"])
|
2020-05-02 14:08:40 +00:00
|
|
|
try:
|
|
|
|
date = make_aware(row["transac_date"])
|
|
|
|
except (pytz.NonExistentTimeError, pytz.AmbiguousTimeError):
|
2020-05-10 12:57:36 +00:00
|
|
|
date = make_aware(row["transac_date"] + datetime.timedelta(hours=1))
|
|
|
|
|
|
|
|
# standart transaction object
|
2020-02-24 13:19:40 +00:00
|
|
|
obj_dict = {
|
2020-04-26 23:19:56 +00:00
|
|
|
# "pk": row["id"],
|
|
|
|
"destination_id": MAP_IDBDE[row["destinataire"]],
|
2020-04-10 16:13:26 +00:00
|
|
|
"source_id": MAP_IDBDE[row["emetteur"]],
|
2020-04-26 23:19:56 +00:00
|
|
|
"created_at": make_aware(date),
|
|
|
|
"amount": row["montant"],
|
|
|
|
"quantity": row["quantite"],
|
|
|
|
"reason": row["description"],
|
|
|
|
"valid": row["valide"],
|
2020-02-24 13:19:40 +00:00
|
|
|
}
|
2020-04-10 21:06:47 +00:00
|
|
|
ttype = row["type"]
|
2020-04-26 23:19:56 +00:00
|
|
|
if ttype == "don" or ttype == "transfert":
|
|
|
|
Transaction.objects.create(**obj_dict)
|
2020-04-10 21:06:47 +00:00
|
|
|
elif ttype == "bouton":
|
2020-02-25 13:19:34 +00:00
|
|
|
cat_name = row["categorie"]
|
2020-04-26 23:19:56 +00:00
|
|
|
if cat_name is None:
|
2020-02-25 13:19:34 +00:00
|
|
|
cat_name = 'None'
|
|
|
|
cat, created = TemplateCategory.objects.get_or_create(name=cat_name)
|
|
|
|
if created:
|
|
|
|
cat.save()
|
|
|
|
obj_dict["category"] = cat
|
2020-04-26 23:19:56 +00:00
|
|
|
RecurrentTransaction.objects.create(**obj_dict)
|
2020-04-10 21:06:47 +00:00
|
|
|
elif ttype == "crédit" or ttype == "retrait":
|
|
|
|
field_id = "source_id" if ttype == "crédit" else "destination_id"
|
|
|
|
if "espèce" in row["description"]:
|
|
|
|
obj_dict[field_id] = 1
|
|
|
|
elif "carte" in row["description"]:
|
|
|
|
obj_dict[field_id] = 2
|
|
|
|
elif "cheques" in row["description"]:
|
|
|
|
obj_dict[field_id] = 3
|
|
|
|
elif "virement" in row["description"]:
|
|
|
|
obj_dict[field_id] = 4
|
2020-04-18 14:03:37 +00:00
|
|
|
pk = max(row["destinataire"], row["emetteur"])
|
|
|
|
actor = Note.objects.get(id=MAP_IDBDE[pk])
|
2020-04-10 21:06:47 +00:00
|
|
|
# custom fields of SpecialTransaction
|
2020-04-18 14:03:37 +00:00
|
|
|
if actor.__class__.__name__ == "NoteUser":
|
|
|
|
obj_dict["first_name"] = actor.user.first_name
|
|
|
|
obj_dict["last_name"] = actor.user.last_name
|
|
|
|
elif actor.__class__.__name__ == "NoteClub":
|
|
|
|
obj_dict["first_name"] = actor.club.name
|
|
|
|
obj_dict["last_name"] = actor.club.name
|
2020-04-10 21:06:47 +00:00
|
|
|
else:
|
2020-05-02 14:13:58 +00:00
|
|
|
raise Exception("Badly formatted Special Transaction You should'nt be there.")
|
2020-04-27 00:08:01 +00:00
|
|
|
tr = SpecialTransaction.objects.create(**obj_dict)
|
|
|
|
if "cheques" in row["description"]:
|
|
|
|
MAP_IDSPECIALTRANSACTION[row["id"]] = tr
|
2020-05-10 12:59:54 +00:00
|
|
|
|
2020-04-10 21:06:47 +00:00
|
|
|
elif ttype == "adhésion":
|
2020-05-02 14:13:58 +00:00
|
|
|
montant = row["montant"]
|
|
|
|
|
2020-05-10 12:59:54 +00:00
|
|
|
# Create Double membership to Kfet and Bde
|
|
|
|
# sometimes montant = 0, fees are modified accordingly.
|
2020-05-02 14:13:58 +00:00
|
|
|
bde_dict = {
|
|
|
|
"user": MAP_IDBDE[row["idbde"]],
|
|
|
|
"club": bde,
|
|
|
|
"date_start": row["date"].date(), # Only date, not time
|
2020-05-10 12:59:54 +00:00
|
|
|
"fee": min(500, montant)
|
2020-05-02 14:13:58 +00:00
|
|
|
}
|
|
|
|
kfet_dict = {
|
|
|
|
"user": MAP_IDBDE[row["idbde"]],
|
|
|
|
"club": kfet,
|
|
|
|
"date_start": row["date"].date(), # Only date, not time
|
2020-05-10 12:59:54 +00:00
|
|
|
"fee": max(montant - 500, 0),
|
2020-05-02 14:13:58 +00:00
|
|
|
}
|
2020-05-10 12:59:54 +00:00
|
|
|
|
|
|
|
if row["valide"]:
|
2020-05-02 14:13:58 +00:00
|
|
|
with transaction.atomic():
|
2020-05-10 12:59:54 +00:00
|
|
|
# membership save triggers MembershipTransaction creation
|
2020-05-02 14:13:58 +00:00
|
|
|
bde_membership = Membership.objects.get_or_create(**bde_dict)
|
|
|
|
kfet_membership = Membership.objects.get_or_create(**kfet_dict)
|
2020-05-10 12:59:54 +00:00
|
|
|
bde_membership.transaction.created_at = row["transac_date"]
|
|
|
|
bde_membership.transaction.description = row["description"]
|
2020-05-02 14:13:58 +00:00
|
|
|
bde_membership.transaction.save()
|
2020-05-10 12:59:54 +00:00
|
|
|
kfet_membership.transaction.created_at = row["transac_date"]
|
|
|
|
kfet_membership.transaction.description = row["description"] + "(Kfet)"
|
2020-05-02 14:13:58 +00:00
|
|
|
kfet_membership.transaction.save()
|
2020-05-10 12:59:54 +00:00
|
|
|
else:
|
|
|
|
# don't create membership
|
|
|
|
MembershipTransaction.objects.create(**obj_dict)
|
2020-04-26 23:19:56 +00:00
|
|
|
elif ttype == "invitation":
|
2020-05-10 12:57:36 +00:00
|
|
|
m = re.search(r"Invitation (.*?) \((.*?)\)", row["description"])
|
2020-04-26 23:19:56 +00:00
|
|
|
if m is None:
|
2020-05-10 12:57:36 +00:00
|
|
|
raise IntegrityError(f"Invitation is not well formated: {row['description']} (must be 'Invitation ACTIVITY_NAME (NAME)')")
|
2020-04-26 23:19:56 +00:00
|
|
|
|
|
|
|
activity_name = m.group(1)
|
|
|
|
guest_name = m.group(2)
|
|
|
|
|
|
|
|
if activity_name not in MAP_NAMEACTIVITY:
|
2020-05-10 12:57:36 +00:00
|
|
|
raise IntegrityError(f"Activity {activity_name} is not found")
|
2020-04-26 23:19:56 +00:00
|
|
|
activity = MAP_NAMEACTIVITY[activity_name]
|
|
|
|
|
|
|
|
if guest_name not in MAP_NAMEGUEST:
|
2020-05-10 12:57:36 +00:00
|
|
|
raise IntegrityError(f"Guest {guest_name} is not found")
|
2020-04-26 23:19:56 +00:00
|
|
|
|
|
|
|
guest = None
|
|
|
|
for g in MAP_NAMEGUEST[guest_name]:
|
|
|
|
if g.activity.pk == activity.pk:
|
|
|
|
guest = g
|
|
|
|
break
|
|
|
|
if guest is None:
|
2020-05-10 12:57:36 +00:00
|
|
|
raise IntegrityError("Guest {guest_name} didn't go to the activity {activity_name}")
|
2020-04-26 23:19:56 +00:00
|
|
|
|
|
|
|
obj_dict["guest"] = guest
|
|
|
|
|
|
|
|
GuestTransaction.objects.get_or_create(**obj_dict)
|
2020-02-25 13:19:34 +00:00
|
|
|
else:
|
2020-04-26 23:19:56 +00:00
|
|
|
print("other type not supported yet:", ttype)
|
|
|
|
|
2020-04-10 16:13:26 +00:00
|
|
|
|
2020-02-24 13:19:40 +00:00
|
|
|
@transaction.atomic
|
2020-04-10 16:13:26 +00:00
|
|
|
def import_aliases(cur):
|
2020-02-24 13:19:40 +00:00
|
|
|
cur.execute("SELECT * FROM aliases ORDER by id")
|
2020-04-26 23:19:56 +00:00
|
|
|
n = cur.rowcount
|
2020-04-10 16:13:26 +00:00
|
|
|
for idx, row in enumerate(cur):
|
2020-04-26 23:19:56 +00:00
|
|
|
update_line(idx, n, row["titre"])
|
2020-02-24 13:19:40 +00:00
|
|
|
alias_name = row["alias"]
|
2020-04-26 23:19:56 +00:00
|
|
|
alias_name_good = (alias_name[:252] + '...') if len(alias_name) > 255 else alias_name
|
2020-02-24 13:19:40 +00:00
|
|
|
obj_dict = {
|
2020-04-26 23:19:56 +00:00
|
|
|
"note_id": MAP_IDBDE[row["idbde"]],
|
|
|
|
"name": alias_name_good,
|
2020-05-10 12:57:36 +00:00
|
|
|
"normalized_name": Alias.normalize(alias_name_good),
|
2020-02-24 13:19:40 +00:00
|
|
|
}
|
|
|
|
try:
|
|
|
|
with transaction.atomic():
|
2020-04-26 23:19:56 +00:00
|
|
|
alias, created = Alias.objects.get_or_create(**obj_dict)
|
2020-04-10 16:13:26 +00:00
|
|
|
|
2020-02-24 13:19:40 +00:00
|
|
|
except IntegrityError as e:
|
|
|
|
if "unique" in e.args[0]:
|
|
|
|
continue
|
|
|
|
else:
|
2020-04-26 23:19:56 +00:00
|
|
|
raise e
|
2020-02-24 13:19:40 +00:00
|
|
|
alias.save()
|
|
|
|
|
|
|
|
|
2020-04-26 23:19:56 +00:00
|
|
|
@transaction.atomic
|
|
|
|
def import_activities(cur):
|
|
|
|
cur.execute("SELECT * FROM activites ORDER by id")
|
|
|
|
n = cur.rowcount
|
|
|
|
activity_type = ActivityType.objects.get(name="Pot") # Need to be fixed manually
|
|
|
|
kfet = Club.objects.get(name="Kfet")
|
|
|
|
for idx, row in enumerate(cur):
|
|
|
|
update_line(idx, n, row["alias"])
|
|
|
|
organizer = Club.objects.filter(name=row["signature"])
|
|
|
|
if organizer.exists():
|
2020-05-10 12:57:36 +00:00
|
|
|
# Try to find the club that organizes the activity. If not founded, assume it's Kfet (fix manually)
|
2020-04-26 23:19:56 +00:00
|
|
|
organizer = organizer.get()
|
|
|
|
else:
|
|
|
|
organizer = kfet
|
|
|
|
obj_dict = {
|
|
|
|
"name": row["titre"],
|
|
|
|
"description": row["description"],
|
|
|
|
"activity_type": activity_type, # By default Pot
|
|
|
|
"creater": MAP_IDBDE[row["responsable"]],
|
|
|
|
"organizer": organizer,
|
|
|
|
"attendees_club": kfet, # Maybe fix manually
|
|
|
|
"date_start": row["debut"],
|
|
|
|
"date_end": row["fin"],
|
|
|
|
"valid": row["validepar"] is not None,
|
|
|
|
"open": row["open"], # Should be always False
|
|
|
|
}
|
|
|
|
# WARNING: Fields lieu, liste, listeimprimee are missing
|
|
|
|
try:
|
|
|
|
with transaction.atomic():
|
|
|
|
activity = Activity.objects.get_or_create(**obj_dict)[0]
|
|
|
|
MAP_IDACTIVITY[row["id"]] = activity
|
|
|
|
MAP_NAMEACTIVITY[activity.name] = activity
|
|
|
|
except IntegrityError as e:
|
|
|
|
raise e
|
|
|
|
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def import_activity_entries(cur):
|
|
|
|
map_idguests = {}
|
|
|
|
|
|
|
|
cur.execute("SELECT * FROM invites ORDER by id")
|
|
|
|
n = cur.rowcount
|
|
|
|
for idx, row in enumerate(cur):
|
|
|
|
update_line(idx, n, row["nom"] + " " + row["prenom"])
|
|
|
|
obj_dict = {
|
|
|
|
"activity": MAP_IDACTIVITY[row["activity"]],
|
|
|
|
"last_name": row["nom"],
|
|
|
|
"first_name": row["prenom"],
|
|
|
|
"inviter": MAP_IDBDE[row["responsable"]],
|
|
|
|
}
|
|
|
|
try:
|
|
|
|
with transaction.atomic():
|
|
|
|
guest = Guest.objects.get_or_create(**obj_dict)[0]
|
|
|
|
map_idguests.setdefault(row["responsable"], [])
|
|
|
|
map_idguests[row["id"]].append(guest)
|
|
|
|
guest_name = guest.first_name + " " + guest.last_name
|
|
|
|
MAP_NAMEGUEST.setdefault(guest_name, [])
|
|
|
|
MAP_NAMEGUEST[guest_name].append(guest)
|
|
|
|
except IntegrityError as e:
|
|
|
|
raise e
|
|
|
|
|
|
|
|
cur.execute("SELECT * FROM entree_activites ORDER by id")
|
|
|
|
n = cur.rowcount
|
|
|
|
for idx, row in enumerate(cur):
|
|
|
|
update_line(idx, n, row["nom"] + " " + row["prenom"])
|
|
|
|
activity = MAP_IDACTIVITY[row["activity"]]
|
|
|
|
guest = None
|
|
|
|
if row["est_invite"]:
|
|
|
|
for g in map_idguests[row["id"]]:
|
|
|
|
if g.activity.pk == activity.pk:
|
|
|
|
guest = g
|
|
|
|
break
|
|
|
|
if not guest:
|
|
|
|
raise IntegrityError("Guest was not found: " + str(row))
|
|
|
|
obj_dict = {
|
|
|
|
"activity": activity,
|
|
|
|
"time": row["heure_entree"],
|
|
|
|
"note": guest.inviter if guest else MAP_IDBDE[row["idbde"]],
|
|
|
|
"guest": guest,
|
|
|
|
}
|
|
|
|
try:
|
|
|
|
with transaction.atomic():
|
|
|
|
Entry.objects.get_or_create(**obj_dict)
|
|
|
|
except IntegrityError as e:
|
|
|
|
raise e
|
|
|
|
|
|
|
|
|
2020-04-27 00:08:01 +00:00
|
|
|
@transaction.atomic
|
|
|
|
def import_remittances(cur):
|
|
|
|
cur.execute("SELECT * FROM remises ORDER by id")
|
|
|
|
map_idremittance = {}
|
|
|
|
n = cur.rowcount
|
|
|
|
check_type = RemittanceType.objects.get(note__name="Chèque")
|
|
|
|
for idx, row in enumerate(cur):
|
|
|
|
update_line(idx, n, row["date"])
|
|
|
|
obj_dict = {
|
|
|
|
"date": row["date"][10:],
|
|
|
|
"remittance_type": check_type,
|
|
|
|
"comment": row["commentaire"],
|
|
|
|
"closed": row["close"],
|
|
|
|
}
|
|
|
|
try:
|
|
|
|
with transaction.atomic():
|
|
|
|
remittance = Remittance.objects.get_or_create(**obj_dict)
|
|
|
|
map_idremittance[row["id"]] = remittance
|
|
|
|
except IntegrityError as e:
|
|
|
|
raise e
|
|
|
|
|
|
|
|
print("remittances are imported")
|
|
|
|
print("imported checks")
|
|
|
|
|
|
|
|
cur.execute("SELECT * FROM cheques ORDER by id")
|
|
|
|
n = cur.rowcount
|
|
|
|
for idx, row in enumerate(cur):
|
|
|
|
update_line(idx, n, row["date"])
|
|
|
|
obj_dict = {
|
|
|
|
"date": row["date"][10:],
|
|
|
|
"remittance_type": check_type,
|
|
|
|
"comment": row["commentaire"],
|
|
|
|
"closed": row["close"],
|
|
|
|
}
|
|
|
|
tr = MAP_IDSPECIALTRANSACTION[row["idtransaction"]]
|
|
|
|
proxy = SpecialTransactionProxy.objects.get_or_create(transaction=tr)
|
|
|
|
proxy.remittance = map_idremittance[row["idremise"]]
|
|
|
|
try:
|
|
|
|
with transaction.atomic():
|
|
|
|
proxy.save()
|
|
|
|
except IntegrityError as e:
|
|
|
|
raise e
|
|
|
|
|
|
|
|
|
2020-02-24 13:19:40 +00:00
|
|
|
class Command(BaseCommand):
|
|
|
|
"""
|
|
|
|
Command for importing the database of NK15.
|
|
|
|
Need to be run by a user with a registered role in postgres for the database nk15.
|
|
|
|
"""
|
2020-04-26 23:19:56 +00:00
|
|
|
|
|
|
|
def print_success(self, to_print):
|
2020-02-25 13:17:59 +00:00
|
|
|
return self.stdout.write(self.style.SUCCESS(to_print))
|
2020-04-26 23:19:56 +00:00
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
parser.add_argument('-c', '--comptes', action='store_true', help="import accounts")
|
|
|
|
parser.add_argument('-b', '--boutons', action='store_true', help="import boutons")
|
|
|
|
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")
|
2020-04-26 23:52:06 +00:00
|
|
|
parser.add_argument('-M', '--memberships', action='store_true', help="import memberships")
|
2020-04-27 00:08:01 +00:00
|
|
|
parser.add_argument('-r', '--remittances', action='store_true', help="import check remittances")
|
2020-04-10 16:13:26 +00:00
|
|
|
parser.add_argument('-s', '--save', action='store', help="save mapping of idbde")
|
|
|
|
parser.add_argument('-m', '--map', action='store', help="import mapping of idbde")
|
2020-03-22 17:25:01 +00:00
|
|
|
parser.add_argument('-d', '--nk15db', action='store', default='nk15', help='NK15 database name')
|
|
|
|
parser.add_argument('-u', '--nk15user', action='store', default='nk15_user', help='NK15 database owner')
|
2020-04-26 23:19:56 +00:00
|
|
|
|
2020-02-24 13:19:40 +00:00
|
|
|
def handle(self, *args, **kwargs):
|
2020-04-10 16:13:26 +00:00
|
|
|
global MAP_IDBDE
|
2020-03-22 17:25:01 +00:00
|
|
|
nk15db, nk15user = kwargs['nk15db'], kwargs['nk15user']
|
2020-02-25 13:17:59 +00:00
|
|
|
# connecting to nk15 database
|
2020-04-26 23:19:56 +00:00
|
|
|
conn = pg.connect(database=nk15db, user=nk15user)
|
|
|
|
cur = conn.cursor(cursor_factory=pge.DictCursor)
|
2020-02-24 13:19:40 +00:00
|
|
|
|
|
|
|
if kwargs["comptes"]:
|
2020-04-26 23:19:56 +00:00
|
|
|
# reset database.
|
2020-04-10 22:33:04 +00:00
|
|
|
call_command("migrate")
|
2020-04-26 23:19:56 +00:00
|
|
|
call_command("loaddata", "initial")
|
2020-04-18 14:04:13 +00:00
|
|
|
self.print_success("reset nk20 database\n")
|
2020-04-10 16:13:26 +00:00
|
|
|
import_comptes(cur)
|
2020-02-25 13:17:59 +00:00
|
|
|
self.print_success("comptes table imported")
|
2020-04-10 16:13:26 +00:00
|
|
|
elif kwargs["map"]:
|
|
|
|
filename = kwargs["map"]
|
2020-04-26 23:19:56 +00:00
|
|
|
with open(filename, 'r') as fp:
|
2020-04-10 16:13:26 +00:00
|
|
|
MAP_IDBDE = json.load(fp)
|
2020-04-26 23:19:56 +00:00
|
|
|
MAP_IDBDE = {int(k): int(v) for k, v in MAP_IDBDE.items()}
|
2020-04-10 16:13:26 +00:00
|
|
|
if kwargs["save"]:
|
|
|
|
filename = kwargs["save"]
|
2020-04-26 23:19:56 +00:00
|
|
|
with open(filename, 'w') as fp:
|
|
|
|
json.dump(MAP_IDBDE, fp, sort_keys=True, indent=2)
|
|
|
|
|
2020-04-10 16:13:26 +00:00
|
|
|
# /!\ need a prober MAP_IDBDE
|
2020-02-24 13:19:40 +00:00
|
|
|
if kwargs["boutons"]:
|
2020-04-10 16:13:26 +00:00
|
|
|
import_boutons(cur)
|
2020-04-18 14:04:13 +00:00
|
|
|
self.print_success("boutons table imported\n")
|
2020-04-26 23:19:56 +00:00
|
|
|
if kwargs["activities"]:
|
|
|
|
import_activities(cur)
|
|
|
|
self.print_success("activities imported\n")
|
2020-04-26 23:52:06 +00:00
|
|
|
import_activity_entries(cur)
|
|
|
|
self.print_success("activity entries imported\n")
|
2020-02-24 13:19:40 +00:00
|
|
|
if kwargs["aliases"]:
|
2020-04-10 16:13:26 +00:00
|
|
|
import_aliases(cur)
|
2020-04-18 14:04:13 +00:00
|
|
|
self.print_success("aliases imported\n")
|
|
|
|
if kwargs["transactions"]:
|
|
|
|
import_transaction(cur)
|
|
|
|
self.print_success("transaction imported\n")
|
2020-04-27 00:08:01 +00:00
|
|
|
if kwargs["remittances"]:
|
|
|
|
import_remittances(cur)
|
|
|
|
self.print_success("remittances imported\n")
|