from rest_framework import serializers from .models import Training, Attendance, TrainingExercise from utils.permissions import get_user_club class AttendanceSerializer(serializers.ModelSerializer): wrestler_name = serializers.SerializerMethodField() wrestler_first_name = serializers.SerializerMethodField() wrestler_last_name = serializers.SerializerMethodField() wrestler_group = serializers.SerializerMethodField() wrestler_club_name = serializers.SerializerMethodField() class Meta: model = Attendance fields = ['id', 'training', 'wrestler', 'wrestler_name', 'wrestler_first_name', 'wrestler_last_name', 'wrestler_group', 'wrestler_club_name', 'created_at'] def get_wrestler_name(self, obj): return str(obj.wrestler) def get_wrestler_first_name(self, obj): return obj.wrestler.first_name if hasattr(obj.wrestler, 'first_name') else None def get_wrestler_last_name(self, obj): return obj.wrestler.last_name if hasattr(obj.wrestler, 'last_name') else None def get_wrestler_group(self, obj): return obj.wrestler.group if hasattr(obj.wrestler, 'group') else None def get_wrestler_club_name(self, obj): return obj.wrestler.club.name if hasattr(obj.wrestler, 'club') and obj.wrestler.club else None class TrainingExerciseSerializer(serializers.ModelSerializer): exercise_name = serializers.CharField(source='exercise.name', read_only=True) exercise_category = serializers.CharField(source='exercise.category', read_only=True) class Meta: model = TrainingExercise fields = ['id', 'training', 'exercise', 'exercise_name', 'exercise_category', 'reps', 'time_minutes', 'order'] class TrainingSerializer(serializers.ModelSerializer): location_name = serializers.CharField(source='location.name', read_only=True) trainer_names = serializers.SerializerMethodField() attendance_count = serializers.SerializerMethodField() class Meta: model = Training fields = '__all__' read_only_fields = ['created_at', 'updated_at'] def get_trainer_names(self, obj): return [t.__str__() for t in obj.trainers.all()] def get_attendance_count(self, obj): return obj.attendances.count() def create(self, validated_data): request = self.context.get('request') if request and hasattr(request.user, 'profile') and request.user.profile.club: validated_data['club'] = request.user.profile.club return super().create(validated_data) class TrainingDetailSerializer(TrainingSerializer): attendances = AttendanceSerializer(many=True, read_only=True) exercise_count = serializers.SerializerMethodField() class Meta(TrainingSerializer.Meta): fields = ['id', 'date', 'start_time', 'end_time', 'club', 'location', 'location_name', 'trainers', 'trainer_names', 'template', 'group', 'notes', 'is_completed', 'created_at', 'updated_at', 'attendances', 'attendance_count', 'exercise_count'] def get_exercise_count(self, obj): return obj.training_exercises.count()