class FluorTable(db.Model): __tablename__ = 'fluor' # id = db.Column(db.Integer, primary_key=True, autoincrement=True) filename = db.Column(db.String()) pct_fluor = db.Column(db.Float()) MFI = db.Column(db.Float()) plate = db.Column(db.Integer) well = db.Column(db.String(), primary_key=True) row = db.Column(db.String()) col = db.Column(db.String()) pct_fluor_source = db.Column(db.String()) MFI_source = db.Column(db.String()) db.relationship('Plate', backref='fluortable') def __init(self, filename, pct_fluor, MFI, plate, well, row, col, pct_fluor_source, MFI_source): self.filename = filename self.pct_fluor = pct_fluor self.MFI = MFI self.plate = plate self.well = well self.row = row self.col = col self.pct_fluor_source = pct_fluor_source self.MFI_source = MFI_source def __repr__(self): return '<obs id {}>'.format(self.MFI) def serialize(self): return { 'MFI': self.MFI, 'well': self.well }
class Like(db.Model): __tablename__ = 'Like' id = db.Column(db.Integer, primary_key=True) date = db.Column(db.DateTime, default=datetime.datetime.utcnow(), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('User.id'), nullable=False) post_id = db.Column(db.Integer, db.ForeignKey('Post.id'), nullable=False) user = db.relationship('User') @property def serialize(self): return { 'id': self.id, 'date': self.date, 'user': self.user.serialize, 'post_id': self.post_id } def create_like(data): like = Like() like.post_id = data['post_id'] like.user_id = data['user']['id'] return like
class SummaryStat(db.Model): __tablename__ = 'SummaryStat' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False) entries = db.relationship('SummaryStatEntry', lazy=False) @property def serialize(self): return { 'id': self.id, 'name': self.name, 'entries': [entry.serialize for entry in self.entries] if self.entries is not None else {} }
class Stat(db.Model): __tablename__ = 'Stat' id = db.Column(db.Integer, primary_key=True) eval_date = db.Column(db.DateTime, nullable=False) percent = db.Column(db.Float, nullable=False) max_val = db.Column(db.Integer, nullable=False) stat_group_id = db.Column(db.Integer, db.ForeignKey('StatGroup.id')) user_id = db.Column(db.Integer, db.ForeignKey('User.id')) stat_group = db.relationship('StatGroup') user = db.relationship('User') @property def serialize(self): return { 'id': self.id, 'eval_date': self.eval_date, 'percent': self.percent, 'max_val': self.max_val, 'stat_group': self.stat_group.serialize if self.stat_group is not None else {}, 'user': self.user.serialize if self.user is not None else {} }
class Post(db.Model): __tablename__ = 'Post' id = db.Column(db.Integer, primary_key=True) date = db.Column(db.DateTime, default=datetime.datetime.utcnow, nullable=False) content = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('User.id'), nullable=False) category_id = db.Column(db.Integer, db.ForeignKey('PostCategory.id'), nullable=False) outcome_id = db.Column(db.Integer, db.ForeignKey('CallOutcome.id'), nullable=False) parent_id = db.Column(db.Integer, db.ForeignKey('Post.id')) user = db.relationship('User') category = db.relationship('PostCategory') outcome = db.relationship('CallOutcome') children = db.relationship('Post', backref=db.backref('parent', remote_side=[id])) likes = db.relationship('Like') @property def serialize(self): return { 'id': self.id, 'date': self.date, 'likes': [like.serialize for like in self.likes], 'user': self.user.serialize, 'content': self.content, 'category': self.category.serialize, 'outcome': self.outcome.serialize, 'children': self.serialize_children(), 'parent': self.serialize_parent() } def serialize_children(self): children_list = [] for child in self.children: children_list.append({ 'id': child.id, 'date': child.date, 'likes': [like.serialize for like in child.likes], 'user': child.user.serialize, 'content': child.content, 'category': child.category.serialize, 'outcome': child.outcome.serialize, 'children': child.serialize_children(), 'parent': child.serialize_parent() }) return children_list def serialize_parent(self): if not self.parent_id: return {} return { 'id': self.parent_id, 'date': self.parent.date, 'likes': [like.serialize for like in self.parent.likes], 'user': self.parent.user.serialize, 'content': self.parent.content, 'category': self.parent.category.serialize, 'outcome': self.parent.outcome.serialize, 'parent': self.parent.serialize_parent() } def create_post(data): post = Post() post.content = data['content'] post.category_id = data['category']['id'] post.outcome_id = data['outcome']['id'] if 'parent' in data and data['parent']: post.parent_id = data['parent']['id'] return post