feat: add Django web app, CloudKit sync, dashboard, and game_datetime_utc export

Adds the full Django application layer on top of sportstime_parser:
- core: Sport, Team, Stadium, Game models with aliases and league structure
- scraper: orchestration engine, adapter, job management, Celery tasks
- cloudkit: CloudKit sync client, sync state tracking, sync jobs
- dashboard: staff dashboard for monitoring scrapers, sync, review queue
- notifications: email reports for scrape/sync results
- Docker setup for deployment (Dockerfile, docker-compose, entrypoint)

Game exports now use game_datetime_utc (ISO 8601 UTC) instead of
venue-local date+time strings, matching the canonical format used
by the iOS app.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Trey t
2026-02-19 14:04:27 -06:00
parent 4353d5943c
commit 63acf7accb
114 changed files with 13070 additions and 887 deletions

17
core/models/__init__.py Normal file
View File

@@ -0,0 +1,17 @@
from .sport import Sport
from .league_structure import Conference, Division
from .team import Team
from .stadium import Stadium
from .game import Game
from .alias import TeamAlias, StadiumAlias
__all__ = [
'Sport',
'Conference',
'Division',
'Team',
'Stadium',
'Game',
'TeamAlias',
'StadiumAlias',
]

169
core/models/alias.py Normal file
View File

@@ -0,0 +1,169 @@
from django.db import models
from simple_history.models import HistoricalRecords
class TeamAlias(models.Model):
"""
Historical team name aliases for resolution.
Handles team renames, relocations, and alternate names.
"""
ALIAS_TYPE_CHOICES = [
('full_name', 'Full Name'),
('city_name', 'City + Name'),
('abbreviation', 'Abbreviation'),
('nickname', 'Nickname'),
('historical', 'Historical Name'),
]
team = models.ForeignKey(
'core.Team',
on_delete=models.CASCADE,
related_name='aliases'
)
alias = models.CharField(
max_length=200,
help_text='The alias text to match against'
)
alias_type = models.CharField(
max_length=20,
choices=ALIAS_TYPE_CHOICES,
default='full_name'
)
valid_from = models.DateField(
null=True,
blank=True,
help_text='Date from which this alias is valid (inclusive)'
)
valid_until = models.DateField(
null=True,
blank=True,
help_text='Date until which this alias is valid (inclusive)'
)
is_primary = models.BooleanField(
default=False,
help_text='Whether this is a primary/preferred alias'
)
source = models.CharField(
max_length=200,
blank=True,
help_text='Source of this alias (e.g., ESPN, Basketball-Reference)'
)
notes = models.TextField(
blank=True,
help_text='Notes about this alias (e.g., relocation details)'
)
# Metadata
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
# Audit trail
history = HistoricalRecords()
class Meta:
ordering = ['team', '-valid_from']
verbose_name = 'Team Alias'
verbose_name_plural = 'Team Aliases'
indexes = [
models.Index(fields=['alias']),
models.Index(fields=['team', 'valid_from', 'valid_until']),
]
def __str__(self):
date_range = ""
if self.valid_from or self.valid_until:
start = self.valid_from.strftime('%Y') if self.valid_from else '...'
end = self.valid_until.strftime('%Y') if self.valid_until else 'present'
date_range = f" ({start}-{end})"
return f"{self.alias}{self.team.abbreviation}{date_range}"
def is_valid_for_date(self, check_date):
"""Check if this alias is valid for a given date."""
if self.valid_from and check_date < self.valid_from:
return False
if self.valid_until and check_date > self.valid_until:
return False
return True
class StadiumAlias(models.Model):
"""
Historical stadium name aliases for resolution.
Handles naming rights changes and alternate names.
"""
ALIAS_TYPE_CHOICES = [
('official', 'Official Name'),
('former', 'Former Name'),
('nickname', 'Nickname'),
('abbreviation', 'Abbreviation'),
]
stadium = models.ForeignKey(
'core.Stadium',
on_delete=models.CASCADE,
related_name='aliases'
)
alias = models.CharField(
max_length=200,
help_text='The alias text to match against'
)
alias_type = models.CharField(
max_length=20,
choices=ALIAS_TYPE_CHOICES,
default='official'
)
valid_from = models.DateField(
null=True,
blank=True,
help_text='Date from which this alias is valid (inclusive)'
)
valid_until = models.DateField(
null=True,
blank=True,
help_text='Date until which this alias is valid (inclusive)'
)
is_primary = models.BooleanField(
default=False,
help_text='Whether this is the current/primary name'
)
source = models.CharField(
max_length=200,
blank=True,
help_text='Source of this alias'
)
notes = models.TextField(
blank=True,
help_text='Notes about this alias (e.g., naming rights deal)'
)
# Metadata
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
# Audit trail
history = HistoricalRecords()
class Meta:
ordering = ['stadium', '-valid_from']
verbose_name = 'Stadium Alias'
verbose_name_plural = 'Stadium Aliases'
indexes = [
models.Index(fields=['alias']),
models.Index(fields=['stadium', 'valid_from', 'valid_until']),
]
def __str__(self):
date_range = ""
if self.valid_from or self.valid_until:
start = self.valid_from.strftime('%Y') if self.valid_from else '...'
end = self.valid_until.strftime('%Y') if self.valid_until else 'present'
date_range = f" ({start}-{end})"
return f"{self.alias}{self.stadium.name}{date_range}"
def is_valid_for_date(self, check_date):
"""Check if this alias is valid for a given date."""
if self.valid_from and check_date < self.valid_from:
return False
if self.valid_until and check_date > self.valid_until:
return False
return True

146
core/models/game.py Normal file
View File

@@ -0,0 +1,146 @@
from django.db import models
from simple_history.models import HistoricalRecords
class Game(models.Model):
"""
Game model representing a single game between two teams.
"""
STATUS_CHOICES = [
('scheduled', 'Scheduled'),
('in_progress', 'In Progress'),
('final', 'Final'),
('postponed', 'Postponed'),
('cancelled', 'Cancelled'),
('suspended', 'Suspended'),
]
id = models.CharField(
max_length=100,
primary_key=True,
help_text='Canonical ID (e.g., game_nba_2025_20251022_bos_lal)'
)
sport = models.ForeignKey(
'core.Sport',
on_delete=models.CASCADE,
related_name='games'
)
season = models.PositiveSmallIntegerField(
help_text='Season start year (e.g., 2025 for 2025-26 season)'
)
home_team = models.ForeignKey(
'core.Team',
on_delete=models.CASCADE,
related_name='home_games'
)
away_team = models.ForeignKey(
'core.Team',
on_delete=models.CASCADE,
related_name='away_games'
)
stadium = models.ForeignKey(
'core.Stadium',
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name='games'
)
game_date = models.DateTimeField(
help_text='Game date and time (UTC)'
)
game_number = models.PositiveSmallIntegerField(
null=True,
blank=True,
help_text='Game number for doubleheaders (1 or 2)'
)
home_score = models.PositiveSmallIntegerField(
null=True,
blank=True
)
away_score = models.PositiveSmallIntegerField(
null=True,
blank=True
)
status = models.CharField(
max_length=20,
choices=STATUS_CHOICES,
default='scheduled'
)
is_neutral_site = models.BooleanField(
default=False,
help_text='Whether game is at neutral site'
)
is_playoff = models.BooleanField(
default=False,
help_text='Whether this is a playoff game'
)
playoff_round = models.CharField(
max_length=50,
blank=True,
help_text='Playoff round (e.g., Finals, Conference Finals)'
)
# Raw scraped values (for debugging/review)
raw_home_team = models.CharField(
max_length=200,
blank=True,
help_text='Original scraped home team name'
)
raw_away_team = models.CharField(
max_length=200,
blank=True,
help_text='Original scraped away team name'
)
raw_stadium = models.CharField(
max_length=200,
blank=True,
help_text='Original scraped stadium name'
)
source_url = models.URLField(
blank=True,
help_text='URL where game was scraped from'
)
# Metadata
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
# Audit trail
history = HistoricalRecords()
class Meta:
ordering = ['-game_date', 'sport']
verbose_name = 'Game'
verbose_name_plural = 'Games'
indexes = [
models.Index(fields=['sport', 'season']),
models.Index(fields=['sport', 'game_date']),
models.Index(fields=['home_team', 'season']),
models.Index(fields=['away_team', 'season']),
models.Index(fields=['status']),
]
def __str__(self):
return f"{self.away_team.abbreviation} @ {self.home_team.abbreviation} - {self.game_date.strftime('%Y-%m-%d')}"
@property
def is_final(self):
return self.status == 'final'
@property
def winner(self):
"""Return winning team or None if not final."""
if not self.is_final or self.home_score is None or self.away_score is None:
return None
if self.home_score > self.away_score:
return self.home_team
elif self.away_score > self.home_score:
return self.away_team
return None # Tie
@property
def score_display(self):
"""Return score as 'away_score - home_score' or 'TBD'."""
if self.home_score is not None and self.away_score is not None:
return f"{self.away_score} - {self.home_score}"
return "TBD"

View File

@@ -0,0 +1,92 @@
from django.db import models
from simple_history.models import HistoricalRecords
class Conference(models.Model):
"""
Conference within a sport (e.g., Eastern, Western for NBA).
"""
sport = models.ForeignKey(
'core.Sport',
on_delete=models.CASCADE,
related_name='conferences'
)
canonical_id = models.CharField(
max_length=100,
blank=True,
db_index=True,
help_text='Canonical ID from bootstrap JSON (e.g., nba_eastern)'
)
name = models.CharField(max_length=50)
short_name = models.CharField(
max_length=10,
blank=True,
help_text='Short name (e.g., East, West)'
)
order = models.PositiveSmallIntegerField(
default=0,
help_text='Display order'
)
# Metadata
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
# Audit trail
history = HistoricalRecords()
class Meta:
ordering = ['sport', 'order', 'name']
unique_together = ['sport', 'name']
verbose_name = 'Conference'
verbose_name_plural = 'Conferences'
def __str__(self):
return f"{self.sport.short_name} - {self.name}"
class Division(models.Model):
"""
Division within a conference (e.g., Atlantic, Central for NBA East).
"""
conference = models.ForeignKey(
Conference,
on_delete=models.CASCADE,
related_name='divisions'
)
canonical_id = models.CharField(
max_length=100,
blank=True,
db_index=True,
help_text='Canonical ID from bootstrap JSON (e.g., nba_southeast)'
)
name = models.CharField(max_length=50)
short_name = models.CharField(
max_length=10,
blank=True,
help_text='Short name'
)
order = models.PositiveSmallIntegerField(
default=0,
help_text='Display order'
)
# Metadata
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
# Audit trail
history = HistoricalRecords()
class Meta:
ordering = ['conference', 'order', 'name']
unique_together = ['conference', 'name']
verbose_name = 'Division'
verbose_name_plural = 'Divisions'
def __str__(self):
return f"{self.conference.sport.short_name} - {self.conference.name} - {self.name}"
@property
def sport(self):
return self.conference.sport

78
core/models/sport.py Normal file
View File

@@ -0,0 +1,78 @@
from django.db import models
from simple_history.models import HistoricalRecords
class Sport(models.Model):
"""
Sport configuration model.
"""
SEASON_TYPE_CHOICES = [
('split', 'Split Year (e.g., 2024-25)'),
('single', 'Single Year (e.g., 2024)'),
]
code = models.CharField(
max_length=10,
primary_key=True,
help_text='Sport code (e.g., nba, mlb, nfl)'
)
name = models.CharField(
max_length=100,
help_text='Full name (e.g., National Basketball Association)'
)
short_name = models.CharField(
max_length=20,
help_text='Short name (e.g., NBA)'
)
season_type = models.CharField(
max_length=10,
choices=SEASON_TYPE_CHOICES,
help_text='Whether season spans two years or one'
)
expected_game_count = models.PositiveIntegerField(
default=0,
help_text='Expected number of regular season games'
)
season_start_month = models.PositiveSmallIntegerField(
default=1,
help_text='Month when season typically starts (1-12)'
)
season_end_month = models.PositiveSmallIntegerField(
default=12,
help_text='Month when season typically ends (1-12)'
)
icon_name = models.CharField(
max_length=50,
blank=True,
help_text='SF Symbol name (e.g., baseball.fill, basketball.fill)'
)
color_hex = models.CharField(
max_length=10,
blank=True,
help_text='Brand color hex (e.g., #CE1141)'
)
is_active = models.BooleanField(
default=True,
help_text='Whether this sport is actively being scraped'
)
# Metadata
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
# Audit trail
history = HistoricalRecords()
class Meta:
ordering = ['name']
verbose_name = 'Sport'
verbose_name_plural = 'Sports'
def __str__(self):
return self.short_name
def get_season_display(self, year: int) -> str:
"""Return display string for a season (e.g., '2024-25' or '2024')."""
if self.season_type == 'split':
return f"{year}-{str(year + 1)[-2:]}"
return str(year)

109
core/models/stadium.py Normal file
View File

@@ -0,0 +1,109 @@
from django.db import models
from simple_history.models import HistoricalRecords
class Stadium(models.Model):
"""
Stadium/Arena/Venue model.
"""
SURFACE_CHOICES = [
('grass', 'Natural Grass'),
('turf', 'Artificial Turf'),
('ice', 'Ice'),
('hardwood', 'Hardwood'),
('other', 'Other'),
]
ROOF_TYPE_CHOICES = [
('dome', 'Dome (Closed)'),
('retractable', 'Retractable'),
('open', 'Open Air'),
]
id = models.CharField(
max_length=100,
primary_key=True,
help_text='Canonical ID (e.g., stadium_nba_los_angeles_lakers)'
)
sport = models.ForeignKey(
'core.Sport',
on_delete=models.CASCADE,
related_name='stadiums'
)
name = models.CharField(
max_length=200,
help_text='Current stadium name'
)
city = models.CharField(max_length=100)
state = models.CharField(
max_length=100,
blank=True,
help_text='State/Province (blank for international)'
)
country = models.CharField(
max_length=100,
default='USA'
)
latitude = models.DecimalField(
max_digits=9,
decimal_places=6,
null=True,
blank=True
)
longitude = models.DecimalField(
max_digits=9,
decimal_places=6,
null=True,
blank=True
)
capacity = models.PositiveIntegerField(
null=True,
blank=True,
help_text='Seating capacity'
)
surface = models.CharField(
max_length=20,
choices=SURFACE_CHOICES,
blank=True
)
roof_type = models.CharField(
max_length=20,
choices=ROOF_TYPE_CHOICES,
blank=True
)
opened_year = models.PositiveSmallIntegerField(
null=True,
blank=True,
help_text='Year stadium opened'
)
timezone = models.CharField(
max_length=50,
blank=True,
help_text='IANA timezone (e.g., America/Los_Angeles)'
)
image_url = models.URLField(
blank=True,
help_text='URL to stadium image'
)
# Metadata
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
# Audit trail
history = HistoricalRecords()
class Meta:
ordering = ['sport', 'city', 'name']
verbose_name = 'Stadium'
verbose_name_plural = 'Stadiums'
def __str__(self):
return f"{self.name} ({self.city})"
@property
def location(self):
"""Return city, state/country string."""
if self.state:
return f"{self.city}, {self.state}"
return f"{self.city}, {self.country}"

88
core/models/team.py Normal file
View File

@@ -0,0 +1,88 @@
from django.db import models
from simple_history.models import HistoricalRecords
class Team(models.Model):
"""
Team model with canonical identifiers.
"""
id = models.CharField(
max_length=50,
primary_key=True,
help_text='Canonical ID (e.g., team_nba_lal)'
)
sport = models.ForeignKey(
'core.Sport',
on_delete=models.CASCADE,
related_name='teams'
)
division = models.ForeignKey(
'core.Division',
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name='teams'
)
city = models.CharField(
max_length=100,
help_text='Team city (e.g., Los Angeles)'
)
name = models.CharField(
max_length=100,
help_text='Team name (e.g., Lakers)'
)
full_name = models.CharField(
max_length=200,
help_text='Full team name (e.g., Los Angeles Lakers)'
)
abbreviation = models.CharField(
max_length=10,
help_text='Team abbreviation (e.g., LAL)'
)
home_stadium = models.ForeignKey(
'core.Stadium',
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name='home_teams'
)
primary_color = models.CharField(
max_length=7,
blank=True,
help_text='Primary color hex (e.g., #552583)'
)
secondary_color = models.CharField(
max_length=7,
blank=True,
help_text='Secondary color hex (e.g., #FDB927)'
)
logo_url = models.URLField(
blank=True,
help_text='URL to team logo'
)
is_active = models.BooleanField(
default=True,
help_text='Whether team is currently active'
)
# Metadata
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
# Audit trail
history = HistoricalRecords()
class Meta:
ordering = ['sport', 'city', 'name']
verbose_name = 'Team'
verbose_name_plural = 'Teams'
def __str__(self):
return self.full_name
@property
def conference(self):
"""Return team's conference via division."""
if self.division:
return self.division.conference
return None