"""Player — registers for tryouts, manages their own profile.""" from app.models.user_model.user import User class Player(User): """Player — registers for tryouts, manages their own profile.""" __mapper_args__ = {'polymorphic_identity': 'player'} def get_visible_tryouts(self): from app.models.tryout.tryout import Tryout from app.models.match_model.match import Match from app.models.participant.match_participant import MatchParticipant # tryouts they registered for player_tryout_ids = [r.tryout_id for r in self.tryout_registrations.all()] tryouts = Tryout.query.filter( Tryout.id.in_(player_tryout_ids) ).order_by(Tryout.date).all() if player_tryout_ids else [] # plus tryouts where they participate in a match player_matches = Match.query.join(MatchParticipant).filter( MatchParticipant.player_id == self.id, ).all() extra_ids = set(m.tryout_id for m in player_matches) extra = Tryout.query.filter( Tryout.id.in_(extra_ids), ).order_by(Tryout.date).all() if extra_ids else [] all_ids = {t.id for t in tryouts} return tryouts + [t for t in extra if t.id not in all_ids]