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 PostCategory(db.Model): __tablename__ = 'PostCategory' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(32), nullable=False) @property def serialize(self): return {'id': self.id, 'name': self.name}
class CallOutcome(db.Model): __tablename__ = 'CallOutcome' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(32), nullable=False) icon_name = db.Column(db.String(64), nullable=False) @property def serialize(self): return {'id': self.id, 'name': self.name, 'icon_name': self.icon_name}
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 Plate(db.Model): __tablename__ = 'plate_dict' plate_id = db.Column(db.Integer, primary_key=True, autoincrement=True) plate = db.Column(db.Integer) well = db.Column(db.String()) sample_id = db.Column(db.String()) sample_type = db.Column(db.String()) experiment = db.Column(db.String()) sample_dilution = db.Column(db.String()) cell_donor = db.Column(db.String()) expt_id = db.Column(db.String()) # expt_type = db.Column(db.String()) def __init__(self, plate, well, sample_id, sample_type, experiment, sample_dilution, cell_donor, expt_id, expt_type): self.plate = plate self.well = well self.sample_id = sample_id self.sample_type = sample_type self.experiment = experiment self.sample_dilution = sample_dilution self.cell_donor = cell_donor self.expt_id = expt_id # self.expt_type = expt_type def __repr__(self): return '<plate id {}>'.format(self.plate_id) def serialize(self): return { 'plate_id': self.plate_id, 'well': self.well, 'expt_id': self.expt_id }
class StatGroup(db.Model): __tablename__ = 'StatGroup' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, unique=True, nullable=False) @property def serialize(self): return { 'id': self.id, 'name': self.name }
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 FileRef(db.Model): __tablename__ = 'file_upload' file_id = db.Column(db.Integer, primary_key=True, autoincrement=True) file = db.Column(db.LargeBinary) file_type = db.Column(db.String()) expt_id = db.Column(db.String()) expt_type = db.Column(db.String()) def __init__(self, file, file_type, expt_id, expt_type): self.file = file self.file_type = file_type self.expt_id = expt_id self.expt_type = expt_type def __repr__(self): return '<file id {}>'.format(self.file_id)
class User(db.Model): __tablename__ = 'User' id = db.Column(db.Integer, primary_key=True) # employee_id = db.Column(db.Integer, unique=True, nullable=False) username = db.Column(db.String(128), unique=True, nullable=False) first_name = db.Column(db.Unicode(128), nullable=False) last_name = db.Column(db.Unicode(128), nullable=False) location = db.Column(db.String(256), default='Unknown') bio = db.Column(db.Unicode) image = db.Column(db.String) # Flask Login fields is_authenticated = db.Column(db.Boolean, default=False) is_active = db.Column(db.Boolean, default=True) is_anonymous = db.Column(db.Boolean, default=False) def get_id(self): return self.username @property def serialize(self): return { 'id': self.id, # 'employee_id': self.employee_id, 'username': self.username, 'first_name': self.first_name, 'last_name': self.last_name, 'location': self.location, 'bio': self.bio, 'image': self.image, 'is_authenticated': self.is_authenticated, 'is_active': self.is_active, 'is_anonymous': self.is_anonymous } @property def get_editable(self): return ['location', 'bio', 'image']
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