add missing fields and fix bugs

This commit is contained in:
Pierre-antoine Comby 2020-06-01 17:54:49 +02:00
parent 0ba656d5e0
commit 559be286b2
2 changed files with 56 additions and 20 deletions

View File

@ -82,7 +82,7 @@ class Command(ImportCommand):
"balance": row['solde'], "balance": row['solde'],
"last_negative": None, "last_negative": None,
"is_active": True, "is_active": True,
"display_image": "", "display_image": "pic/default.png",
"created_at": now() "created_at": now()
} }
if row["last_negatif"] is not None: if row["last_negatif"] is not None:

View File

@ -8,28 +8,42 @@ import copy
from django.utils.timezone import make_aware from django.utils.timezone import make_aware
from django.db import transaction from django.db import transaction
from django.contrib.contenttypes.models import ContentType
from note.models import (TemplateCategory, from note.models import (TemplateCategory,
TransactionTemplate, TransactionTemplate,
Transaction, Transaction,
RecurrentTransaction, RecurrentTransaction,
SpecialTransaction SpecialTransaction,
MembershipTransaction,
) )
from note.models import Note, NoteClub from note.models import Note, NoteClub
from activity.models import Guest, GuestTransaction from activity.models import Guest, GuestTransaction
from member.models import Membership, MembershipTransaction from member.models import Membership
from ._import_utils import ImportCommand, BulkCreateManager, timed from ._import_utils import ImportCommand, BulkCreateManager, timed
# from member/fixtures/initial
BDE_PK = 1 BDE_PK = 1
KFET_PK = 2 KFET_PK = 2
# from note/fixtures/initial
NOTE_SPECIAL_CODE = { NOTE_SPECIAL_CODE = {
"espèce": 1, "espèce": 1,
"carte": 2, "carte": 2,
"chèque": 3, "chèque": 3,
"virement": 4, "virement": 4,
} }
# from permission/fixtures/initial
BDE_ROLE_PK = 1
KFET_ROLE_PK = 2
CT = {
"RecurrentTransaction": ContentType.objects.get(app_label="note", model="recurrenttransaction"),
"SpecialTransaction": ContentType.objects.get(app_label="note", model="specialtransaction"),
"MembershipTransaction": ContentType.objects.get(app_label="note", model="membershiptransaction"),
"GuestTransaction": ContentType.objects.get(app_label="activity", model="guesttransaction"),
}
def get_date_end(date_start): def get_date_end(date_start):
date_end = copy.deepcopy(date_start) date_end = copy.deepcopy(date_start)
@ -82,18 +96,20 @@ class Command(ImportCommand):
return obj_dict, None, None return obj_dict, None, None
def _template_transaction(self, row, obj_dict, child_dict): def _template_transaction(self, row, obj_dict, child_dict):
if self.categories.get(row["categorie"]): if self.buttons.get(row["description"]):
child_dict["category_id"] = self.categories[row["categorie"]]
elif "WEI" in row["description"]:
return obj_dict, None, None
elif self.buttons.get(row["description"]):
child_dict["category_id"] = self.buttons[row["description"]][1] child_dict["category_id"] = self.buttons[row["description"]][1]
child_dict["template_id"] = self.buttons[row["description"]][0] child_dict["template_id"] = self.buttons[row["description"]][0]
# elif self.categories.get(row["categorie"]):
# child_dict["category_id"] = self.categories[row["categorie"]]
elif "WEI" in row["description"]:
return obj_dict, None, None
else: else:
return obj_dict, None, None return obj_dict, None, None
obj_dict["polymorphic_ctype"] = CT["RecurrentTransaction"]
return obj_dict, child_dict, RecurrentTransaction return obj_dict, child_dict, RecurrentTransaction
def _membership_transaction(self, row, obj_dict, child_dict, pk_membership): def _membership_transaction(self, row, obj_dict, child_dict, pk_membership):
obj_dict["polymorphic_ctype"] = CT["MembershipTransaction"]
obj_dict2 = obj_dict.copy() obj_dict2 = obj_dict.copy()
child_dict2 = child_dict.copy() child_dict2 = child_dict.copy()
child_dict2["membership_id"] = pk_membership child_dict2["membership_id"] = pk_membership
@ -103,6 +119,7 @@ class Command(ImportCommand):
def _special_transaction(self, row, obj_dict, child_dict): def _special_transaction(self, row, obj_dict, child_dict):
# Some transaction uses BDE (idbde=0) as source or destination, # Some transaction uses BDE (idbde=0) as source or destination,
# lets fix that. # lets fix that.
obj_dict["polymorphic_ctype"] = CT["SpecialTransaction"]
field_id = "source_id" if row["type"] == "crédit" else "destination_id" field_id = "source_id" if row["type"] == "crédit" else "destination_id"
if "espèce" in row["description"]: if "espèce" in row["description"]:
obj_dict[field_id] = 1 obj_dict[field_id] = 1
@ -127,6 +144,7 @@ class Command(ImportCommand):
def _guest_transaction(self, row, obj_dict, child_dict): def _guest_transaction(self, row, obj_dict, child_dict):
# Currently GuestTransaction is related to a Guest. # Currently GuestTransaction is related to a Guest.
# This is not ideal and should be change to the Entry of this Guest. # This is not ideal and should be change to the Entry of this Guest.
obj_dict["polymorphic_ctype"] = CT["GuestTransaction"]
m = re.search(r"Invitation (.*?)(?:\s\()(.*?)\s(.*?)\)", row["description"]) m = re.search(r"Invitation (.*?)(?:\s\()(.*?)\s(.*?)\)", row["description"])
if m: if m:
first_name, last_name = m.group(2), m.group(3) first_name, last_name = m.group(2), m.group(3)
@ -165,28 +183,35 @@ class Command(ImportCommand):
obj_dict = { obj_dict = {
"pk": pk_transaction, "pk": pk_transaction,
"destination_id": self.MAP_IDBDE[row["destinataire"]], "destination_id": self.MAP_IDBDE[row["destinataire"]],
"polymorphic_ctype": None,
"source_id": self.MAP_IDBDE[row["emetteur"]], "source_id": self.MAP_IDBDE[row["emetteur"]],
"created_at": date,
"amount": row["montant"], "amount": row["montant"],
"created_at": date,
"destination_alias": "",
"invalidity_reason": None,
"quantity": row["quantite"], "quantity": row["quantite"],
"reason": row["description"], "reason": row["description"],
"source_alias": "",
"valid": row["valide"], "valid": row["valide"],
} }
# for child transaction Models # for child transaction Models
child_dict = {"pk": obj_dict["pk"]} child_dict = {"pk": pk_transaction}
ttype = row["type"] ttype = row["type"]
# Membership transaction detection and import
if row["valide"] and (ttype == "adhésion" or row["description"].lower() == "inscription"): if row["valide"] and (ttype == "adhésion" or row["description"].lower() == "inscription"):
note = Note.objects.get(pk=obj_dict["source_id"]) note = Note.objects.get(pk=obj_dict["source_id"])
if isinstance(note, NoteClub): if isinstance(note, NoteClub):
child_transaction = None child_transaction = None # don't bother register clubs
else: else:
user_id = note.user_id user_id = note.user_id
montant = obj_dict["amount"] montant = obj_dict["amount"]
obj_dict0, child_dict0, child_transaction = self._membership_transaction(row, obj_dict, child_dict,pk_membership) (obj_dict0,
child_dict0,
child_transaction) = self._membership_transaction(row, obj_dict, child_dict, pk_membership)
bde_dict = { bde_dict = {
"pk": pk_membership, "pk": pk_membership,
"user_id": user_id, "user_id": user_id,
"club_id": KFET_PK, "club_id": BDE_PK,
"date_start": date.date(), # Only date, not time "date_start": date.date(), # Only date, not time
"date_end": get_date_end(date.date()), "date_end": get_date_end(date.date()),
"fee": min(500, montant) "fee": min(500, montant)
@ -201,7 +226,7 @@ class Command(ImportCommand):
kfet_dict = { kfet_dict = {
"pk": pk_membership, "pk": pk_membership,
"user_id": user_id, "user_id": user_id,
"club_id": BDE_PK, "club_id": KFET_PK,
"date_start": date.date(), # Only date, not time "date_start": date.date(), # Only date, not time
"date_end": get_date_end(date.date()), "date_end": get_date_end(date.date()),
"fee": max(montant - 500, 0), "fee": max(montant - 500, 0),
@ -212,12 +237,12 @@ class Command(ImportCommand):
pk_membership += 1 pk_membership += 1
pk_transaction += 1 pk_transaction += 1
bulk_mgr.add( bulk_mgr.add(
Membership(**bde_dict),
Membership(**kfet_dict),
Transaction(**obj_dict0), Transaction(**obj_dict0),
child_transaction(**child_dict0), child_transaction(**child_dict0),
Transaction(**obj_dict), Transaction(**obj_dict),
child_transaction(**child_dict), child_transaction(**child_dict),
Membership(**bde_dict),
Membership(**kfet_dict),
) )
continue continue
elif ttype == "bouton": elif ttype == "bouton":
@ -226,7 +251,7 @@ class Command(ImportCommand):
obj_dict, child_dict, child_transaction = self._special_transaction(row, obj_dict, child_dict) obj_dict, child_dict, child_transaction = self._special_transaction(row, obj_dict, child_dict)
elif ttype == "invitation": elif ttype == "invitation":
obj_dict, child_dict, child_transaction = self._guest_transaction(row, obj_dict, child_dict) obj_dict, child_dict, child_transaction = self._guest_transaction(row, obj_dict, child_dict)
if ttype == "don" or ttype == "transfert": elif ttype == "don" or ttype == "transfert":
obj_dict, child_dict, child_transaction = self._basic_transaction(row, obj_dict, child_dict) obj_dict, child_dict, child_transaction = self._basic_transaction(row, obj_dict, child_dict)
else: else:
child_transaction = None child_transaction = None
@ -236,6 +261,17 @@ class Command(ImportCommand):
bulk_mgr.add(child_transaction(**child_dict)) bulk_mgr.add(child_transaction(**child_dict))
pk_transaction += 1 pk_transaction += 1
bulk_mgr.done() bulk_mgr.done()
def set_roles(self):
bulk_mgr = BulkCreateManager(chunk_size=10000)
membership_ids = Membership.objects.values_list('id',flat=True)
for m_id in membership_ids:
bulk_mgr.add(
Membership.roles.trough(membership_id=m_id,role_id=BDE_ROLE_PK),
Membership.roles.trough(membership_id=m_id,role_id=KFET_ROLE_PK),
)
bulk_mgr.done()
@timed @timed
def handle(self, *args, **kwargs): def handle(self, *args, **kwargs):
# default args, provided by ImportCommand. # default args, provided by ImportCommand.