Export notes on Google Sheet automatically

Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
Emmy D'Anello 2024-03-30 14:21:28 +01:00
parent c522387482
commit 37ad3cf8a6
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
1 changed files with 29 additions and 1 deletions

View File

@ -1,6 +1,7 @@
# Copyright (C) 2020 by Animath
# SPDX-License-Identifier: GPL-3.0-or-later
import asyncio
from datetime import date
import os
@ -15,6 +16,7 @@ from django.utils.crypto import get_random_string
from django.utils.text import format_lazy
from django.utils.translation import gettext_lazy as _
import gspread
from django.views.debug import ExceptionReporter
from gspread.utils import a1_range_to_grid_range, MergeType
from registration.models import Payment, VolunteerRegistration
from tfjm.lists import get_sympa_client
@ -736,7 +738,7 @@ class Pool(models.Model):
format_requests = []
# Merge cells
merge_cells = ["A1:B1"]
merge_cells = ["A1:B1", "A2:B2", "A3:B3"]
for i, passage in enumerate(passages):
merge_cells.append(f"{getcol(3 + i * passage_width)}1:{getcol(2 + passage_width + i * passage_width)}1")
@ -1262,6 +1264,28 @@ class Note(models.Model):
self.reporter_oral = reporter_oral
self.observer_oral = observer_oral
def update_spreadsheet(self):
gc = gspread.service_account_from_dict(settings.GOOGLE_SERVICE_CLIENT)
passage = Passage.objects.prefetch_related('pool__tournament', 'pool__participations').get(pk=self.passage.pk)
spreadsheet_id = passage.pool.tournament.notes_sheet_id
spreadsheet = gc.open_by_key(spreadsheet_id)
worksheet = spreadsheet.worksheet(f"Poule {passage.pool.short_name}")
jury_id_cell = worksheet.find(str(self.jury_id), in_column=2)
if not jury_id_cell:
raise ValueError("The jury ID cell was not found in the spreadsheet.")
jury_row = jury_id_cell.row
passage_width = 7 if passage.pool.participations.count() == 4 else 6
def getcol(number: int) -> str:
if number == 0:
return ''
return getcol((number - 1) // 26) + chr(65 + (number - 1) % 26)
min_col = getcol(3 + (self.passage.position - 1) * passage_width)
max_col = getcol(3 + self.passage.position * passage_width - 1)
worksheet.update([list(self.get_all())], f"{min_col}{jury_row}:{max_col}{jury_row}")
def get_absolute_url(self):
return reverse_lazy("participation:passage_detail", args=(self.passage.pk,))
@ -1272,6 +1296,10 @@ 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)