From 49fda3df494d98475c8597038315157a91f5f4a0 Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Wed, 17 Apr 2024 00:29:48 +0200 Subject: [PATCH] Add mentions Signed-off-by: Emmy D'Anello --- ...ion_mention_participation_mention_final.py | 27 ++++++++++ participation/models.py | 53 ++++++++++++++++--- .../participation/tournament_detail.html | 6 +++ 3 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 participation/migrations/0012_participation_mention_participation_mention_final.py diff --git a/participation/migrations/0012_participation_mention_participation_mention_final.py b/participation/migrations/0012_participation_mention_participation_mention_final.py new file mode 100644 index 0000000..93b9f15 --- /dev/null +++ b/participation/migrations/0012_participation_mention_participation_mention_final.py @@ -0,0 +1,27 @@ +# Generated by Django 5.0.3 on 2024-04-16 22:17 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("participation", "0011_remove_note_observer_oral_remove_passage_observer"), + ] + + operations = [ + migrations.AddField( + model_name="participation", + name="mention", + field=models.CharField( + blank=True, default="", max_length=255, verbose_name="mention" + ), + ), + migrations.AddField( + model_name="participation", + name="mention_final", + field=models.CharField( + blank=True, default="", max_length=255, verbose_name="mention (final)" + ), + ), + ] diff --git a/participation/models.py b/participation/models.py index 8261cec..b97b83b 100644 --- a/participation/models.py +++ b/participation/models.py @@ -474,21 +474,32 @@ class Tournament(models.Model): 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"], + final_ranking = [["", "", "", ""], ["", "", "", ""], ["Équipe", "Score", "Rang", "Mention"], [f"=SORT($A$2:$A${participations.count() + 1}; " f"$F$2:$F${participations.count() + 1}; FALSE)", f"=SORT($F$2:$F${participations.count() + 1}; " f"$F$2:$F${participations.count() + 1}; FALSE)", f"=SORT($G$2:$G${participations.count() + 1}; " f"$F$2:$F${participations.count() + 1}; FALSE)", ]] + final_ranking += [["", "", ""] for _i in range(participations.count() - 1)] + + notes = dict() + for participation in self.participations.filter(valid=True).all(): + note = sum(pool.average(participation) for pool in self.pools.filter(participations=participation).all()) + if note: + notes[participation] = note + sorted_notes = sorted(notes.items(), key=lambda x: x[1], reverse=True) + + for i, (participation, _note) in enumerate(sorted_notes): + final_ranking[i + 3].append(participation.mention if not self.final else participation.mention_final) data = header + lines + final_ranking - worksheet.update(data, f"A1:G{participations.count() + 5}", raw=False) + worksheet.update(data, f"A1:G{2 * participations.count() + 4}", raw=False) format_requests = [] # Set the width of the columns - column_widths = [("A", 300), ("B", 120), ("C", 120), ("D", 120), ("E", 120), ("F", 120), ("G", 120)] + column_widths = [("A", 300), ("B", 150), ("C", 150), ("D", 150), ("E", 150), ("F", 150), ("G", 150)] for column, width in column_widths: grid_range = a1_range_to_grid_range(column, worksheet.id) format_requests.append({ @@ -509,7 +520,7 @@ class Tournament(models.Model): # Set borders border_ranges = [("A1:AF", "0000"), (f"A1:G{participations.count() + 1}", "1111"), - (f"A{participations.count() + 4}:C{2 * participations.count() + 4}", "1111")] + (f"A{participations.count() + 4}:D{2 * participations.count() + 4}", "1111")] sides_names = ['top', 'bottom', 'left', 'right'] styles = ["NONE", "SOLID", "SOLID_MEDIUM", "SOLID_THICK", "DOUBLE"] for border_range, sides in border_ranges: @@ -531,7 +542,7 @@ class Tournament(models.Model): # Make titles bold bold_ranges = [("A1:AF", False), ("A1:G1", True), - (f"A{participations.count() + 4}:C{participations.count() + 4}", True)] + (f"A{participations.count() + 4}:D{participations.count() + 4}", True)] for bold_range, bold in bold_ranges: format_requests.append({ "repeatCell": { @@ -549,7 +560,7 @@ class Tournament(models.Model): (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)), + (f"A{participations.count() + 4}:D{participations.count() + 4}", (0.8, 0.8, 0.8)), (f"A{participations.count() + 5}:C{2 * participations.count() + 4}", (0.9, 0.9, 0.9)),] for bg_range, bg_color in bg_colors: r, g, b = bg_color @@ -646,6 +657,22 @@ class Tournament(models.Model): create_defaults={'diff': tweak2_nb, 'pool': pool2, 'participation': participation}) + nb_participations = self.participations.filter(valid=True).count() + mentions = worksheet.get_values(f"A{score_cell.row + 1}:D{score_cell.row + nb_participations}") + notes = dict() + for participation in self.participations.filter(valid=True).all(): + note = sum(pool.average(participation) for pool in self.pools.filter(participations=participation).all()) + if note: + notes[participation] = note + sorted_notes = sorted(notes.items(), key=lambda x: x[1], reverse=True) + for i, (participation, _note) in enumerate(sorted_notes): + mention = mentions[i][3] + if not self.final: + participation.mention = mention + else: + participation.mention_final = mention + participation.save() + def get_absolute_url(self): return reverse_lazy("participation:tournament_detail", args=(self.pk,)) @@ -693,6 +720,20 @@ class Participation(models.Model): help_text=_("The team is selected for the final tournament."), ) + mention = models.CharField( + verbose_name=_("mention"), + max_length=255, + blank=True, + default="", + ) + + mention_final = models.CharField( + verbose_name=_("mention (final)"), + max_length=255, + blank=True, + default="", + ) + def get_absolute_url(self): return reverse_lazy("participation:participation_detail", args=(self.pk,)) diff --git a/participation/templates/participation/tournament_detail.html b/participation/templates/participation/tournament_detail.html index e889bc2..88d22a8 100644 --- a/participation/templates/participation/tournament_detail.html +++ b/participation/templates/participation/tournament_detail.html @@ -105,6 +105,12 @@ {% for participation, note in notes %}
  • {{ participation.team }} : {{ note|floatformat }} + {% if not tournament.final and participation.mention %} + — {{ participation.mention }} + {% endif %} + {% if tournament.final and participation.mention_final %} + — {{ participation.mention_final }} + {% endif %} {% if participation.final %}