Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: vastly reduces number of db queries #252

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 14 additions & 49 deletions agagd/agagd_core/tables/games.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,14 @@
import django_tables2 as tables


# Column for the Winner of the Game
class LinkFullMembersNameColumn(tables.Column):
def __init__(
self,
color="W",
viewname=None,
urlconf=None,
args=None,
kwargs=None,
current_app=None,
attrs=None,
**extra,
):
super().__init__(
attrs=attrs,
linkify=dict(
viewname=viewname,
urlconf=urlconf,
args=args,
kwargs=kwargs,
current_app=current_app,
),
**extra,
)
self.color = color

def render(self, value, record):
if record["result"] == self.color:
self.attrs["td"] = {"class": "winner"}
elif record["result"] != self.color:
self.attrs["td"] = {"class": "runner-up"}
def class_player_1(record):
"""returns td class for player 1 (white)"""
return "winner" if record["result"] == "W" else "runner-up"

try:
member_name_and_id = agagd_models.Member.objects.values("full_name").get(
member_id=value
)
value = f"{member_name_and_id['full_name']} ({value})"
except ObjectDoesNotExist:
value = None

return value
def class_player_2(record):
"""returns td class for player 2 (black)"""
return "winner" if record["result"] == "B" else "runner-up"


# Basic table which is use as as base for many of the game layouts.
Expand All @@ -54,17 +21,15 @@ class GamesTable(tables.Table):
handicap = tables.Column(
attrs=django_tables2_styles.default_bootstrap_column_attrs, orderable=False
)
pin_player_1 = LinkFullMembersNameColumn(
color="W",
full_name_and_id_1 = tables.Column(
verbose_name="White",
viewname="players_profile",
kwargs={"player_id": tables.A("pin_player_1")},
linkify=("players_profile", [tables.A("pin_player_1")]),
attrs={"td": {"class": class_player_1}},
)
pin_player_2 = LinkFullMembersNameColumn(
color="B",
full_name_and_id_2 = tables.Column(
verbose_name="Black",
viewname="players_profile",
kwargs={"player_id": tables.A("pin_player_2")},
linkify=("players_profile", [tables.A("pin_player_2")]),
attrs={"td": {"class": class_player_2}},
)
tournament_code = tables.Column(
verbose_name="Tournament",
Expand All @@ -74,8 +39,8 @@ class GamesTable(tables.Table):
class Meta:
attrs = django_tables2_styles.default_bootstrap_header_column_attrs
fields = (
"pin_player_1",
"pin_player_2",
"full_name_and_id_1",
"full_name_and_id_2",
"tournament_code",
"handicap",
"game_date",
Expand Down
25 changes: 19 additions & 6 deletions agagd/agagd_core/tables/players.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,26 @@ class Meta:


class PlayersOpponentTable(tables.Table):
opponent = tables.Column(
opponent_id = tables.Column(
verbose_name="Opponent",
orderable=False,
linkify={
"viewname": "players_profile",
"args": [tables.A("opponent.member_id")],
"args": [tables.A("opponent_id")],
},
)
total = tables.Column(verbose_name="Games")
won = tables.Column(verbose_name="Won", default=0)
lost = tables.Column(verbose_name="Lost")
ratio = tables.Column(verbose_name="Rate", default=0, empty_values=(-1,))

def render_opponent_id(self, record):
opponent_full_name = record["opponent_full_name"]
opponent_id = record["opponent_id"]
return f"{opponent_full_name} ({opponent_id})"

def render_ratio(self, record):
ratio = record["won"] / record["total"]

return f"{ratio:.2f}"

class Meta:
Expand All @@ -44,16 +49,24 @@ class Meta:


class PlayersTournamentTable(tables.Table):
tournament = tables.Column(
linkify=("tournament_detail", [tables.A("tournament.pk")])
tournament_code = tables.Column(
verbose_name="Tournament",
linkify=("tournament_detail", [tables.A("tournament_code")]),
)
date = tables.Column(default="Unknown")
won = tables.Column(verbose_name="Won", default=0)
lost = tables.Column(verbose_name="Lost", default=0)

def render_tournament_code(self, record):
tournament_code = record["tournament_code"]
tournament_date = record["tournament_date"]
tournament_total_players = record["tournament_total_players"]

return f"{tournament_code} - on {tournament_date} with {tournament_total_players} players"

class Meta:
attrs = django_tables2_styles.default_bootstrap_header_column_attrs
fields = ("date", "tournament", "won", "lost")
fields = ("date", "tournament_code", "won", "lost")
orderable = False
template_name = "django_tables2/bootstrap4.html"
sequence = fields
30 changes: 18 additions & 12 deletions agagd/agagd_core/tables/tournaments.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import agagd_core.defaults.styles.django_tables2 as django_tables2_styles
import agagd_core.models as agagd_models
import django_tables2 as tables
from agagd_core.tables.games import LinkFullMembersNameColumn


class TournamentsTable(tables.Table):
Expand Down Expand Up @@ -51,19 +50,26 @@ class Meta:
template_name = "tournament_detail_information.html"


def class_player_1(record):
"""returns td class for player 1 (white)"""
return "winner" if record["result"] == "W" else "runner-up"


def class_player_2(record):
"""returns td class for player 2 (black)"""
return "winner" if record["result"] == "B" else "runner-up"


class TournamentsGamesTable(tables.Table):
pin_player_1 = LinkFullMembersNameColumn(
color="W",
full_name_and_id_1 = tables.Column(
verbose_name="White",
viewname="players_profile",
kwargs={"player_id": tables.A("pin_player_1")},
linkify=("players_profile", [tables.A("pin_player_1")]),
attrs={"td": {"class": class_player_1}},
)

pin_player_2 = LinkFullMembersNameColumn(
color="B",
full_name_and_id_2 = tables.Column(
verbose_name="Black",
viewname="players_profile",
kwargs={"player_id": tables.A("pin_player_2")},
linkify=("players_profile", [tables.A("pin_player_2")]),
attrs={"td": {"class": class_player_2}},
)

def render_result(self, value):
Expand All @@ -77,8 +83,8 @@ class Meta:
attrs = django_tables2_styles.default_bootstrap_header_column_attrs
fields = (
"game_date",
"pin_player_1",
"pin_player_2",
"full_name_and_id_1",
"full_name_and_id_2",
"handicap",
"komi",
"result",
Expand Down
17 changes: 17 additions & 0 deletions agagd/agagd_core/views/frontpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
from agagd_core.tables.players import PlayersTournamentTable
from agagd_core.tables.top_players import TopDanTable, TopKyuTable
from agagd_core.tables.tournaments import TournamentsTable
from django.db.models import CharField
from django.db.models import Value as V
from django.db.models.functions import Concat
from django.shortcuts import render
from django.template.response import TemplateResponse

Expand All @@ -27,6 +30,20 @@ def __get_latest_games(self):
"pin_player_2",
"tournament_code",
"result",
full_name_and_id_1=Concat(
"pin_player_1__full_name",
V(" ("),
"pin_player_1",
V(")"),
output_field=CharField(),
),
full_name_and_id_2=Concat(
"pin_player_2__full_name",
V(" ("),
"pin_player_2",
V(")"),
output_field=CharField(),
),
).order_by("-game_date")[:20]

return latest_games
Expand Down