plateforme-tfjm2/apps/participation/management/commands/export_results.py

87 lines
3.7 KiB
Python

# Copyright (C) 2021 by Animath
# SPDX-License-Identifier: GPL-3.0-or-later
import os
from django.contrib.auth.models import User
from django.core.management import BaseCommand
from django.utils.formats import date_format
from django.utils.translation import activate
from participation.models import Tournament
class Command(BaseCommand):
def handle(self, *args, **kwargs):
activate('fr')
tournaments = Tournament.objects.order_by('-date_start', 'name')
for tournament in tournaments:
self.handle_tournament(tournament)
self.w("")
self.w("")
def w(self, msg):
self.stdout.write(msg)
def handle_tournament(self, tournament):
name = tournament.name
date_start = date_format(tournament.date_start, "DATE_FORMAT")
date_end = date_format(tournament.date_end, "DATE_FORMAT")
notes = dict()
for participation in tournament.participations.filter(valid=True).all():
note = sum(pool.average(participation)
for pool in tournament.pools.filter(participations=participation).all())
notes[participation] = note
notes = sorted(notes.items(), key=lambda x: x[1], reverse=True)
self.w("<!-- wp:heading {\"level\":3} -->")
self.w(f"<h3><strong>{name}</strong></h3>")
self.w("<!-- /wp:heading -->")
self.w("")
self.w("<!-- wp:paragraph -->")
if tournament.final:
self.w(f"<p>La finale a eu lieu le weekend du {date_start} au {date_end} et a été remporté par l'équipe <em>{notes[0][0].team.name}</em> suivie de l'équipe <em>{notes[1][0].team.name}</em>. Les deux premières équipes sont sélectionnées pour représenter la France lors de l'ITYM.</p>")
else:
self.w(f"<p>Le tournoi de {name} a eu lieu le weekend du {date_start} au {date_end} et a été remporté par l'équipe <em>{notes[0][0].team.name}</em>.</p>")
self.w("<!-- /wp:paragraph -->")
self.w("")
self.w("")
self.w("<!-- wp:table -->")
self.w("<figure class=\"wp-block-table\">")
self.w("<table>")
self.w("<thead>")
self.w("<tr>")
self.w("\t<th>Équipe</th>")
self.w("\t<th>Score Tour 1</th>")
self.w("\t<th>Score Tour 2</th>")
self.w("\t<th>Total</th>")
self.w("\t<th class=\"has-text-align-center\">Prix</th>")
self.w("</tr>")
self.w("</thead>")
self.w("<tbody>")
for i, (participation, note) in enumerate(notes):
self.w("<tr>")
if i < (2 if len(notes) >= 7 else 1):
self.w(f"\t<th>{participation.team.name} ({participation.team.trigram})</td>")
else:
self.w(f"\t<td>{participation.team.name} ({participation.team.trigram})</td>")
for pool in tournament.pools.filter(participations=participation).all():
pool_note = pool.average(participation)
self.w(f"\t<td>{pool_note:.01f}</td>")
self.w(f"\t<td>{note:.01f}</td>")
if i == 0:
self.w("\t<td class=\"has-text-align-center\">1<sup>er</sup> prix</td>")
elif i < (5 if tournament.final else 3):
self.w(f"\t<td class=\"has-text-align-center\">{i + 1}<sup>ème</sup> prix</td>")
elif i < 2 * len(notes) / 3:
self.w("\t<td class=\"has-text-align-center\">Mention très honorable</td>")
else:
self.w("\t<td class=\"has-text-align-center\">Mention honorable</td>")
self.w("</tr>")
self.w("</tbody>")
self.w("</table>")
self.w("</figure>")
self.w("<!-- /wp:table -->")