mirror of
https://gitlab.crans.org/bde/nk20-scripts
synced 2025-06-29 09:50:54 +02:00
Fix activities import
This commit is contained in:
@ -18,7 +18,7 @@ from note.models import (TemplateCategory,
|
||||
from note.models import Note, NoteClub
|
||||
from activity.models import Guest, GuestTransaction
|
||||
|
||||
from member.models import Membership, MembershipTransaction
|
||||
from member.models import Membership, MembershipTransaction, Role
|
||||
from ._import_utils import ImportCommand, BulkCreateManager, timed
|
||||
|
||||
BDE_PK = 1
|
||||
@ -49,7 +49,7 @@ class Command(ImportCommand):
|
||||
parser.add_argument('-t', '--transactions', action='store', default=0, help="start id for transaction import")
|
||||
|
||||
@timed
|
||||
def import_buttons(self, cur, chunk_size):
|
||||
def import_buttons(self, cur, chunk_size, import_buttons):
|
||||
self.categories = dict()
|
||||
self.buttons = dict()
|
||||
bulk_mgr = BulkCreateManager(chunk_size=chunk_size)
|
||||
@ -58,7 +58,7 @@ class Command(ImportCommand):
|
||||
for idx, row in enumerate(cur):
|
||||
self.update_line(idx, n, row["label"])
|
||||
if row["categorie"] not in self.categories:
|
||||
cat = TemplateCategory.objects.create(name=row["categorie"])
|
||||
cat = TemplateCategory.objects.get_or_create(name=row["categorie"])[0]
|
||||
cat.save()
|
||||
self.categories[row["categorie"]] = cat.pk
|
||||
obj_dict = {
|
||||
@ -72,7 +72,8 @@ class Command(ImportCommand):
|
||||
}
|
||||
if row["label"] in self.buttons:
|
||||
obj_dict["name"] = f"{obj_dict['name']}_{obj_dict['destination_id']}"
|
||||
bulk_mgr.add(TransactionTemplate(**obj_dict))
|
||||
if import_buttons:
|
||||
bulk_mgr.add(TransactionTemplate(**obj_dict))
|
||||
self.buttons[obj_dict["name"]] = (row["id"], self.categories[row["categorie"]])
|
||||
bulk_mgr.done()
|
||||
|
||||
@ -130,6 +131,8 @@ class Command(ImportCommand):
|
||||
m = re.search(r"Invitation (.*?)(?:\s\()(.*?)\s(.*?)\)", row["description"])
|
||||
if m:
|
||||
first_name, last_name = m.group(2), m.group(3)
|
||||
if first_name == "Marion" and last_name == "Bizu Pose":
|
||||
first_name, last_name = "Marion Bizu", "Pose"
|
||||
guest_id = Guest.objects.filter(first_name__iexact=first_name,
|
||||
last_name__iexact=last_name).first().pk
|
||||
child_dict["guest_id"] = guest_id
|
||||
@ -161,6 +164,9 @@ class Command(ImportCommand):
|
||||
except (pytz.NonExistentTimeError, pytz.AmbiguousTimeError):
|
||||
date = make_aware(row["transac_date"] + datetime.timedelta(hours=1))
|
||||
|
||||
if len(row["description"]) > 255:
|
||||
row["description"] = row["description"][:252] + "..."
|
||||
|
||||
# standart transaction object
|
||||
obj_dict = {
|
||||
"pk": pk_transaction,
|
||||
@ -236,6 +242,20 @@ class Command(ImportCommand):
|
||||
bulk_mgr.add(child_transaction(**child_dict))
|
||||
pk_transaction += 1
|
||||
bulk_mgr.done()
|
||||
|
||||
@timed
|
||||
def adjust_roles(self):
|
||||
bdeRole = Role.objects.get(name="Adhérent BDE")
|
||||
kfetRole = Role.objects.get(name="Adhérent Kfet")
|
||||
memberships = Membership.objects.all()
|
||||
n = len(memberships)
|
||||
for idx, membership in enumerate(memberships):
|
||||
self.update_line(idx, n, membership.user.username)
|
||||
if membership.club.name == "BDE":
|
||||
membership.roles.set([bdeRole])
|
||||
elif membership.club.name == "Kfet":
|
||||
membership.roles.set([kfetRole])
|
||||
|
||||
@timed
|
||||
def handle(self, *args, **kwargs):
|
||||
# default args, provided by ImportCommand.
|
||||
@ -246,5 +266,6 @@ class Command(ImportCommand):
|
||||
|
||||
if kwargs["map"]:
|
||||
self.load_map(kwargs["map"])
|
||||
self.import_buttons(cur, kwargs["chunk"])
|
||||
self.import_transaction(cur, kwargs["chunk"], 0)
|
||||
self.import_buttons(cur, kwargs["chunk"], kwargs["buttons"])
|
||||
self.import_transaction(cur, kwargs["chunk"], kwargs["transactions"])
|
||||
self.adjust_roles()
|
||||
|
Reference in New Issue
Block a user