2020-05-21 16:28:00 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import psycopg2 as pg
|
|
|
|
import psycopg2.extras as pge
|
|
|
|
import datetime
|
|
|
|
import copy
|
|
|
|
|
|
|
|
from django.utils.timezone import make_aware
|
|
|
|
from django.db import transaction
|
|
|
|
|
2020-05-24 23:21:32 +00:00
|
|
|
from activity.models import ActivityType, Activity, Guest, Entry
|
2020-05-21 16:28:00 +00:00
|
|
|
from member.models import Club
|
2020-05-24 23:21:32 +00:00
|
|
|
from note.models import Note
|
|
|
|
from ._import_utils import ImportCommand, BulkCreateManager, timed
|
2020-05-21 16:28:00 +00:00
|
|
|
|
2020-05-24 23:21:32 +00:00
|
|
|
MAP_ACTIVITY = dict()
|
2020-05-21 16:28:00 +00:00
|
|
|
|
|
|
|
class Command(ImportCommand):
|
|
|
|
"""
|
|
|
|
Import command for Activities Base Data (Comptes, and Aliases)
|
|
|
|
"""
|
|
|
|
|
2020-05-24 23:12:31 +00:00
|
|
|
@timed
|
2020-05-21 16:28:00 +00:00
|
|
|
@transaction.atomic
|
2020-05-25 10:16:36 +00:00
|
|
|
def import_activities(self, cur, chunk):
|
2020-05-21 16:28:00 +00:00
|
|
|
cur.execute("SELECT * FROM activites ORDER by id")
|
|
|
|
n = cur.rowcount
|
2020-05-25 10:16:36 +00:00
|
|
|
bulk_mgr = BulkCreateManager(chunk_size=chunk)
|
2020-05-24 23:21:32 +00:00
|
|
|
activity_type_id = ActivityType.objects.get(name="Pot").pk # Need to be fixed manually
|
2020-05-21 16:28:00 +00:00
|
|
|
kfet = Club.objects.get(name="Kfet")
|
2020-05-24 23:21:32 +00:00
|
|
|
pk_activity = 1
|
2020-05-21 16:28:00 +00:00
|
|
|
for idx, row in enumerate(cur):
|
2020-05-24 23:21:32 +00:00
|
|
|
self.update_line(idx, n, row["titre"])
|
|
|
|
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
|
2020-05-21 16:28:00 +00:00
|
|
|
organizer = Club.objects.filter(name=row["signature"])
|
|
|
|
if organizer.exists():
|
2020-05-24 23:21:32 +00:00
|
|
|
# Try to find the club that organizes the activity.
|
|
|
|
# If not found, assume it's Kfet (fix manually)
|
2020-05-21 16:28:00 +00:00
|
|
|
organizer = organizer.get()
|
|
|
|
else:
|
|
|
|
organizer = kfet
|
|
|
|
obj_dict = {
|
2020-05-24 23:21:32 +00:00
|
|
|
"pk": pk_activity,
|
2020-05-21 16:28:00 +00:00
|
|
|
"name": row["titre"],
|
|
|
|
"description": row["description"],
|
2020-05-24 23:21:32 +00:00
|
|
|
"activity_type_id": activity_type_id, # By default Pot
|
|
|
|
"creater_id": note,
|
|
|
|
"organizer_id": organizer.pk,
|
|
|
|
"attendees_club_id": kfet.pk, # Maybe fix manually
|
|
|
|
"date_start": make_aware(row["debut"]),
|
|
|
|
"date_end": make_aware(row["fin"]),
|
2020-05-21 16:28:00 +00:00
|
|
|
"valid": row["validepar"] is not None,
|
2020-05-24 23:21:32 +00:00
|
|
|
"open": row["ouvert"], # Should always be False
|
2020-05-21 16:28:00 +00:00
|
|
|
}
|
|
|
|
# WARNING: Fields lieu, liste, listeimprimee are missing
|
2020-05-24 23:21:32 +00:00
|
|
|
MAP_ACTIVITY[row["id"]] = pk_activity
|
2020-05-26 21:53:13 +00:00
|
|
|
pk_activity += 1
|
2020-05-21 16:28:00 +00:00
|
|
|
bulk_mgr.add(Activity(**obj_dict))
|
|
|
|
bulk_mgr.done()
|
|
|
|
|
2020-05-24 23:12:31 +00:00
|
|
|
@timed
|
2020-05-21 16:28:00 +00:00
|
|
|
@transaction.atomic
|
2020-05-25 10:16:36 +00:00
|
|
|
def import_guest(self, cur, chunk):
|
|
|
|
bulk_mgr = BulkCreateManager(chunk_size=chunk)
|
2020-05-21 16:28:00 +00:00
|
|
|
cur.execute("SELECT * FROM invites ORDER by id")
|
|
|
|
n = cur.rowcount
|
|
|
|
for idx, row in enumerate(cur):
|
2020-05-24 23:21:32 +00:00
|
|
|
self.update_line(idx, n, f"{row['nom']} {row['prenom']}")
|
2020-05-21 16:28:00 +00:00
|
|
|
obj_dict = {
|
|
|
|
"pk": row["id"],
|
2020-05-24 23:21:32 +00:00
|
|
|
"activity_id": MAP_ACTIVITY[row["activite"]],
|
2020-05-21 16:28:00 +00:00
|
|
|
"last_name": row["nom"],
|
|
|
|
"first_name": row["prenom"],
|
2020-05-24 23:21:32 +00:00
|
|
|
"inviter_id": self.MAP_IDBDE[row["responsable"]],
|
2020-05-21 16:28:00 +00:00
|
|
|
}
|
|
|
|
bulk_mgr.add(Guest(**obj_dict))
|
|
|
|
bulk_mgr.done()
|
2020-05-25 10:16:36 +00:00
|
|
|
|
|
|
|
@timed
|
|
|
|
@transaction.atomic
|
|
|
|
def import_activities_entries(self, cur, chunk):
|
|
|
|
bulk_mgr = BulkCreateManager(chunk_size=chunk)
|
2020-05-21 16:28:00 +00:00
|
|
|
cur.execute("SELECT * FROM entree_activites ORDER by id")
|
|
|
|
n = cur.rowcount
|
|
|
|
for idx, row in enumerate(cur):
|
2020-05-24 23:21:32 +00:00
|
|
|
self.update_line(idx, n, f"{row['idbde']} {row['responsable']}")
|
2020-05-21 16:28:00 +00:00
|
|
|
obj_dict = {
|
2020-05-24 23:21:32 +00:00
|
|
|
"activity_id": MAP_ACTIVITY[row["activite"]],
|
2020-05-21 16:28:00 +00:00
|
|
|
"time": make_aware(row["heure_entree"]),
|
2020-05-25 10:16:36 +00:00
|
|
|
"note_id": self.MAP_IDBDE[row["responsable"] if row['est_invite'] else row["idbde"]],
|
|
|
|
"guest_id": self.MAP_IDBDE[row["idbde"]] if row['est_invite'] else None,
|
2020-05-21 16:28:00 +00:00
|
|
|
}
|
2020-05-24 23:21:32 +00:00
|
|
|
bulk_mgr.add(Entry(**obj_dict))
|
|
|
|
bulk_mgr.done()
|
2020-05-21 16:28:00 +00:00
|
|
|
|
|
|
|
def handle(self, *args, **kwargs):
|
|
|
|
# default args, provided by ImportCommand.
|
|
|
|
nk15db, nk15user = kwargs['nk15db'], kwargs['nk15user']
|
|
|
|
# connecting to nk15 database
|
|
|
|
conn = pg.connect(database=nk15db, user=nk15user)
|
|
|
|
cur = conn.cursor(cursor_factory=pge.DictCursor)
|
|
|
|
|
|
|
|
if kwargs["map"]:
|
2020-05-24 23:21:32 +00:00
|
|
|
self.load_map(kwargs["map"])
|
|
|
|
self.import_activities(cur, kwargs["chunk"])
|
2020-05-25 10:16:36 +00:00
|
|
|
self.import_guest(cur, kwargs["chunk"])
|
|
|
|
self.import_activities_entries(cur, kwargs["chunk"])
|