1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2024-12-27 21:42:22 +00:00
plateforme-tfjm2/apps/member/management/commands/import_olddb.py

310 lines
12 KiB
Python
Raw Normal View History

2020-05-11 12:08:19 +00:00
import os
2020-04-29 14:26:52 +00:00
from django.core.management import BaseCommand, CommandError
2020-04-29 04:52:39 +00:00
from django.db import transaction
from member.models import TFJMUser, Document, Solution, Synthesis, Authorization, MotivationLetter
from tournament.models import Team, Tournament
class Command(BaseCommand):
2020-05-11 12:08:19 +00:00
"""
Import the old database.
Tables must be found into the import_olddb folder, as CSV files.
"""
2020-04-29 04:52:39 +00:00
def add_arguments(self, parser):
2020-05-06 12:28:51 +00:00
parser.add_argument('--tournaments', '-t', action="store", help="Import tournaments")
parser.add_argument('--teams', '-T', action="store", help="Import teams")
parser.add_argument('--users', '-u', action="store", help="Import users")
parser.add_argument('--documents', '-d', action="store", help="Import all documents")
2020-04-29 04:52:39 +00:00
def handle(self, *args, **options):
if "tournaments" in options:
self.import_tournaments()
if "teams" in options:
self.import_teams()
if "users" in options:
self.import_users()
if "documents" in options:
self.import_documents()
@transaction.atomic
def import_tournaments(self):
2020-05-11 12:08:19 +00:00
"""
Import tournaments into the new database.
"""
2020-04-29 04:52:39 +00:00
print("Importing tournaments...")
with open("import_olddb/tournaments.csv") as f:
first_line = True
for line in f:
if first_line:
first_line = False
continue
line = line[:-1].replace("\"", "")
args = line.split(";")
args = [arg if arg and arg != "NULL" else None for arg in args]
if Tournament.objects.filter(pk=args[0]).exists():
continue
obj_dict = {
"id": args[0],
"name": args[1],
"size": args[2],
"place": args[3],
"price": args[4],
"description": args[5],
"date_start": args[6],
"date_end": args[7],
"date_inscription": args[8],
"date_solutions": args[9],
"date_syntheses": args[10],
2020-05-05 04:59:32 +00:00
"date_solutions_2": args[11],
"date_syntheses_2": args[12],
"final": args[13],
"year": args[14],
2020-04-29 04:52:39 +00:00
}
with transaction.atomic():
Tournament.objects.create(**obj_dict)
print(self.style.SUCCESS("Tournaments imported"))
2020-04-29 14:26:52 +00:00
@staticmethod
def validation_status(status):
if status == "NOT_READY":
return "0invalid"
elif status == "WAITING":
return "1waiting"
elif status == "VALIDATED":
return "2valid"
else:
raise CommandError("Unknown status: {}".format(status))
2020-04-29 04:52:39 +00:00
@transaction.atomic
def import_teams(self):
2020-05-11 12:08:19 +00:00
"""
Import teams into new database.
"""
2020-04-29 04:52:39 +00:00
self.stdout.write("Importing teams...")
with open("import_olddb/teams.csv") as f:
first_line = True
for line in f:
if first_line:
first_line = False
continue
line = line[:-1].replace("\"", "")
args = line.split(";")
args = [arg if arg and arg != "NULL" else None for arg in args]
if Team.objects.filter(pk=args[0]).exists():
continue
obj_dict = {
"id": args[0],
"name": args[1],
"trigram": args[2],
"tournament": Tournament.objects.get(pk=args[3]),
"inscription_date": args[13],
2020-04-29 14:26:52 +00:00
"validation_status": Command.validation_status(args[14]),
2020-04-29 04:52:39 +00:00
"selected_for_final": args[15],
"access_code": args[16],
"year": args[17],
}
with transaction.atomic():
Team.objects.create(**obj_dict)
print(self.style.SUCCESS("Teams imported"))
2020-04-29 14:26:52 +00:00
@staticmethod
def role(role):
if role == "ADMIN":
return "0admin"
elif role == "ORGANIZER":
return "1volunteer"
elif role == "ENCADRANT":
return "2coach"
elif role == "PARTICIPANT":
return "3participant"
else:
raise CommandError("Unknown role: {}".format(role))
2020-04-29 04:52:39 +00:00
@transaction.atomic
def import_users(self):
2020-05-11 12:08:19 +00:00
"""
Import users into the new database.
:return:
"""
2020-04-29 04:52:39 +00:00
self.stdout.write("Importing users...")
with open("import_olddb/users.csv") as f:
first_line = True
for line in f:
if first_line:
first_line = False
continue
line = line[:-1].replace("\"", "")
args = line.split(";")
args = [arg if arg and arg != "NULL" else None for arg in args]
if TFJMUser.objects.filter(pk=args[0]).exists():
continue
obj_dict = {
"id": args[0],
"email": args[1],
"username": args[1],
"password": "bcrypt$" + args[2],
"last_name": args[3],
"first_name": args[4],
"birth_date": args[5],
"gender": "male" if args[6] == "M" else "female",
"address": args[7],
"postal_code": args[8],
"city": args[9],
"country": args[10],
"phone_number": args[11],
"school": args[12],
2020-05-05 05:09:17 +00:00
"student_class": args[13].lower().replace('premiere', 'première') if args[13] else None,
2020-04-29 04:52:39 +00:00
"responsible_name": args[14],
"responsible_phone": args[15],
"responsible_email": args[16],
"description": args[17].replace("\\n", "\n") if args[17] else None,
2020-04-29 14:26:52 +00:00
"role": Command.role(args[18]),
2020-04-29 04:52:39 +00:00
"team": Team.objects.get(pk=args[19]) if args[19] else None,
"year": args[20],
"date_joined": args[23],
2020-05-11 12:08:19 +00:00
"is_active": args[18] == "ADMIN" or os.getenv("TFJM_STAGE", "dev") == "prod",
2020-04-29 04:52:39 +00:00
"is_staff": args[18] == "ADMIN",
"is_superuser": args[18] == "ADMIN",
}
with transaction.atomic():
TFJMUser.objects.create(**obj_dict)
self.stdout.write(self.style.SUCCESS("Users imported"))
2020-04-29 14:59:59 +00:00
self.stdout.write("Importing organizers...")
2020-05-11 12:08:19 +00:00
# We also import the information about the organizers of a tournament.
2020-04-29 14:59:59 +00:00
with open("import_olddb/organizers.csv") as f:
first_line = True
for line in f:
if first_line:
first_line = False
continue
line = line[:-1].replace("\"", "")
args = line.split(";")
args = [arg if arg and arg != "NULL" else None for arg in args]
with transaction.atomic():
tournament = Tournament.objects.get(pk=args[2])
organizer = TFJMUser.objects.get(pk=args[1])
tournament.organizers.add(organizer)
tournament.save()
self.stdout.write(self.style.SUCCESS("Organizers imported"))
2020-04-29 04:52:39 +00:00
@transaction.atomic
def import_documents(self):
2020-05-11 12:08:19 +00:00
"""
Import the documents (authorizations, motivation letters, solutions, syntheses) from the old database.
"""
2020-04-29 04:52:39 +00:00
self.stdout.write("Importing documents...")
with open("import_olddb/documents.csv") as f:
first_line = True
for line in f:
if first_line:
first_line = False
continue
line = line[:-1].replace("\"", "")
args = line.split(";")
args = [arg if arg and arg != "NULL" else None for arg in args]
if Document.objects.filter(file=args[0]).exists():
2020-05-06 12:28:51 +00:00
doc = Document.objects.get(file=args[0])
doc.uploaded_at = args[5].replace(" ", "T")
doc.save()
2020-04-29 04:52:39 +00:00
continue
obj_dict = {
"file": args[0],
"uploaded_at": args[5],
}
if args[4] != "MOTIVATION_LETTER":
obj_dict["user"] = TFJMUser.objects.get(args[1]),
obj_dict["type"] = args[4].lower()
else:
2020-04-29 23:20:50 +00:00
try:
obj_dict["team"] = Team.objects.get(pk=args[2])
except Team.DoesNotExist:
print("Team with pk {} does not exist, ignoring".format(args[2]))
continue
2020-04-29 04:52:39 +00:00
with transaction.atomic():
if args[4] != "MOTIVATION_LETTER":
Authorization.objects.create(**obj_dict)
else:
MotivationLetter.objects.create(**obj_dict)
self.stdout.write(self.style.SUCCESS("Authorizations imported"))
with open("import_olddb/solutions.csv") as f:
first_line = True
for line in f:
if first_line:
first_line = False
continue
line = line[:-1].replace("\"", "")
args = line.split(";")
args = [arg if arg and arg != "NULL" else None for arg in args]
if Document.objects.filter(file=args[0]).exists():
2020-05-06 12:28:51 +00:00
doc = Document.objects.get(file=args[0])
doc.uploaded_at = args[4].replace(" ", "T")
doc.save()
2020-04-29 04:52:39 +00:00
continue
obj_dict = {
"file": args[0],
"team": Team.objects.get(pk=args[1]),
"problem": args[3],
"uploaded_at": args[4],
}
with transaction.atomic():
2020-05-05 05:09:17 +00:00
try:
Solution.objects.create(**obj_dict)
except:
print("Solution exists")
2020-04-29 04:52:39 +00:00
self.stdout.write(self.style.SUCCESS("Solutions imported"))
with open("import_olddb/syntheses.csv") as f:
first_line = True
for line in f:
if first_line:
first_line = False
continue
line = line[:-1].replace("\"", "")
args = line.split(";")
args = [arg if arg and arg != "NULL" else None for arg in args]
if Document.objects.filter(file=args[0]).exists():
2020-05-06 12:28:51 +00:00
doc = Document.objects.get(file=args[0])
doc.uploaded_at = args[5].replace(" ", "T")
doc.save()
2020-04-29 04:52:39 +00:00
continue
obj_dict = {
"file": args[0],
"team": Team.objects.get(pk=args[1]),
2020-05-05 06:04:55 +00:00
"source": "opponent" if args[3] == "1" else "rapporteur",
2020-05-05 06:01:42 +00:00
"round": args[4],
"uploaded_at": args[5],
2020-04-29 04:52:39 +00:00
}
with transaction.atomic():
2020-05-05 05:23:07 +00:00
try:
Synthesis.objects.create(**obj_dict)
except:
print("Synthesis exists")
2020-04-29 04:52:39 +00:00
self.stdout.write(self.style.SUCCESS("Syntheses imported"))