1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2024-11-30 03:33:03 +00:00
plateforme-tfjm2/apps/member/management/commands/extract_solutions.py

76 lines
2.9 KiB
Python
Raw Normal View History

import os
2020-06-02 20:32:10 +00:00
from urllib.request import urlretrieve
from shutil import copyfile
from django.core.management import BaseCommand
from django.utils import translation
2020-06-02 20:07:54 +00:00
from member.models import Solution
from tournament.models import Tournament
class Command(BaseCommand):
2020-06-02 20:07:54 +00:00
PROBLEMS = [
'Création de puzzles',
'Départ en vacances',
'Un festin stratégique',
'Sauver les meubles',
'Prêt à décoller !',
'Ils nous espionnent !',
'De joyeux bûcherons',
'Robots auto-réplicateurs',
]
def add_arguments(self, parser):
parser.add_argument('dir',
type=str,
default='.',
help="Directory where solutions should be saved.")
parser.add_argument('--language', '-l',
type=str,
choices=['en', 'fr'],
default='fr',
help="Language of the title of the files.")
def handle(self, *args, **options):
"""
Copy solutions elsewhere.
"""
d = options['dir']
2020-06-02 20:32:10 +00:00
teams_dir = d + '/Par équipe'
os.makedirs(teams_dir, exist_ok=True)
translation.activate(options['language'])
copied = 0
for tournament in Tournament.objects.all():
2020-06-02 20:32:10 +00:00
os.mkdir(teams_dir + '/' + tournament.name)
for team in tournament.teams.filter(validation_status='2valid'):
2020-06-02 20:32:10 +00:00
os.mkdir(teams_dir + '/' + tournament.name + '/' + str(team))
for sol in tournament.solutions:
if not os.path.isfile('media/' + sol.file.name):
self.stdout.write(self.style.WARNING(("Warning: solution '{sol}' is not found. Maybe the file"
"was deleted?").format(sol=str(sol))))
continue
2020-06-02 20:32:10 +00:00
copyfile('media/' + sol.file.name, teams_dir + '/' + tournament.name
+ '/' + str(sol.team) + '/' + str(sol) + '.pdf')
copied += 1
self.stdout.write(self.style.SUCCESS("Successfully copied {copied} solutions!".format(copied=copied)))
2020-06-02 20:07:54 +00:00
os.mkdir(d + '/Par problème')
for pb in range(1, 9):
sols = Solution.objects.filter(problem=pb).all()
pbdir = d + '/Par problème/Problème n°{number}{problem}'.format(number=pb, problem=self.PROBLEMS[pb - 1])
os.mkdir(pbdir)
for sol in sols:
2020-06-02 20:32:10 +00:00
os.symlink('../../Par équipe/' + sol.tournament.name + '/' + str(sol.team) + '/' + str(sol) + '.pdf',
2020-06-02 20:07:54 +00:00
pbdir + '/' + str(sol) + '.pdf')
self.stdout.write(self.style.SUCCESS("Symlinks by problem created!"))
2020-06-02 20:32:10 +00:00
urlretrieve('https://tfjm.org/wp-content/uploads/2020/01/Problemes2020_23_01_v1_1.pdf', d + '/Énoncés.pdf')
self.stdout.write(self.style.SUCCESS("Questions retrieved!"))