Consider tweaks in notation sheet

Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
Emmy D'Anello 2024-03-30 18:57:05 +01:00
parent 5261a52401
commit f1012efcaa
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 59 additions and 13 deletions

View File

@ -36,4 +36,6 @@ class Command(BaseCommand):
self.stdout.write(f"Parsing notation sheet for pool {pool.short_name} for {tournament}")
pool.parse_spreadsheet()
tournament.parse_tweaks_spreadskeets()
sleep(1)

View File

@ -439,10 +439,10 @@ class Tournament(models.Model):
else:
worksheet = spreadsheet.worksheet("Classement final")
if worksheet.index < 100:
worksheet.update_index(self.pools.count() + 1)
if worksheet.index != self.pools.count():
worksheet.update_index(self.pools.count())
header = [["Équipe", "Points jour 1", "Points jour 2", "Total", "Harmonisation", "Total final", "Rang"]]
header = [["Équipe", "Score jour 1", "Harmonisation 1", "Score jour 2", "Harmonisation 2", "Total", "Rang"]]
lines = []
participations = self.participations.filter(pools__round=1, pools__tournament=self).all()
for i, participation in enumerate(participations):
@ -451,13 +451,19 @@ class Tournament(models.Model):
pool1 = self.pools.get(round=1, participations=participation)
passage1 = pool1.passages.get(defender=participation)
tweak1_qs = Tweak.objects.filter(pool=pool1, participation=participation)
tweak1 = tweak1_qs.get() if tweak1_qs.exists() else None
pool2 = self.pools.get(round=2, participations=participation)
passage2 = pool2.passages.get(defender=participation)
tweak2_qs = Tweak.objects.filter(pool=pool2, participation=participation)
tweak2 = tweak2_qs.get() if tweak2_qs.exists() else None
line.append(f"=SIERREUR('Poule {pool1.short_name}'!$D{pool1.juries.count() + 10 + passage1.position}; 0)")
line.append(tweak1.diff if tweak1 else 0)
line.append(f"=SIERREUR('Poule {pool2.short_name}'!$D{pool2.juries.count() + 10 + passage2.position}; 0)")
line.append(f"=$B{i + 2} + $C{i + 2}")
line.append(0)
line.append(f"=$D{i + 2} + $E{i + 2}")
line.append(tweak2.diff if tweak2 else 0)
line.append(f"=$B{i + 2} + $C{i + 2} + $D{i + 2} + E{i + 2}")
line.append(f"=RANG($F{i + 2}; $F$2:$F${participations.count() + 1})")
final_ranking = [["", "", ""], ["", "", ""], ["Équipe", "Score", "Rang"],
@ -475,7 +481,7 @@ class Tournament(models.Model):
format_requests = []
# Set the width of the columns
column_widths = [("A", 250), ("B", 100), ("C", 100), ("D", 100), ("E", 100), ("F", 100), ("G", 100)]
column_widths = [("A", 250), ("B", 120), ("C", 120), ("D", 120), ("E", 120), ("F", 120), ("G", 120)]
for column, width in column_widths:
grid_range = a1_range_to_grid_range(column, worksheet.id)
format_requests.append({
@ -531,7 +537,9 @@ class Tournament(models.Model):
# Set background color for headers and footers
bg_colors = [("A1:AF", (1, 1, 1)),
(f"A1:G1", (0.8, 0.8, 0.8)),
(f"A2:D{participations.count() + 1}", (0.9, 0.9, 0.9)),
(f"A2:B{participations.count() + 1}", (0.9, 0.9, 0.9)),
(f"C2:C{participations.count() + 1}", (1, 1, 1)),
(f"D2:D{participations.count() + 1}", (0.9, 0.9, 0.9)),
(f"E2:E{participations.count() + 1}", (1, 1, 1)),
(f"F2:G{participations.count() + 1}", (0.9, 0.9, 0.9)),
(f"A{participations.count() + 4}:C{participations.count() + 4}", (0.8, 0.8, 0.8)),
@ -547,13 +555,19 @@ class Tournament(models.Model):
})
# Set number format, display only one decimal
number_format_ranges = [f"B2:D{participations.count() + 1}", f"F2:F{participations.count() + 1}",
f"B{participations.count() + 5}:B{2 * participations.count() + 5}", ]
for number_format_range in number_format_ranges:
number_format_ranges = [(f"B2:B{participations.count() + 1}", "0.0"),
(f"C2:C{participations.count() + 1}", "0"),
(f"D2:D{participations.count() + 1}", "0.0"),
(f"E2:E{participations.count() + 1}", "0"),
(f"F2:F{participations.count() + 1}", "0.0"),
(f"G2:G{participations.count() + 1}", "0"),
(f"B{participations.count() + 5}:B{2 * participations.count() + 5}", "0.0"),
(f"C{participations.count() + 5}:C{2 * participations.count() + 5}", "0"), ]
for number_format_range, pattern in number_format_ranges:
format_requests.append({
"repeatCell": {
"range": a1_range_to_grid_range(number_format_range, worksheet.id),
"cell": {"userEnteredFormat": {"numberFormat": {"type": "NUMBER", "pattern": "0.0"}}},
"cell": {"userEnteredFormat": {"numberFormat": {"type": "NUMBER", "pattern": pattern}}},
"fields": "userEnteredFormat.numberFormat",
}
})
@ -567,7 +581,8 @@ class Tournament(models.Model):
})
# Protect the header, the juries list, the footer and the ranking
protected_ranges = [f"A1:G1", f"A2:D{participations.count() + 1}", f"F2:G{participations.count() + 1}",
protected_ranges = [f"A1:G1", f"A2:B{participations.count() + 1}",
f"D2:D{participations.count() + 1}", f"F2:G{participations.count() + 1}",
f"A{participations.count() + 4}:C{2 * participations.count() + 4}", ]
for protected_range in protected_ranges:
format_requests.append({
@ -584,6 +599,35 @@ class Tournament(models.Model):
body = {"requests": format_requests}
worksheet.client.batch_update(spreadsheet.id, body)
def parse_tweaks_spreadskeets(self):
gc = gspread.service_account_from_dict(settings.GOOGLE_SERVICE_CLIENT)
spreadsheet = gc.open_by_key(self.notes_sheet_id)
worksheet = spreadsheet.worksheet("Classement final")
score_cell = worksheet.find("Score")
max_row = score_cell.row - 3
data = worksheet.get_values(f"A2:E{max_row}")
for line in data:
trigram = line[0][-4:-1]
participation = self.participations.get(team__trigram=trigram)
pool1 = self.pools.get(round=1, participations=participation)
pool2 = self.pools.get(round=2, participations=participation)
tweak1_qs = Tweak.objects.filter(pool=pool1, participation=participation)
tweak2_qs = Tweak.objects.filter(pool=pool2, participation=participation)
tweak1_nb, tweak2_nb = int(line[2]), int(line[4])
if not tweak1_nb:
tweak1_qs.delete()
else:
tweak1_qs.update_or_create(defaults={'diff': tweak1_nb},
create_defaults={'diff': tweak1_nb, 'pool': pool1,
'participation': participation})
if not tweak2_nb:
tweak2_qs.delete()
else:
tweak2_qs.update_or_create(defaults={'diff': tweak2_nb},
create_defaults={'diff': tweak2_nb, 'pool': pool2,
'participation': participation})
def get_absolute_url(self):
return reverse_lazy("participation:tournament_detail", args=(self.pk,))