phase3: add normalized domain schema, admin, services, and multistage docker build
This commit is contained in:
60
apps/players/admin.py
Normal file
60
apps/players/admin.py
Normal file
@ -0,0 +1,60 @@
|
||||
from django.contrib import admin
|
||||
|
||||
from .models import Nationality, Player, PlayerAlias, PlayerCareerEntry, Position, Role
|
||||
|
||||
|
||||
@admin.register(Nationality)
|
||||
class NationalityAdmin(admin.ModelAdmin):
|
||||
list_display = ("name", "iso2_code", "iso3_code")
|
||||
search_fields = ("name", "iso2_code", "iso3_code")
|
||||
|
||||
|
||||
@admin.register(Position)
|
||||
class PositionAdmin(admin.ModelAdmin):
|
||||
list_display = ("code", "name")
|
||||
search_fields = ("code", "name")
|
||||
|
||||
|
||||
@admin.register(Role)
|
||||
class RoleAdmin(admin.ModelAdmin):
|
||||
list_display = ("code", "name")
|
||||
search_fields = ("code", "name")
|
||||
|
||||
|
||||
class PlayerAliasInline(admin.TabularInline):
|
||||
model = PlayerAlias
|
||||
extra = 0
|
||||
|
||||
|
||||
class PlayerCareerEntryInline(admin.TabularInline):
|
||||
model = PlayerCareerEntry
|
||||
extra = 0
|
||||
|
||||
|
||||
@admin.register(Player)
|
||||
class PlayerAdmin(admin.ModelAdmin):
|
||||
list_display = (
|
||||
"full_name",
|
||||
"birth_date",
|
||||
"nationality",
|
||||
"nominal_position",
|
||||
"inferred_role",
|
||||
"is_active",
|
||||
)
|
||||
list_filter = ("is_active", "nationality", "nominal_position", "inferred_role")
|
||||
search_fields = ("full_name", "first_name", "last_name")
|
||||
inlines = (PlayerAliasInline, PlayerCareerEntryInline)
|
||||
|
||||
|
||||
@admin.register(PlayerAlias)
|
||||
class PlayerAliasAdmin(admin.ModelAdmin):
|
||||
list_display = ("alias", "player", "source", "is_primary")
|
||||
list_filter = ("is_primary", "source")
|
||||
search_fields = ("alias", "player__full_name")
|
||||
|
||||
|
||||
@admin.register(PlayerCareerEntry)
|
||||
class PlayerCareerEntryAdmin(admin.ModelAdmin):
|
||||
list_display = ("player", "team", "competition", "season", "start_date", "end_date")
|
||||
list_filter = ("competition", "season", "role_snapshot")
|
||||
search_fields = ("player__full_name", "team__name", "competition__name")
|
||||
113
apps/players/migrations/0001_initial.py
Normal file
113
apps/players/migrations/0001_initial.py
Normal file
@ -0,0 +1,113 @@
|
||||
# Generated by Django 5.2.12 on 2026-03-10 09:33
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('competitions', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Position',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('code', models.CharField(max_length=10, unique=True)),
|
||||
('name', models.CharField(max_length=80, unique=True)),
|
||||
],
|
||||
options={
|
||||
'ordering': ['code'],
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Role',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('code', models.CharField(max_length=32, unique=True)),
|
||||
('name', models.CharField(max_length=120, unique=True)),
|
||||
('description', models.TextField(blank=True)),
|
||||
],
|
||||
options={
|
||||
'ordering': ['name'],
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Nationality',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('name', models.CharField(max_length=120, unique=True)),
|
||||
('iso2_code', models.CharField(max_length=2, unique=True)),
|
||||
('iso3_code', models.CharField(blank=True, max_length=3, null=True, unique=True)),
|
||||
],
|
||||
options={
|
||||
'verbose_name_plural': 'Nationalities',
|
||||
'ordering': ['name'],
|
||||
'indexes': [models.Index(fields=['name'], name='players_nat_name_8688fe_idx'), models.Index(fields=['iso2_code'], name='players_nat_iso2_co_57069a_idx')],
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Player',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('first_name', models.CharField(max_length=120)),
|
||||
('last_name', models.CharField(max_length=120)),
|
||||
('full_name', models.CharField(max_length=260)),
|
||||
('birth_date', models.DateField(blank=True, null=True)),
|
||||
('height_cm', models.PositiveSmallIntegerField(blank=True, null=True)),
|
||||
('weight_kg', models.PositiveSmallIntegerField(blank=True, null=True)),
|
||||
('wingspan_cm', models.PositiveSmallIntegerField(blank=True, null=True)),
|
||||
('dominant_hand', models.CharField(choices=[('right', 'Right'), ('left', 'Left'), ('both', 'Both'), ('unknown', 'Unknown')], default='unknown', max_length=16)),
|
||||
('is_active', models.BooleanField(default=True)),
|
||||
('nationality', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='players', to='players.nationality')),
|
||||
],
|
||||
options={
|
||||
'ordering': ['full_name', 'id'],
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='PlayerAlias',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('alias', models.CharField(max_length=260)),
|
||||
('source', models.CharField(blank=True, max_length=80)),
|
||||
('is_primary', models.BooleanField(default=False)),
|
||||
('player', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='aliases', to='players.player')),
|
||||
],
|
||||
options={
|
||||
'ordering': ['alias'],
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='PlayerCareerEntry',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('shirt_number', models.PositiveSmallIntegerField(blank=True, null=True)),
|
||||
('start_date', models.DateField(blank=True, null=True)),
|
||||
('end_date', models.DateField(blank=True, null=True)),
|
||||
('notes', models.TextField(blank=True)),
|
||||
('competition', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='career_entries', to='competitions.competition')),
|
||||
('player', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='career_entries', to='players.player')),
|
||||
('season', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='career_entries', to='competitions.season')),
|
||||
],
|
||||
options={
|
||||
'ordering': ['player', '-start_date', '-id'],
|
||||
},
|
||||
),
|
||||
]
|
||||
121
apps/players/migrations/0002_initial.py
Normal file
121
apps/players/migrations/0002_initial.py
Normal file
@ -0,0 +1,121 @@
|
||||
# Generated by Django 5.2.12 on 2026-03-10 09:33
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('players', '0001_initial'),
|
||||
('teams', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='playercareerentry',
|
||||
name='team',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='career_entries', to='teams.team'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='position',
|
||||
index=models.Index(fields=['code'], name='players_pos_code_ae7cc3_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='position',
|
||||
index=models.Index(fields=['name'], name='players_pos_name_0b1fd3_idx'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='player',
|
||||
name='nominal_position',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='nominal_players', to='players.position'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='role',
|
||||
index=models.Index(fields=['code'], name='players_rol_code_51dc71_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='role',
|
||||
index=models.Index(fields=['name'], name='players_rol_name_bef503_idx'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='playercareerentry',
|
||||
name='role_snapshot',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='career_entries', to='players.role'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='player',
|
||||
name='inferred_role',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='role_players', to='players.role'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='playeralias',
|
||||
index=models.Index(fields=['alias'], name='players_pla_alias_c2e58f_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='playeralias',
|
||||
index=models.Index(fields=['source'], name='players_pla_source_dd2e52_idx'),
|
||||
),
|
||||
migrations.AddConstraint(
|
||||
model_name='playeralias',
|
||||
constraint=models.UniqueConstraint(fields=('player', 'alias'), name='uq_player_alias_per_player'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='playercareerentry',
|
||||
index=models.Index(fields=['player', 'start_date'], name='players_pla_player__80e21f_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='playercareerentry',
|
||||
index=models.Index(fields=['team', 'season'], name='players_pla_team_id_200f70_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='playercareerentry',
|
||||
index=models.Index(fields=['competition', 'season'], name='players_pla_competi_10cd9d_idx'),
|
||||
),
|
||||
migrations.AddConstraint(
|
||||
model_name='playercareerentry',
|
||||
constraint=models.UniqueConstraint(fields=('player', 'team', 'competition', 'season', 'start_date'), name='uq_player_career_entry_scope'),
|
||||
),
|
||||
migrations.AddConstraint(
|
||||
model_name='playercareerentry',
|
||||
constraint=models.CheckConstraint(condition=models.Q(('end_date__isnull', True), ('start_date__isnull', True), ('end_date__gte', models.F('start_date')), _connector='OR'), name='ck_career_entry_dates'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='player',
|
||||
index=models.Index(fields=['full_name'], name='players_pla_full_na_c03349_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='player',
|
||||
index=models.Index(fields=['last_name', 'first_name'], name='players_pla_last_na_1786cf_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='player',
|
||||
index=models.Index(fields=['birth_date'], name='players_pla_birth_d_354cf8_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='player',
|
||||
index=models.Index(fields=['nationality'], name='players_pla_nationa_2ab246_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='player',
|
||||
index=models.Index(fields=['nominal_position'], name='players_pla_nominal_ff9fb6_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='player',
|
||||
index=models.Index(fields=['inferred_role'], name='players_pla_inferre_136050_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='player',
|
||||
index=models.Index(fields=['is_active'], name='players_pla_is_acti_90e805_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='player',
|
||||
index=models.Index(fields=['height_cm'], name='players_pla_height__c4ae99_idx'),
|
||||
),
|
||||
migrations.AddConstraint(
|
||||
model_name='player',
|
||||
constraint=models.UniqueConstraint(fields=('full_name', 'birth_date'), name='uq_player_full_name_birth_date'),
|
||||
),
|
||||
]
|
||||
191
apps/players/models.py
Normal file
191
apps/players/models.py
Normal file
@ -0,0 +1,191 @@
|
||||
from django.db import models
|
||||
|
||||
|
||||
class TimeStampedModel(models.Model):
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
|
||||
class Nationality(TimeStampedModel):
|
||||
name = models.CharField(max_length=120, unique=True)
|
||||
iso2_code = models.CharField(max_length=2, unique=True)
|
||||
iso3_code = models.CharField(max_length=3, unique=True, blank=True, null=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ["name"]
|
||||
indexes = [models.Index(fields=["name"]), models.Index(fields=["iso2_code"])]
|
||||
verbose_name_plural = "Nationalities"
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.name} ({self.iso2_code})"
|
||||
|
||||
|
||||
class Position(TimeStampedModel):
|
||||
code = models.CharField(max_length=10, unique=True)
|
||||
name = models.CharField(max_length=80, unique=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ["code"]
|
||||
indexes = [models.Index(fields=["code"]), models.Index(fields=["name"])]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.code} - {self.name}"
|
||||
|
||||
|
||||
class Role(TimeStampedModel):
|
||||
code = models.CharField(max_length=32, unique=True)
|
||||
name = models.CharField(max_length=120, unique=True)
|
||||
description = models.TextField(blank=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ["name"]
|
||||
indexes = [models.Index(fields=["code"]), models.Index(fields=["name"])]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.name
|
||||
|
||||
|
||||
class Player(TimeStampedModel):
|
||||
class DominantHand(models.TextChoices):
|
||||
RIGHT = "right", "Right"
|
||||
LEFT = "left", "Left"
|
||||
BOTH = "both", "Both"
|
||||
UNKNOWN = "unknown", "Unknown"
|
||||
|
||||
first_name = models.CharField(max_length=120)
|
||||
last_name = models.CharField(max_length=120)
|
||||
full_name = models.CharField(max_length=260)
|
||||
birth_date = models.DateField(blank=True, null=True)
|
||||
nationality = models.ForeignKey(
|
||||
"players.Nationality",
|
||||
on_delete=models.SET_NULL,
|
||||
blank=True,
|
||||
null=True,
|
||||
related_name="players",
|
||||
)
|
||||
nominal_position = models.ForeignKey(
|
||||
"players.Position",
|
||||
on_delete=models.SET_NULL,
|
||||
blank=True,
|
||||
null=True,
|
||||
related_name="nominal_players",
|
||||
)
|
||||
inferred_role = models.ForeignKey(
|
||||
"players.Role",
|
||||
on_delete=models.SET_NULL,
|
||||
blank=True,
|
||||
null=True,
|
||||
related_name="role_players",
|
||||
)
|
||||
height_cm = models.PositiveSmallIntegerField(blank=True, null=True)
|
||||
weight_kg = models.PositiveSmallIntegerField(blank=True, null=True)
|
||||
wingspan_cm = models.PositiveSmallIntegerField(blank=True, null=True)
|
||||
dominant_hand = models.CharField(
|
||||
max_length=16,
|
||||
choices=DominantHand.choices,
|
||||
default=DominantHand.UNKNOWN,
|
||||
)
|
||||
is_active = models.BooleanField(default=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ["full_name", "id"]
|
||||
constraints = [
|
||||
models.UniqueConstraint(
|
||||
fields=["full_name", "birth_date"],
|
||||
name="uq_player_full_name_birth_date",
|
||||
)
|
||||
]
|
||||
indexes = [
|
||||
models.Index(fields=["full_name"]),
|
||||
models.Index(fields=["last_name", "first_name"]),
|
||||
models.Index(fields=["birth_date"]),
|
||||
models.Index(fields=["nationality"]),
|
||||
models.Index(fields=["nominal_position"]),
|
||||
models.Index(fields=["inferred_role"]),
|
||||
models.Index(fields=["is_active"]),
|
||||
models.Index(fields=["height_cm"]),
|
||||
]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.full_name
|
||||
|
||||
|
||||
class PlayerAlias(TimeStampedModel):
|
||||
player = models.ForeignKey("players.Player", on_delete=models.CASCADE, related_name="aliases")
|
||||
alias = models.CharField(max_length=260)
|
||||
source = models.CharField(max_length=80, blank=True)
|
||||
is_primary = models.BooleanField(default=False)
|
||||
|
||||
class Meta:
|
||||
ordering = ["alias"]
|
||||
constraints = [
|
||||
models.UniqueConstraint(fields=["player", "alias"], name="uq_player_alias_per_player")
|
||||
]
|
||||
indexes = [models.Index(fields=["alias"]), models.Index(fields=["source"])]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.alias} ({self.player_id})"
|
||||
|
||||
|
||||
class PlayerCareerEntry(TimeStampedModel):
|
||||
player = models.ForeignKey(
|
||||
"players.Player",
|
||||
on_delete=models.CASCADE,
|
||||
related_name="career_entries",
|
||||
)
|
||||
team = models.ForeignKey(
|
||||
"teams.Team",
|
||||
on_delete=models.SET_NULL,
|
||||
blank=True,
|
||||
null=True,
|
||||
related_name="career_entries",
|
||||
)
|
||||
competition = models.ForeignKey(
|
||||
"competitions.Competition",
|
||||
on_delete=models.SET_NULL,
|
||||
blank=True,
|
||||
null=True,
|
||||
related_name="career_entries",
|
||||
)
|
||||
season = models.ForeignKey(
|
||||
"competitions.Season",
|
||||
on_delete=models.SET_NULL,
|
||||
blank=True,
|
||||
null=True,
|
||||
related_name="career_entries",
|
||||
)
|
||||
role_snapshot = models.ForeignKey(
|
||||
"players.Role",
|
||||
on_delete=models.SET_NULL,
|
||||
blank=True,
|
||||
null=True,
|
||||
related_name="career_entries",
|
||||
)
|
||||
shirt_number = models.PositiveSmallIntegerField(blank=True, null=True)
|
||||
start_date = models.DateField(blank=True, null=True)
|
||||
end_date = models.DateField(blank=True, null=True)
|
||||
notes = models.TextField(blank=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ["player", "-start_date", "-id"]
|
||||
constraints = [
|
||||
models.UniqueConstraint(
|
||||
fields=["player", "team", "competition", "season", "start_date"],
|
||||
name="uq_player_career_entry_scope",
|
||||
),
|
||||
models.CheckConstraint(
|
||||
check=models.Q(end_date__isnull=True) | models.Q(start_date__isnull=True) | models.Q(end_date__gte=models.F("start_date")),
|
||||
name="ck_career_entry_dates",
|
||||
),
|
||||
]
|
||||
indexes = [
|
||||
models.Index(fields=["player", "start_date"]),
|
||||
models.Index(fields=["team", "season"]),
|
||||
models.Index(fields=["competition", "season"]),
|
||||
]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.player} | {self.team or '-'} | {self.season or '-'}"
|
||||
Reference in New Issue
Block a user