Add script to parse notes in Google Sheets
Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
parent
b9295cc199
commit
234b84ef60
|
@ -0,0 +1,39 @@
|
|||
# Copyright (C) 2024 by Animath
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
from time import sleep
|
||||
|
||||
from django.core.management import BaseCommand
|
||||
from participation.models import Tournament
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument(
|
||||
'--tournament', '-t', help="Tournament name to update (if not set, all tournaments will be updated)",
|
||||
)
|
||||
parser.add_argument(
|
||||
'--round', '-r', type=int, help="Round number to update (if not set, all rounds will be updated)",
|
||||
)
|
||||
parser.add_argument(
|
||||
'--letter', '-l', help="Letter of the pool to update (if not set, all pools will be updated)",
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
tournaments = Tournament.objects.all() if not options['tournament'] \
|
||||
else Tournament.objects.filter(name=options['tournament']).all()
|
||||
|
||||
for tournament in tournaments:
|
||||
if options['verbosity'] >= 1:
|
||||
self.stdout.write(f"Parsing notation sheet for {tournament}")
|
||||
|
||||
pools = tournament.pools.all()
|
||||
if options['round']:
|
||||
pools = pools.filter(round=options['round'])
|
||||
if options['letter']:
|
||||
pools = pools.filter(letter=ord(options['letter']) - 64)
|
||||
for pool in pools.all():
|
||||
if options['verbosity'] >= 1:
|
||||
self.stdout.write(f"Parsing notation sheet for pool {pool.short_name} for {tournament}")
|
||||
pool.parse_spreadsheet()
|
||||
|
||||
sleep(1)
|
|
@ -422,9 +422,6 @@ class Tournament(models.Model):
|
|||
|
||||
def create_spreadsheet(self):
|
||||
if self.notes_sheet_id:
|
||||
gc = gspread.service_account_from_dict(settings.GOOGLE_SERVICE_CLIENT)
|
||||
spreadsheet = gc.open_by_key(self.notes_sheet_id)
|
||||
spreadsheet.update_locale("fr_FR")
|
||||
return self.notes_sheet_id
|
||||
|
||||
gc = gspread.service_account_from_dict(settings.GOOGLE_SERVICE_CLIENT)
|
||||
|
@ -955,6 +952,34 @@ class Pool(models.Model):
|
|||
worksheet.insert_row([str(jury), jury.id], max_row)
|
||||
max_row += 1
|
||||
|
||||
def parse_spreadsheet(self):
|
||||
gc = gspread.service_account_from_dict(settings.GOOGLE_SERVICE_CLIENT)
|
||||
self.tournament.create_spreadsheet()
|
||||
spreadsheet = gc.open_by_key(self.tournament.notes_sheet_id)
|
||||
worksheet = spreadsheet.worksheet(f"Poule {self.short_name}")
|
||||
|
||||
average_cell = worksheet.find("Moyenne")
|
||||
min_row = 5
|
||||
max_row = average_cell.row - 2
|
||||
data = worksheet.get_values(f"A{min_row}:AF{max_row}")
|
||||
if not data or not data[0]:
|
||||
return
|
||||
|
||||
passage_width = 7 if self.participations.count() == 4 else 6
|
||||
for line in data:
|
||||
jury_name = line[0]
|
||||
jury_id = line[1]
|
||||
if not jury_id.isnumeric() or int(jury_id) not in self.juries.values_list("id", flat=True):
|
||||
print(format_lazy(_("The jury {jury} is not part of the jury for this pool."), jury=jury_name))
|
||||
continue
|
||||
|
||||
jury = self.juries.get(id=jury_id)
|
||||
for i, passage in enumerate(self.passages.all()):
|
||||
note = passage.notes.get(jury=jury)
|
||||
note_line = line[2 + i * passage_width:2 + (i + 1) * passage_width]
|
||||
note.set_all(*note_line)
|
||||
note.save()
|
||||
|
||||
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
|
||||
self.update_juries_lines_spreadsheet()
|
||||
super().save(force_insert, force_update, using, update_fields)
|
||||
|
@ -1358,10 +1383,6 @@ class Note(models.Model):
|
|||
def has_any_note(self):
|
||||
return any(self.get_all())
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
super().save(*args, **kwargs)
|
||||
self.update_spreadsheet()
|
||||
|
||||
def __str__(self):
|
||||
return _("Notes of {jury} for {passage}").format(jury=self.jury, passage=self.passage)
|
||||
|
||||
|
|
|
@ -1101,7 +1101,7 @@ class PoolUploadNotesView(VolunteerMixin, FormView, DetailView):
|
|||
for i, passage in enumerate(pool.passages.all()):
|
||||
note = Note.objects.get_or_create(jury=vr, passage=passage)[0]
|
||||
passage_notes = notes[notes_count * i:notes_count * (i + 1)]
|
||||
note.set_all(*passage_notes)
|
||||
note.set_all(*list(map(int, passage_notes)))
|
||||
note.save()
|
||||
|
||||
messages.success(self.request, _("Notes were successfully uploaded."))
|
||||
|
@ -1967,3 +1967,8 @@ class NoteUpdateView(VolunteerMixin, UpdateView):
|
|||
# Set the note of the observer only for 4-teams pools
|
||||
del form.fields['observer_oral']
|
||||
return form
|
||||
|
||||
def form_valid(self, form):
|
||||
ret = super().form_valid(form)
|
||||
self.object.update_spreadsheet()
|
||||
return ret
|
||||
|
|
|
@ -15,5 +15,8 @@
|
|||
# Send reminders for payments
|
||||
30 6 * * 1 cd /code && python manage.py remind_payments &> /dev/null
|
||||
|
||||
# Check notation sheets every 15 minutes from 08:00 to 23:00 on fridays to mondays in april and may
|
||||
*/15 8-23 * 4-5 5,6,7,1 cd /code && python manage.py parse_notation_sheets -v 0
|
||||
|
||||
# Clean temporary files
|
||||
30 * * * * rm -rf /tmp/*
|
||||
|
|
Loading…
Reference in New Issue