nk20-scripts/management/commands/import_nk15.py

260 lines
9.7 KiB
Python
Raw Normal View History

2020-02-24 13:19:40 +00:00
#!/usr/env/bin python3
from django.core.management.base import BaseCommand
2020-02-25 13:17:59 +00:00
from django.core.management import call_command
2020-02-24 13:19:40 +00:00
from django.utils import timezone
import psycopg2 as pg
import psycopg2.extras as pge
from django.db import transaction
import collections
2020-03-09 18:00:19 +00:00
from datetime import timedelta
2020-02-24 13:19:40 +00:00
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
from note.models import Note, NoteSpecial, NoteUser, NoteClub
from note.models import Alias
from note.models import Transaction, TransactionTemplate,\
TemplateCategory, TemplateTransaction, MembershipTransaction
from member.models import Profile, Club, Membership
2020-02-24 13:19:40 +00:00
"""
Script d'import de la nk15:
TODO: import transactions
TODO: import adhesion
TODO: import activite
TODO: import ...
2020-02-24 13:19:40 +00:00
"""
2020-03-09 18:00:19 +00:00
M_DURATION = timedelta(days=396)
M_START = timedelta(days=213)
M_END = timedelta(days=273)
2020-02-24 13:19:40 +00:00
@transaction.atomic
def import_special(cur):
cur.execute("SELECT * FROM comptes WHERE idbde <0 ORDER BY idbde;")
map_idbde = dict()
for row in cur:
obj,created = NoteSpecial.objects.get_or_create(special_type = row["pseudo"],
balance = row["solde"],
is_active =True)
if created:
obj.save()
map_idbde[row["idbde"]] = obj.pk
cur.execute("SELECT * FROM comptes WHERE idbde=0;")
res = cur.fetchone()
clubBde, c = Club.objects.get_or_create(pk = 1,
name = "Bde",
email = "bureau.bde@lists.crans.org",
2020-03-09 18:00:19 +00:00
membership_duration = M_DURATION,
membership_start = M_START,
membership_end = M_END,
2020-02-24 13:19:40 +00:00
membership_fee = 5,
)
clubKfet, c = Club.objects.get_or_create(pk = 2,
name = "Kfet",
email = "tresorerie.bde@lists.crans.org",
2020-03-09 18:00:19 +00:00
membership_duration = M_DURATION,
membership_start = M_START,
membership_end = M_END,
2020-02-24 13:19:40 +00:00
membership_fee = 35,
)
clubBde.save()
clubKfet.save()
clubBde.note.solde=res["solde"]
map_idbde[0] = clubKfet.note.pk
return map_idbde
@transaction.atomic
def import_comptes(cur,map_idbde):
cur.execute("SELECT * FROM comptes WHERE idbde > 0 ORDER BY idbde;")
pkclub = 3
for row in cur:
if row["type"] == "personne":
#sanitize password
if row["passwd"] != "*|*":
passwd_nk15 = "$".join(["custom_nk15","1",row["passwd"]])
else:
passwd_nk15 = ''
try:
obj_dict = {
"username": row["pseudo"],
"password": passwd_nk15,
"first_name": row["nom"],
"last_name": row["prenom"],
"email": row["mail"],
"is_active" : False, # temporary
}
user = User.objects.create(**obj_dict)
#sanitize duplicate aliases (nk12)
except ValidationError as e:
if e.code == 'same_alias':
obj_dict["username"] = row["pseudo"]+str(row["idbde"])
user = User.objects.create(**obj_dict)
else:
raise(e)
2020-03-09 18:00:19 +00:00
# profile and note created via signal.
profile = user.profile
profile.phone_number = row["tel"]
profile.address = row["adresse"]
profile.paid = row["normalien"]
2020-02-24 13:19:40 +00:00
note = user.note
2020-02-25 13:18:39 +00:00
date = row.get("last_negatif",None)
if date != None:
note.last_negative = make_aware(date)
2020-02-24 13:19:40 +00:00
note.balance = row["solde"]
obj_list =[user, profile, note]
else: # club
obj_dict = {
"pk":pkclub,
"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-02-24 13:19:40 +00:00
"membership_fee": 0,
}
club,c = Club.objects.get_or_create(**obj_dict)
pkclub +=1
note = club.note
note.balance = row["solde"]
obj_list = [club,note]
for obj in obj_list:
obj.save()
2020-03-09 18:00:19 +00:00
map_idbde[row["idbde"]] = note.pk
2020-02-24 13:19:40 +00:00
return map_idbde
@transaction.atomic
def import_boutons(cur,map_idbde):
cur.execute("SELECT * FROM boutons;")
for row in cur:
cat, created = TemplateCategory.objects.get_or_create(name=row["categorie"])
if created:
cat.save()
2020-02-24 13:19:40 +00:00
obj_dict = {
"pk": row["id"],
"name": row["label"],
"amount": row["montant"],
"destination_id": map_idbde[row["destinataire"]],
"category": cat,
"display" : row["affiche"],
"description": row["description"],
}
try:
with transaction.atomic(): # required for error management
button = TransactionTemplate.objects.create(**obj_dict)
except IntegrityError as e:
# button with the same name is not possible in NK20.
2020-02-24 13:19:40 +00:00
if "unique" in e.args[0]:
qs = Club.objects.filter(note__id=map_idbde[row["destinataire"]]).values('name')
note_name = qs[0]["name"]
#rename button name
obj_dict["name"] ="{} {}".format(obj_dict["name"],note_name)
2020-02-24 13:19:40 +00:00
button = TransactionTemplate.objects.create(**obj_dict)
else:
raise(e)
button.save()
@transaction.atomic
def import_transaction(cur, map_idbde):
cur.execute("SELECT * FROM transactions LEFT JOIN adhesions ON transactions.id = adhesions.idtransaction ORDER BY -id;")
2020-02-24 13:19:40 +00:00
for row in cur:
obj_dict = {
# "pk": row["id"],
"destination_id" : map_idbde[row["destinataire"]],
"source_id": map_idbde[row["emetteur"]],
"created_at":make_aware(row["date"]),
"amount":row["montant"],
"quantity":row["quantite"],
"reason":row["description"],
"valid":row["valide"],
2020-02-24 13:19:40 +00:00
}
if row["type"] == "bouton":
cat_name = row["categorie"]
if cat_name == None:
cat_name = 'None'
cat, created = TemplateCategory.objects.get_or_create(name=cat_name)
if created:
cat.save()
obj_dict["category"] = cat
transac = TemplateTransaction.objects.create(**obj_dict)
transac.save()
elif row["type"] == "adhésion":
2020-03-09 18:00:19 +00:00
print("adhesion not supported yet")
else:
2020-03-09 18:00:19 +00:00
print("other type not supported yet")
2020-02-24 13:19:40 +00:00
@transaction.atomic
def import_aliases(cur,map_idbde):
cur.execute("SELECT * FROM aliases ORDER by id")
for row in cur:
alias_name = row["alias"]
alias_name_good = (alias_name[:252]+'...') if len(alias_name) > 255 else alias_name
obj_dict = {
"note_id":map_idbde[row["idbde"]],
"name":alias_name_good,
"normalized_name":Alias.normalize(alias_name_good)
2020-02-24 13:19:40 +00:00
}
try:
with transaction.atomic():
alias, created = Alias.objects.get_or_create(**obj_dict)
print(alias)
2020-02-24 13:19:40 +00:00
except IntegrityError as e:
if "unique" in e.args[0]:
print("error, {}".format(alias))
2020-02-24 13:19:40 +00:00
continue
else:
raise(e)
alias.save()
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-02-25 13:17:59 +00:00
def print_success(self,to_print):
return self.stdout.write(self.style.SUCCESS(to_print))
2020-02-24 13:19:40 +00:00
def add_arguments(self,parser):
2020-02-25 13:17:59 +00:00
parser.add_argument('-s', '--special', action = 'store_true', help="create Minimum instance (special note and clubs)")
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('-a', '--aliases', action = 'store_true',help="import aliases")
2020-02-24 13:19:40 +00:00
def handle(self, *args, **kwargs):
2020-02-25 13:17:59 +00:00
#reset database.
call_command("flush")
self.print_success("flush nk20 database")
# connecting to nk15 database
2020-02-24 13:19:40 +00:00
conn = pg.connect(database="nk15",user="nk15_user")
cur = conn.cursor(cursor_factory = pge.DictCursor)
if kwargs["special"]:
map_idbde = import_special(cur)
2020-02-25 13:17:59 +00:00
self.print_success("Minimal setup created")
2020-02-24 13:19:40 +00:00
if kwargs["comptes"]:
map_idbde = import_comptes(cur,map_idbde)
2020-02-25 13:17:59 +00:00
self.print_success("comptes table imported")
2020-02-24 13:19:40 +00:00
if kwargs["boutons"]:
import_boutons(cur,map_idbde)
2020-02-25 13:17:59 +00:00
self.print_success("boutons table imported")
2020-02-24 13:19:40 +00:00
if kwargs["transactions"]:
2020-02-25 13:17:59 +00:00
import_transaction(cur,map_idbde)
self.print_success("transaction imported")
2020-02-24 13:19:40 +00:00
if kwargs["aliases"]:
import_aliases(cur,map_idbde)
2020-02-25 13:17:59 +00:00
self.print_success("aliases imported")