from django.db import models from django.core.exceptions import ValidationError class Training(models.Model): date = models.DateField() start_time = models.TimeField() end_time = models.TimeField() club = models.ForeignKey('clubs.Club', on_delete=models.CASCADE, related_name='trainings', null=True, blank=True) location = models.ForeignKey('locations.Location', on_delete=models.SET_NULL, null=True, blank=True, related_name='trainings') trainers = models.ManyToManyField('trainers.Trainer', related_name='trainings', blank=True) template = models.ForeignKey('templates.TrainingTemplate', on_delete=models.SET_NULL, null=True, blank=True, related_name='trainings') group = models.CharField(max_length=20, choices=[ ('kids', 'Kids'), ('youth', 'Youth'), ('adults', 'Adults'), ('all', 'All'), ], default='all') notes = models.TextField(blank=True) is_completed = models.BooleanField(default=False) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: ordering = ['-date', '-start_time'] indexes = [ models.Index(fields=['date']), models.Index(fields=['group']), models.Index(fields=['club']), ] def __str__(self): return f"{self.date} - {self.get_group_display()}" def clean(self): if self.start_time and self.end_time and self.end_time <= self.start_time: raise ValidationError({'end_time': 'End time must be greater than start time.'}) def save(self, *args, **kwargs): self.full_clean() super().save(*args, **kwargs) class Attendance(models.Model): training = models.ForeignKey(Training, on_delete=models.CASCADE, related_name='attendances') wrestler = models.ForeignKey('wrestlers.Wrestler', on_delete=models.CASCADE, related_name='attendances') created_at = models.DateTimeField(auto_now_add=True) class Meta: unique_together = ['training', 'wrestler'] def __str__(self): return f"{self.wrestler} - {self.training.date}" class TrainingExercise(models.Model): training = models.ForeignKey(Training, on_delete=models.CASCADE, related_name='training_exercises') exercise = models.ForeignKey('exercises.Exercise', on_delete=models.CASCADE, related_name='training_exercises') reps = models.PositiveIntegerField(null=True, blank=True) time_minutes = models.PositiveIntegerField(null=True, blank=True) order = models.IntegerField(default=0) class Meta: ordering = ['order', 'id'] unique_together = ['training', 'exercise'] def __str__(self): return f"{self.training} - {self.exercise.name}"