1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2025-06-21 05:18:26 +02:00

Import data of old database

This commit is contained in:
Yohann D'ANELLO
2020-04-29 06:52:39 +02:00
parent bc869241b6
commit 1ba789411d
7 changed files with 412 additions and 109 deletions

View File

@ -1,6 +1,6 @@
from django.contrib.auth.admin import admin, UserAdmin
from polymorphic.admin import PolymorphicParentModelAdmin, PolymorphicChildModelAdmin
from member.models import TFJMUser, AbstractDocument, Document, Solution, Synthesis
from member.models import TFJMUser, Document, Solution, Synthesis, MotivationLetter, Authorization
@admin.register(TFJMUser)
@ -8,14 +8,19 @@ class TFJMUserAdmin(UserAdmin):
list_display = ('email', 'first_name', 'last_name', 'role', )
@admin.register(AbstractDocument)
class AbstractDocumentAdmin(PolymorphicParentModelAdmin):
child_models = (Document, Solution, Synthesis,)
@admin.register(Document)
class DocumentAdmin(PolymorphicParentModelAdmin):
child_models = (Authorization, MotivationLetter, Solution, Synthesis,)
polymorphic_list = True
@admin.register(Document)
class DocumentAdmin(PolymorphicChildModelAdmin):
@admin.register(Authorization)
class AuthorizationAdmin(PolymorphicChildModelAdmin):
pass
@admin.register(MotivationLetter)
class MotivationLetterAdmin(PolymorphicChildModelAdmin):
pass

View File

@ -0,0 +1,225 @@
from django.core.management import BaseCommand
from django.db import transaction
from member.models import TFJMUser, Document, Solution, Synthesis, Authorization, MotivationLetter
from tournament.models import Team, Tournament
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('--tournaments', '-t', action="store_true", help="Import tournaments")
parser.add_argument('--teams', '-T', action="store_true", help="Import teams")
parser.add_argument('--users', '-u', action="store_true", help="Import users")
parser.add_argument('--documents', '-d', action="store_true", help="Import all documents")
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):
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],
"final": args[11],
"year": args[12],
}
with transaction.atomic():
Tournament.objects.create(**obj_dict)
print(self.style.SUCCESS("Tournaments imported"))
@transaction.atomic
def import_teams(self):
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],
"validation_status": args[14].lower(),
"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"))
@transaction.atomic
def import_users(self):
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],
"student_class": args[13],
"responsible_name": args[14],
"responsible_phone": args[15],
"responsible_email": args[16],
"description": args[17].replace("\\n", "\n") if args[17] else None,
"role": args[18].lower(),
"team": Team.objects.get(pk=args[19]) if args[19] else None,
"year": args[20],
"date_joined": args[23],
"is_active": args[18] == "ADMIN", # TODO Replace it with "True"
"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"))
@transaction.atomic
def import_documents(self):
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():
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:
obj_dict["team"] = Team.objects.get(pk=args[2])
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():
continue
obj_dict = {
"file": args[0],
"team": Team.objects.get(pk=args[1]),
"problem": args[3],
"uploaded_at": args[4],
}
with transaction.atomic():
Solution.objects.create(**obj_dict)
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():
continue
obj_dict = {
"file": args[0],
"team": Team.objects.get(pk=args[1]),
"dest": "defender" if args[3] == "DEFENDER" else "opponent"
if args[4] == "OPPOSANT" else "rapporteur",
"uploaded_at": args[4],
}
with transaction.atomic():
Synthesis.objects.create(**obj_dict)
self.stdout.write(self.style.SUCCESS("Syntheses imported"))

View File

@ -147,58 +147,74 @@ class TFJMUser(AbstractUser):
return self.first_name + " " + self.last_name
class AbstractDocument(PolymorphicModel):
class Document(PolymorphicModel):
file = models.FileField(
unique=True,
upload_to="files/",
verbose_name=_("file"),
)
team = models.ForeignKey(
Team,
on_delete=models.CASCADE,
related_name="documents",
verbose_name=_("team"),
)
tournament = models.ForeignKey(
Tournament,
on_delete=models.CASCADE,
related_name="documents",
verbose_name=_("tournament"),
)
type = models.CharField(
max_length=32,
choices=[
("parental_consent", _("Parental consent")),
("photo_consent", _("Photo consent")),
("sanitary_plug", _("Sanitary plug")),
("motivation_letter", _("Motivation letter")),
("scholarship", _("Scholarship")),
("solution", _("Solution")),
("synthesis", _("Synthesis")),
],
verbose_name=_("type"),
)
uploaded_at = models.DateTimeField(
auto_now_add=True,
verbose_name=_("uploaded at"),
)
class Meta:
verbose_name = _("abstract document")
verbose_name_plural = _("abstract documents")
class Document(AbstractDocument):
class Meta:
verbose_name = _("document")
verbose_name_plural = _("documents")
class Authorization(Document):
user = models.ForeignKey(
TFJMUser,
on_delete=models.CASCADE,
related_name="authorizations",
verbose_name=_("user"),
)
type = models.CharField(
max_length=32,
choices=[
("parental_consent", _("Parental consent")),
("photo_consent", _("Photo consent")),
("sanitary_plug", _("Sanitary plug")),
("scholarship", _("Scholarship")),
],
verbose_name=_("type"),
)
class Meta:
verbose_name = _("authorization")
verbose_name_plural = _("authorizations")
def __str__(self):
return _("{authorization} for user {user}").format(authorization=self.type, user=str(self.user))
class MotivationLetter(Document):
team = models.ForeignKey(
Team,
on_delete=models.CASCADE,
related_name="motivation_letters",
verbose_name=_("team"),
)
class Meta:
verbose_name = _("motivation letter")
verbose_name_plural = _("motivation letters")
def __str__(self):
return _("Motivation letter of team {team} ({trigram})").format(team=self.team.name, trigram=self.team.trigram)
class Solution(Document):
team = models.ForeignKey(
Team,
on_delete=models.CASCADE,
related_name="solutions",
verbose_name=_("team"),
)
problem = models.PositiveSmallIntegerField(
verbose_name=_("problem"),
)
@ -211,10 +227,27 @@ class Solution(Document):
verbose_name = _("solution")
verbose_name_plural = _("solutions")
def __str__(self):
return _("Solution of team {trigram} for problem {problem}")\
.format(trigram=self.team.trigram, problem=self.problem)
class Synthesis(Document):
problem = models.PositiveSmallIntegerField(
verbose_name=_("problem"),
team = models.ForeignKey(
Team,
on_delete=models.CASCADE,
related_name="syntheses",
verbose_name=_("team"),
)
dest = models.CharField(
max_length=16,
choices=[
("defender", _("Defender")),
("opponent", _("Opponent")),
("rapporteur", _("Rapporteur")),
],
verbose_name=_("dest"),
)
def save(self, **kwargs):
@ -224,3 +257,7 @@ class Synthesis(Document):
class Meta:
verbose_name = _("synthesis")
verbose_name_plural = _("syntheses")
def __str__(self):
return _("Synthesis of team {trigram} that is {dest} for problem {problem}")\
.format(trigram=self.team.trigram, dest=self.dest, problem=self.problem)