You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.3 KiB
72 lines
2.3 KiB
|
|
from django.db import models |
|
from django.conf import settings |
|
from django.utils.crypto import get_random_string |
|
|
|
|
|
def generate_coureur_id(): |
|
"""Generate a unique 10-character id for Coureur.""" |
|
# allowed chars: letters + digits |
|
# loop until we find an unused id (call happens at instance creation time) |
|
while True: |
|
s = get_random_string(10, allowed_chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') |
|
if not Coureur.objects.filter(id=s).exists(): |
|
return s |
|
|
|
|
|
class Course(models.Model): |
|
nom = models.CharField(max_length=100) |
|
date = models.DateField() |
|
depart = models.DateTimeField(null=True, blank=True) |
|
fin = models.DateTimeField(null=True, blank=True) |
|
|
|
TYPE_UNIQUE = 'unique' |
|
TYPE_MULTI = 'multi' |
|
TYPE_CHOICES = [ |
|
(TYPE_UNIQUE, 'Unique'), |
|
(TYPE_MULTI, 'Multi'), |
|
] |
|
type = models.CharField(max_length=6, choices=TYPE_CHOICES, default=TYPE_UNIQUE) |
|
|
|
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='courses') |
|
|
|
class Meta: |
|
unique_together = ('nom', 'date') |
|
ordering = ['-date', 'nom'] |
|
|
|
def __str__(self): |
|
return f"{self.nom} ({self.date})" |
|
|
|
|
|
class Arrivee(models.Model): |
|
course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='arrivees') |
|
coureur = models.ForeignKey('Coureur', on_delete=models.CASCADE) |
|
temps = models.DurationField() |
|
rang = models.PositiveIntegerField() |
|
# numéro de tour (pour les courses de type multi) |
|
tour = models.PositiveIntegerField(default=1) |
|
date_arrivee = models.DateTimeField(auto_now_add=True) |
|
|
|
class Meta: |
|
# Allow multiple arrivals for the same coureur in the same course |
|
# by including the tour in the uniqueness constraint |
|
unique_together = ('course', 'coureur', 'tour') |
|
ordering = ['rang'] |
|
|
|
def __str__(self): |
|
return f"{self.coureur.nom} - {self.course.nom} ({self.temps})" |
|
|
|
|
|
class Coureur(models.Model): |
|
# 10-char primary key |
|
id = models.CharField(primary_key=True, max_length=10, editable=False, default=generate_coureur_id) |
|
nom = models.CharField(max_length=100) |
|
prenom = models.CharField(max_length=100, blank=True) |
|
classe = models.CharField(max_length=50) |
|
|
|
class Meta: |
|
unique_together = ('nom', 'prenom', 'classe') |
|
ordering = ['nom', 'prenom', 'classe'] |
|
|
|
def __str__(self): |
|
return f"{self.nom} {self.prenom} ({self.classe})"
|
|
|