99 lines
3.4 KiB
Python
99 lines
3.4 KiB
Python
from django.db import models
|
|
|
|
|
|
class Competition(models.Model):
|
|
class CompetitionType(models.TextChoices):
|
|
LEAGUE = "league", "League"
|
|
CUP = "cup", "Cup"
|
|
INTERNATIONAL = "international", "International"
|
|
|
|
class Gender(models.TextChoices):
|
|
MEN = "men", "Men"
|
|
WOMEN = "women", "Women"
|
|
MIXED = "mixed", "Mixed"
|
|
|
|
name = models.CharField(max_length=220)
|
|
slug = models.SlugField(max_length=240, unique=True)
|
|
competition_type = models.CharField(max_length=24, choices=CompetitionType.choices)
|
|
gender = models.CharField(max_length=16, choices=Gender.choices, default=Gender.MEN)
|
|
level = models.PositiveSmallIntegerField(default=1)
|
|
country = models.ForeignKey(
|
|
"players.Nationality",
|
|
on_delete=models.SET_NULL,
|
|
blank=True,
|
|
null=True,
|
|
related_name="competitions",
|
|
)
|
|
is_active = models.BooleanField(default=True)
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
updated_at = models.DateTimeField(auto_now=True)
|
|
|
|
class Meta:
|
|
ordering = ["name"]
|
|
constraints = [
|
|
models.UniqueConstraint(fields=["name", "country"], name="uq_competition_name_country")
|
|
]
|
|
indexes = [
|
|
models.Index(fields=["name"]),
|
|
models.Index(fields=["country"]),
|
|
models.Index(fields=["competition_type"]),
|
|
models.Index(fields=["gender"]),
|
|
models.Index(fields=["is_active"]),
|
|
]
|
|
|
|
def __str__(self) -> str:
|
|
return self.name
|
|
|
|
|
|
class Season(models.Model):
|
|
label = models.CharField(max_length=40, unique=True)
|
|
start_date = models.DateField()
|
|
end_date = models.DateField()
|
|
is_current = models.BooleanField(default=False)
|
|
|
|
class Meta:
|
|
ordering = ["-start_date"]
|
|
constraints = [
|
|
models.CheckConstraint(check=models.Q(end_date__gte=models.F("start_date")), name="ck_season_dates")
|
|
]
|
|
indexes = [
|
|
models.Index(fields=["is_current"]),
|
|
models.Index(fields=["start_date"]),
|
|
models.Index(fields=["end_date"]),
|
|
]
|
|
|
|
def __str__(self) -> str:
|
|
return self.label
|
|
|
|
|
|
class TeamSeason(models.Model):
|
|
team = models.ForeignKey("teams.Team", on_delete=models.CASCADE, related_name="team_seasons")
|
|
season = models.ForeignKey("competitions.Season", on_delete=models.CASCADE, related_name="team_seasons")
|
|
competition = models.ForeignKey(
|
|
"competitions.Competition",
|
|
on_delete=models.CASCADE,
|
|
related_name="team_seasons",
|
|
)
|
|
standing = models.PositiveSmallIntegerField(blank=True, null=True)
|
|
wins = models.PositiveSmallIntegerField(blank=True, null=True)
|
|
losses = models.PositiveSmallIntegerField(blank=True, null=True)
|
|
points = models.PositiveSmallIntegerField(blank=True, null=True)
|
|
coach_name = models.CharField(max_length=140, blank=True)
|
|
|
|
class Meta:
|
|
ordering = ["competition", "season", "team"]
|
|
constraints = [
|
|
models.UniqueConstraint(
|
|
fields=["team", "season", "competition"],
|
|
name="uq_team_season_competition",
|
|
)
|
|
]
|
|
indexes = [
|
|
models.Index(fields=["team", "season"]),
|
|
models.Index(fields=["season", "competition"]),
|
|
models.Index(fields=["competition", "standing"]),
|
|
]
|
|
|
|
def __str__(self) -> str:
|
|
return f"{self.team} - {self.season} - {self.competition}"
|