Fix activities import

This commit is contained in:
Yohann D'ANELLO 2020-07-22 01:28:28 +02:00
parent ee54fca89e
commit 4839b2deb8
5 changed files with 62 additions and 20 deletions

View File

@ -50,8 +50,8 @@ class ImportCommand(BaseCommand):
parser = super().create_parser(prog_name, subcommand, **kwargs)
parser.add_argument('--nk15db', action='store', default='nk15', help='NK15 database name')
parser.add_argument('--nk15user', action='store', default='nk15_user', help='NK15 database owner')
parser.add_argument('-s', '--save', action='store', help="save mapping of idbde")
parser.add_argument('-m', '--map', action='store', help="import mapping of idbde")
parser.add_argument('-s', '--save', default='map.json', action='store', help="save mapping of idbde")
parser.add_argument('-m', '--map', default='map.json', action='store', help="import mapping of idbde")
parser.add_argument('-c', '--chunk', type=int, default=100, help="chunk size for bulk_create")
return parser

View File

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

View File

@ -10,7 +10,7 @@ from django.db import transaction
from activity.models import ActivityType, Activity, Guest, Entry
from member.models import Club
from note.models import Note
from note.models import Note, NoteUser
from ._import_utils import ImportCommand, BulkCreateManager, timed
MAP_ACTIVITY = dict()
@ -19,9 +19,13 @@ CLUB_RELOU = [
0, # BDE
4771, # Kataclist
5162, # Assurance BDE ?!
5164, #S & L
625, #Aspique
5154, #Frekens
5164, # S & L
625, # Aspique
5154, # Frekens
3944, # DiskJok[ENS]
5153, # Monopo[list]
2351, # JdRM
2365, # Pot Vieux
]
class Command(ImportCommand):
@ -40,11 +44,13 @@ class Command(ImportCommand):
pk_activity = 1
for idx, row in enumerate(cur):
self.update_line(idx, n, row["titre"])
if row["responsable"] in CLUB_RELOU:
row["responsable"] = 3508
note = self.MAP_IDBDE[row["responsable"]]
if note == 6244:
# Licorne magique ne doit pas utiliser son compte club pour proposer des activités
note = Note.objects.get(pk=self.MAP_IDBDE[6524])
note = note.user_id
note = note.id
organizer = Club.objects.filter(name=row["signature"])
if organizer.exists():
# Try to find the club that organizes the activity.
@ -57,7 +63,7 @@ class Command(ImportCommand):
"name": row["titre"],
"description": row["description"],
"activity_type_id": activity_type_id, # By default Pot
"creater_id": note,
"creater_id": NoteUser.objects.get(pk=note).user.id,
"organizer_id": organizer.pk,
"attendees_club_id": kfet.pk, # Maybe fix manually
"date_start": make_aware(row["debut"]),

View File

@ -4,7 +4,9 @@ import subprocess
from django.core.management.base import BaseCommand
from django.core.management import call_command
class Command(BaseCommand):
from ._import_utils import ImportCommand
class Command(ImportCommand):
"""
Command for importing the database of NK15.
Need to be run by a user with a registered role in postgres for the database nk15.
@ -12,7 +14,20 @@ class Command(BaseCommand):
def handle(self, *args, **kwargs):
subprocess.call("./apps/scripts/shell/tabularasa")
call_command('import_account', alias=True, chunk=1000, save = "map.json")
call_command('import_activities', chunk=100, map="map.json")
call_command('import_transaction', chunk=10000, buttons=True, map="map.json")
kwargs["alias"] = True
kwargs["chunk"] = 1000
kwargs["save"] = "map.json"
call_command('import_account', **kwargs)
del kwargs["alias"]
del kwargs["save"]
kwargs["chunk"] = 100
kwargs["map"] = "map.json"
call_command('import_activities', **kwargs)
kwargs["chunk"] = 10000
kwargs["map"] = "map.json"
kwargs["buttons"] = True
call_command('import_transaction', **kwargs)
#

View File

@ -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,6 +72,7 @@ class Command(ImportCommand):
}
if row["label"] in self.buttons:
obj_dict["name"] = f"{obj_dict['name']}_{obj_dict['destination_id']}"
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()