add boutons import

This commit is contained in:
Pierre-antoine Comby 2020-02-23 18:45:21 +01:00
parent 126686ab03
commit b18e5b03a4
2 changed files with 65 additions and 46 deletions

View File

@ -5,10 +5,11 @@ from django.utils import timezone
import psycopg2 as pg import psycopg2 as pg
import psycopg2.extras as pge import psycopg2.extras as pge
from django.db import transaction from django.db import transaction
import json
import collections
from django.core.exceptions import ValidationError
import collections
from django.core.exceptions import ValidationError
from django.db import IntegrityError
from django.contrib.auth.models import User from django.contrib.auth.models import User
from note.models import Note, NoteSpecial, NoteUser, NoteClub from note.models import Note, NoteSpecial, NoteUser, NoteClub
from note.models import Transaction, TransactionTemplate, TransactionCategory, TransactionType from note.models import Transaction, TransactionTemplate, TransactionCategory, TransactionType
@ -64,46 +65,44 @@ def import_comptes(cur,map_idbde):
else: else:
passwd_nk15 = '' passwd_nk15 = ''
try: try:
user = User.objects.create( obj_dict = {
username =row["pseudo"], "username": row["pseudo"],
password = passwd_nk15, "password": passwd_nk15,
first_name = row["nom"], "first_name": row["nom"],
last_name = row["prenom"], "last_name": row["prenom"],
email = row["mail"], "email": row["mail"],
) }
#sanitize duplicate aliases (nk12) user = User.objects.create(**obj_dict)
#sanitize duplicate aliases (nk12)
except ValidationError as e: except ValidationError as e:
if e.code == 'same_alias': if e.code == 'same_alias':
user = User.objects.create( obj_dict["username"] = row["pseudo"]+str(row["idbde"])
username = row["pseudo"]+str(row["idbde"]), user = User.objects.create(**obj_dict)
password = row["passwd"] if row["passwd"] != '*|*' else '',
first_name = row["nom"],
last_name = row["prenom"],
email = row["mail"],
)
else: else:
raise(e) raise(e)
else: else:
pass pass
profile = Profile.objects.create( obj_dict ={
phone_number = row["tel"], "phone_number": row["tel"],
address = row["adresse"], "address": row["adresse"],
paid = row["normalien"], "paid": row["normalien"],
user = user, "user": user,
) }
profile = Profile.objects.create(**obj_dict)
note = user.note note = user.note
note.balance = row["solde"] note.balance = row["solde"]
obj_list =[user, profile, note] obj_list =[user, profile, note]
else:#club else: # club
club,c = Club.objects.get_or_create(pk=pkclub, obj_dict = {
name = row["pseudo"], "pk":pkclub,
email = row["mail"], "name": row["pseudo"],
membership_duration = "396 00:00:00", "email": row["mail"],
membership_start = "213 00:00:00", "membership_duration": "396 00:00:00",
membership_end = "273 00:00:00", "membership_start": "213 00:00:00",
membership_fee =0, "membership_end": "273 00:00:00",
) "membership_fee": 0,
}
club,c = Club.objects.get_or_create(**obj_dict)
pkclub +=1 pkclub +=1
note = club.note note = club.note
note.balance = row["solde"] note.balance = row["solde"]
@ -111,27 +110,46 @@ def import_comptes(cur,map_idbde):
for obj in obj_list: for obj in obj_list:
obj.save() obj.save()
map_idbde[row["idbde"]] = note.pk map_idbde[row["idbde"]] = note.pk
#
return map_idbde return map_idbde
@transaction.atomic @transaction.atomic
def import_boutons(cur,map_idbde): def import_boutons(cur,map_idbde):
cur.execute("SELECT * FROM boutons;") cur.execute("SELECT * FROM boutons;")
for row in cur: for row in cur:
cat, created = TransactionCategory.objects.get_or_create(name=row["categorie"]) cat, created = TransactionCategory.objects.get_or_create(name=row["categorie"])
try:
button = TransactionTemplate.objects.create(pk=row["id"], obj_dict = {
name=row["label"], "pk": row["id"],
amount=row["montant"], "name": row["label"],
destination_id=map_idbde[row["destinataire"]], "amount": row["montant"],
category = cat, "destination_id": map_idbde[row["destinataire"]],
display = row["affiche"], "category": cat,
description = row["description"], "display" : row["affiche"],
) "description": row["description"],
}
with transaction.atomic(): # required for error management
button = TransactionTemplate.objects.create(**obj_dict)
except IntegrityError as e:
if "unique" in e.args[0]:
qs = Club.objects.filter(note__id=map_idbde[row["destinataire"]]).values('name')
note_name = qs[0]["name"]
obj_dict["name"] = ' '.join([obj_dict["name"],note_name])
button = TransactionTemplate.objects.create(**obj_dict)
else:
raise(e)
if created: if created:
cat.save() cat.save()
button.save() button.save()
@transaction.atomic
def import_transaction(cur, map_idbde):
cur.execute("SELECT * FROM transactions;")
for row in cur:
obj_dict = {
"pk":row["id"],
}
class Command(BaseCommand): class Command(BaseCommand):
""" """
@ -142,7 +160,7 @@ class Command(BaseCommand):
parser.add_argument('-s', '--special', action = 'store_true') parser.add_argument('-s', '--special', action = 'store_true')
parser.add_argument('-c', '--comptes', action = 'store_true') parser.add_argument('-c', '--comptes', action = 'store_true')
parser.add_argument('-b', '--boutons', action = 'store_true') parser.add_argument('-b', '--boutons', action = 'store_true')
parser.add_argument('-t', '--transactions', action = 'store_true')
def handle(self, *args, **kwargs): def handle(self, *args, **kwargs):
conn = pg.connect(database="nk15",user="nk15_user") conn = pg.connect(database="nk15",user="nk15_user")
@ -159,5 +177,5 @@ class Command(BaseCommand):
if kwargs["boutons"]: if kwargs["boutons"]:
import_boutons(cur,map_idbde) import_boutons(cur,map_idbde)
print("boutons table imported") print("boutons table imported")
if kwargs["transaction"]: if kwargs["transactions"]:
import_transaction(cur) import_transaction(cur)

View File

@ -43,6 +43,7 @@ class TransactionTemplate(models.Model):
verbose_name=_('name'), verbose_name=_('name'),
max_length=255, max_length=255,
unique=True, unique=True,
error_messages={'unique':_("A template with this name already exist")},
) )
destination = models.ForeignKey( destination = models.ForeignKey(
NoteClub, NoteClub,