"""Coach — evaluates, schedules matches, manages their own org team.""" from app.models.user_model.user import User from app.extensions import db class Coach(User): """Coach — evaluates, schedules matches, manages their own org team.""" __mapper_args__ = {'polymorphic_identity': 'coach'} def can_evaluate(self): return True def can_schedule_matches(self): return True def can_manage_tryouts(self): return True def can_manage_this_tryout(self, tryout): from app.models.org_team.org_team import OrgTeam if tryout.target_org_team_id: is_coach_of_target = OrgTeam.query.filter( OrgTeam.id == tryout.target_org_team_id, OrgTeam.coaches.any(id=self.id), ).first() is not None if is_coach_of_target: return True # Check many-to-many coaches relationship if any(c.id == self.id for c in tryout.coaches): return True # Backward compat: check deprecated coach_id if tryout.coach_id == self.id: return True return False def can_manage_this_org_team(self, org_team): if org_team.coaches.filter_by(id=self.id).first(): return True if org_team.coach_id == self.id: return True return False def get_visible_tryouts(self): from app.models.tryout.tryout import Tryout from app.models._associations import tryout_coaches team_ids = [t.id for t in self.coached_org_teams.all()] conditions = [] if team_ids: conditions.append(Tryout.target_org_team_id.in_(team_ids)) # Check many-to-many coaches conditions.append(Tryout.coaches.any(id=self.id)) # Backward compat: check deprecated coach_id conditions.append(Tryout.coach_id == self.id) return Tryout.query.filter(db.or_(*conditions)).order_by(Tryout.date).all()