class Team(db.Model, InfoMixin): __tablename__ = 'team' _simple_fields = ['id', 'name'] _complex_fields = ['coaches', 'athletes'] id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Text) coaches = db.relationship("Coach", secondary=team_association_table, back_populates='teams') athletes = db.relationship('Athlete', backref='team') def info(self, fields=None): return self._info_complex(fields=fields)
class Survey(db.Model, InfoMixin): __tablename__ = 'survey' _simple_fields = ['id', 'question'] _complex_fields = ['options', 'assignees'] id = db.Column(db.Integer, primary_key=True) question = db.Column(db.Text, nullable=False) # can get all survey responses by survey.options.answers options = db.relationship('SurveyOptions', backref='survey') assignees = db.relationship("Athlete", secondary=survey_assignment_table, back_populates='surveys') @property def answers(self): return self.options.answers def info(self, fields=None): return self._info_complex(fields=fields)
class SurveyOptions(db.Model): id = db.Column(db.Integer, primary_key=True) survey_id = db.Column(db.Integer, db.ForeignKey('survey.id'), nullable=False) option = db.Column(db.Text, nullable=False) score = db.Column(db.Integer, nullable=False) # lazy='joined' makes sure that when an option is queried, all its # matching answers come back on the same query answers = db.relationship('SurveyAnswers', backref='choice', lazy='joined')
class Athlete(User): __tablename__ = 'athlete' _simple_fields = User._info_fields + ['team_id'] _complex_fields = ['surveys'] id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True) team_id = db.Column(db.Integer, db.ForeignKey('team.id')) surveys = db.relationship("Survey", secondary=survey_assignment_table, back_populates='assignees') def info(self, fields=None): i = self._info_complex(fields=fields) i.update({"type": "athlete"}) return i
class Coach(User): # have to specify, as we need a reference to this table for the M2M relationship between coaches and teams. __tablename__ = 'coach' _simple_fields = User._info_fields _complex_fields = ['teams'] id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True) teams = db.relationship("Team", secondary=team_association_table, back_populates='coaches') def info(self, fields=None): i = self._info_complex(fields=fields) i.update({"type": "coach"}) return i