Fix combined search filter semantics across player season joins
This commit is contained in:
@ -3,11 +3,13 @@ from datetime import date, timedelta
|
||||
from django.db.models import (
|
||||
Case,
|
||||
DecimalField,
|
||||
Exists,
|
||||
ExpressionWrapper,
|
||||
F,
|
||||
FloatField,
|
||||
IntegerField,
|
||||
Max,
|
||||
OuterRef,
|
||||
Q,
|
||||
Value,
|
||||
When,
|
||||
@ -15,6 +17,7 @@ from django.db.models import (
|
||||
from django.db.models.functions import Coalesce
|
||||
|
||||
from apps.players.models import Player
|
||||
from apps.stats.models import PlayerSeason
|
||||
|
||||
METRIC_SORT_KEYS = {"ppg_desc", "ppg_asc", "mpg_desc", "mpg_asc"}
|
||||
|
||||
@ -31,15 +34,15 @@ def _years_ago_today(years: int) -> date:
|
||||
def _apply_min_max_filter(queryset, min_key: str, max_key: str, field_name: str, data: dict):
|
||||
min_value = data.get(min_key)
|
||||
max_value = data.get(max_key)
|
||||
if min_value is not None:
|
||||
if min_value not in (None, ""):
|
||||
queryset = queryset.filter(**{f"{field_name}__gte": min_value})
|
||||
if max_value is not None:
|
||||
if max_value not in (None, ""):
|
||||
queryset = queryset.filter(**{f"{field_name}__lte": max_value})
|
||||
return queryset
|
||||
|
||||
|
||||
def _needs_distinct(data: dict) -> bool:
|
||||
join_filter_keys = (
|
||||
def _season_scope_filter_keys() -> tuple[str, ...]:
|
||||
return (
|
||||
"q",
|
||||
"team",
|
||||
"competition",
|
||||
@ -69,7 +72,105 @@ def _needs_distinct(data: dict) -> bool:
|
||||
"efficiency_metric_min",
|
||||
"efficiency_metric_max",
|
||||
)
|
||||
return any(data.get(key) not in (None, "") for key in join_filter_keys)
|
||||
|
||||
|
||||
def _has_season_scope_filters(data: dict) -> bool:
|
||||
return any(data.get(key) not in (None, "") for key in _season_scope_filter_keys() if key != "q")
|
||||
|
||||
|
||||
def _apply_mpg_filter(queryset, *, games_field: str, minutes_field: str, min_value, max_value):
|
||||
if min_value not in (None, ""):
|
||||
queryset = queryset.filter(**{f"{games_field}__gt": 0}).filter(
|
||||
**{f"{minutes_field}__gte": F(games_field) * min_value}
|
||||
)
|
||||
if max_value not in (None, ""):
|
||||
queryset = queryset.filter(**{f"{games_field}__gt": 0}).filter(
|
||||
**{f"{minutes_field}__lte": F(games_field) * max_value}
|
||||
)
|
||||
return queryset
|
||||
|
||||
|
||||
def _apply_player_season_scope_filters(queryset, data: dict):
|
||||
if data.get("team"):
|
||||
queryset = queryset.filter(team=data["team"])
|
||||
if data.get("competition"):
|
||||
queryset = queryset.filter(competition=data["competition"])
|
||||
if data.get("season"):
|
||||
queryset = queryset.filter(season=data["season"])
|
||||
|
||||
queryset = _apply_min_max_filter(queryset, "games_played_min", "games_played_max", "games_played", data)
|
||||
queryset = _apply_mpg_filter(
|
||||
queryset,
|
||||
games_field="games_played",
|
||||
minutes_field="minutes_played",
|
||||
min_value=data.get("minutes_per_game_min"),
|
||||
max_value=data.get("minutes_per_game_max"),
|
||||
)
|
||||
|
||||
stat_pairs = (
|
||||
("points_per_game_min", "points_per_game_max", "stats__points"),
|
||||
("rebounds_per_game_min", "rebounds_per_game_max", "stats__rebounds"),
|
||||
("assists_per_game_min", "assists_per_game_max", "stats__assists"),
|
||||
("steals_per_game_min", "steals_per_game_max", "stats__steals"),
|
||||
("blocks_per_game_min", "blocks_per_game_max", "stats__blocks"),
|
||||
("turnovers_per_game_min", "turnovers_per_game_max", "stats__turnovers"),
|
||||
("fg_pct_min", "fg_pct_max", "stats__fg_pct"),
|
||||
("three_pct_min", "three_pct_max", "stats__three_pct"),
|
||||
("ft_pct_min", "ft_pct_max", "stats__ft_pct"),
|
||||
("efficiency_metric_min", "efficiency_metric_max", "stats__player_efficiency_rating"),
|
||||
)
|
||||
for min_key, max_key, field_name in stat_pairs:
|
||||
queryset = _apply_min_max_filter(queryset, min_key, max_key, field_name, data)
|
||||
|
||||
return queryset
|
||||
|
||||
|
||||
def _build_metric_context_filter(data: dict) -> Q:
|
||||
context_filter = Q()
|
||||
if data.get("team"):
|
||||
context_filter &= Q(player_seasons__team=data["team"])
|
||||
if data.get("competition"):
|
||||
context_filter &= Q(player_seasons__competition=data["competition"])
|
||||
if data.get("season"):
|
||||
context_filter &= Q(player_seasons__season=data["season"])
|
||||
|
||||
minmax_pairs = (
|
||||
("games_played_min", "games_played_max", "player_seasons__games_played"),
|
||||
("points_per_game_min", "points_per_game_max", "player_seasons__stats__points"),
|
||||
("rebounds_per_game_min", "rebounds_per_game_max", "player_seasons__stats__rebounds"),
|
||||
("assists_per_game_min", "assists_per_game_max", "player_seasons__stats__assists"),
|
||||
("steals_per_game_min", "steals_per_game_max", "player_seasons__stats__steals"),
|
||||
("blocks_per_game_min", "blocks_per_game_max", "player_seasons__stats__blocks"),
|
||||
("turnovers_per_game_min", "turnovers_per_game_max", "player_seasons__stats__turnovers"),
|
||||
("fg_pct_min", "fg_pct_max", "player_seasons__stats__fg_pct"),
|
||||
("three_pct_min", "three_pct_max", "player_seasons__stats__three_pct"),
|
||||
("ft_pct_min", "ft_pct_max", "player_seasons__stats__ft_pct"),
|
||||
(
|
||||
"efficiency_metric_min",
|
||||
"efficiency_metric_max",
|
||||
"player_seasons__stats__player_efficiency_rating",
|
||||
),
|
||||
)
|
||||
for min_key, max_key, field_name in minmax_pairs:
|
||||
min_value = data.get(min_key)
|
||||
max_value = data.get(max_key)
|
||||
if min_value not in (None, ""):
|
||||
context_filter &= Q(**{f"{field_name}__gte": min_value})
|
||||
if max_value not in (None, ""):
|
||||
context_filter &= Q(**{f"{field_name}__lte": max_value})
|
||||
|
||||
mpg_min = data.get("minutes_per_game_min")
|
||||
mpg_max = data.get("minutes_per_game_max")
|
||||
if mpg_min not in (None, ""):
|
||||
context_filter &= Q(player_seasons__games_played__gt=0) & Q(
|
||||
player_seasons__minutes_played__gte=F("player_seasons__games_played") * mpg_min
|
||||
)
|
||||
if mpg_max not in (None, ""):
|
||||
context_filter &= Q(player_seasons__games_played__gt=0) & Q(
|
||||
player_seasons__minutes_played__lte=F("player_seasons__games_played") * mpg_max
|
||||
)
|
||||
|
||||
return context_filter
|
||||
|
||||
|
||||
def filter_players(queryset, data: dict):
|
||||
@ -88,13 +189,6 @@ def filter_players(queryset, data: dict):
|
||||
if data.get("origin_team"):
|
||||
queryset = queryset.filter(origin_team=data["origin_team"])
|
||||
|
||||
if data.get("team"):
|
||||
queryset = queryset.filter(player_seasons__team=data["team"])
|
||||
if data.get("competition"):
|
||||
queryset = queryset.filter(player_seasons__competition=data["competition"])
|
||||
if data.get("season"):
|
||||
queryset = queryset.filter(player_seasons__season=data["season"])
|
||||
|
||||
queryset = _apply_min_max_filter(queryset, "height_min", "height_max", "height_cm", data)
|
||||
queryset = _apply_min_max_filter(queryset, "weight_min", "weight_max", "weight_kg", data)
|
||||
|
||||
@ -106,50 +200,22 @@ def filter_players(queryset, data: dict):
|
||||
earliest_birth = _years_ago_today(age_max + 1) + timedelta(days=1)
|
||||
queryset = queryset.filter(birth_date__gte=earliest_birth)
|
||||
|
||||
queryset = _apply_min_max_filter(
|
||||
queryset,
|
||||
"games_played_min",
|
||||
"games_played_max",
|
||||
"player_seasons__games_played",
|
||||
data,
|
||||
)
|
||||
|
||||
mpg_min = data.get("minutes_per_game_min")
|
||||
mpg_max = data.get("minutes_per_game_max")
|
||||
if mpg_min is not None:
|
||||
queryset = queryset.filter(player_seasons__games_played__gt=0).filter(
|
||||
player_seasons__minutes_played__gte=F("player_seasons__games_played") * mpg_min
|
||||
)
|
||||
if mpg_max is not None:
|
||||
queryset = queryset.filter(player_seasons__games_played__gt=0).filter(
|
||||
player_seasons__minutes_played__lte=F("player_seasons__games_played") * mpg_max
|
||||
if _has_season_scope_filters(data):
|
||||
scoped_seasons = _apply_player_season_scope_filters(
|
||||
PlayerSeason.objects.filter(player_id=OuterRef("pk")),
|
||||
data,
|
||||
)
|
||||
queryset = queryset.filter(Exists(scoped_seasons))
|
||||
|
||||
stat_pairs = (
|
||||
("points_per_game_min", "points_per_game_max", "player_seasons__stats__points"),
|
||||
("rebounds_per_game_min", "rebounds_per_game_max", "player_seasons__stats__rebounds"),
|
||||
("assists_per_game_min", "assists_per_game_max", "player_seasons__stats__assists"),
|
||||
("steals_per_game_min", "steals_per_game_max", "player_seasons__stats__steals"),
|
||||
("blocks_per_game_min", "blocks_per_game_max", "player_seasons__stats__blocks"),
|
||||
("turnovers_per_game_min", "turnovers_per_game_max", "player_seasons__stats__turnovers"),
|
||||
("fg_pct_min", "fg_pct_max", "player_seasons__stats__fg_pct"),
|
||||
("three_pct_min", "three_pct_max", "player_seasons__stats__three_pct"),
|
||||
("ft_pct_min", "ft_pct_max", "player_seasons__stats__ft_pct"),
|
||||
(
|
||||
"efficiency_metric_min",
|
||||
"efficiency_metric_max",
|
||||
"player_seasons__stats__player_efficiency_rating",
|
||||
),
|
||||
)
|
||||
for min_key, max_key, field_name in stat_pairs:
|
||||
queryset = _apply_min_max_filter(queryset, min_key, max_key, field_name, data)
|
||||
|
||||
if _needs_distinct(data):
|
||||
if query:
|
||||
return queryset.distinct()
|
||||
return queryset
|
||||
|
||||
|
||||
def annotate_player_metrics(queryset):
|
||||
def annotate_player_metrics(queryset, data: dict | None = None):
|
||||
data = data or {}
|
||||
context_filter = _build_metric_context_filter(data)
|
||||
|
||||
mpg_expression = Case(
|
||||
When(
|
||||
player_seasons__games_played__gt=0,
|
||||
@ -164,38 +230,38 @@ def annotate_player_metrics(queryset):
|
||||
|
||||
return queryset.annotate(
|
||||
games_played_value=Coalesce(
|
||||
Max("player_seasons__games_played"),
|
||||
Max("player_seasons__games_played", filter=context_filter),
|
||||
Value(0, output_field=IntegerField()),
|
||||
output_field=IntegerField(),
|
||||
),
|
||||
mpg_value=Coalesce(Max(mpg_expression), Value(0.0)),
|
||||
mpg_value=Coalesce(Max(mpg_expression, filter=context_filter), Value(0.0)),
|
||||
ppg_value=Coalesce(
|
||||
Max("player_seasons__stats__points"),
|
||||
Max("player_seasons__stats__points", filter=context_filter),
|
||||
Value(0, output_field=DecimalField(max_digits=6, decimal_places=2)),
|
||||
output_field=DecimalField(max_digits=6, decimal_places=2),
|
||||
),
|
||||
rpg_value=Coalesce(
|
||||
Max("player_seasons__stats__rebounds"),
|
||||
Max("player_seasons__stats__rebounds", filter=context_filter),
|
||||
Value(0, output_field=DecimalField(max_digits=6, decimal_places=2)),
|
||||
output_field=DecimalField(max_digits=6, decimal_places=2),
|
||||
),
|
||||
apg_value=Coalesce(
|
||||
Max("player_seasons__stats__assists"),
|
||||
Max("player_seasons__stats__assists", filter=context_filter),
|
||||
Value(0, output_field=DecimalField(max_digits=6, decimal_places=2)),
|
||||
output_field=DecimalField(max_digits=6, decimal_places=2),
|
||||
),
|
||||
spg_value=Coalesce(
|
||||
Max("player_seasons__stats__steals"),
|
||||
Max("player_seasons__stats__steals", filter=context_filter),
|
||||
Value(0, output_field=DecimalField(max_digits=6, decimal_places=2)),
|
||||
output_field=DecimalField(max_digits=6, decimal_places=2),
|
||||
),
|
||||
bpg_value=Coalesce(
|
||||
Max("player_seasons__stats__blocks"),
|
||||
Max("player_seasons__stats__blocks", filter=context_filter),
|
||||
Value(0, output_field=DecimalField(max_digits=6, decimal_places=2)),
|
||||
output_field=DecimalField(max_digits=6, decimal_places=2),
|
||||
),
|
||||
top_efficiency=Coalesce(
|
||||
Max("player_seasons__stats__player_efficiency_rating"),
|
||||
Max("player_seasons__stats__player_efficiency_rating", filter=context_filter),
|
||||
Value(0, output_field=DecimalField(max_digits=6, decimal_places=2)),
|
||||
output_field=DecimalField(max_digits=6, decimal_places=2),
|
||||
),
|
||||
|
||||
@ -48,7 +48,7 @@ class PlayerSearchView(ListView):
|
||||
|
||||
if form.is_valid():
|
||||
queryset = filter_players(queryset, form.cleaned_data)
|
||||
queryset = annotate_player_metrics(queryset)
|
||||
queryset = annotate_player_metrics(queryset, form.cleaned_data)
|
||||
queryset = apply_sorting(queryset, form.cleaned_data.get("sort", "name_asc"))
|
||||
else:
|
||||
queryset = annotate_player_metrics(queryset).order_by("full_name", "id")
|
||||
|
||||
Reference in New Issue
Block a user