from __future__ import annotations from datetime import date from django.core.management.base import BaseCommand from django.db import transaction from scouting.models import ( Competition, Player, PlayerSeason, PlayerSeasonStats, Role, Season, Specialty, Team, ) from scouting.sample_data.scouting_seed import COMPETITIONS, PLAYERS, ROLES, SEASONS, SPECIALTIES, TEAMS class Command(BaseCommand): help = "Load a curated scouting sample dataset for local development." @transaction.atomic def handle(self, *args, **options): roles = {} for record in ROLES: role, _ = Role.objects.update_or_create( slug=record["slug"], defaults={ "name": record["name"], "description": record.get("description", ""), }, ) roles[role.name] = role specialties = {} for record in SPECIALTIES: specialty, _ = Specialty.objects.update_or_create( slug=record["slug"], defaults={ "name": record["name"], "description": record.get("description", ""), }, ) specialties[specialty.name] = specialty competitions = {} for record in COMPETITIONS: competition, _ = Competition.objects.update_or_create( name=record["name"], defaults={ "country": record.get("country", ""), "level": record.get("level", ""), }, ) competitions[competition.name] = competition teams = {} for record in TEAMS: team, _ = Team.objects.update_or_create( name=record["name"], country=record.get("country", ""), defaults={}, ) teams[(team.name, team.country)] = team seasons = {} for record in SEASONS: season, _ = Season.objects.update_or_create( name=record["name"], defaults={ "start_year": record["start_year"], "end_year": record["end_year"], }, ) seasons[season.name] = season for record in PLAYERS: defaults = { "first_name": record.get("first_name", ""), "last_name": record.get("last_name", ""), "birth_date": date.fromisoformat(record["birth_date"]) if record.get("birth_date") else None, "nationality": record.get("nationality", ""), "height_cm": record.get("height_cm"), "weight_kg": record.get("weight_kg"), "wingspan_cm": record.get("wingspan_cm"), "position": record["position"], } player, _ = Player.objects.update_or_create( full_name=record["full_name"], defaults=defaults, ) player.roles.set([roles[name] for name in record.get("roles", [])]) player.specialties.set([specialties[name] for name in record.get("specialties", [])]) for context_record in record.get("contexts", []): context, _ = PlayerSeason.objects.update_or_create( player=player, season=seasons[context_record["season"]], team=teams[context_record["team"]], competition=competitions[context_record["competition"]], defaults={}, ) PlayerSeasonStats.objects.update_or_create( player_season=context, defaults=context_record["stats"], ) self.stdout.write( self.style.SUCCESS( "Seeded scouting sample data: " f"{Player.objects.count()} players, " f"{PlayerSeason.objects.count()} contexts, " f"{PlayerSeasonStats.objects.count()} stat lines." ) ) self.stdout.write( "Suggested filters: " "PG + min assists, C + min blocks, SG/wing + min TS%, or role + specialty combinations." )