class Project(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, unique=True, nullable=False) visualization_type = db.Column(ENUM(VisualizationType), default=VisualizationType.SPECTROGRAM, nullable=False) allow_regions = db.Column(db.Boolean, nullable=False) audiolist_filename = db.Column(db.String, nullable=False) taglist_filename = db.Column(db.String, nullable=False) min_annotations_per_file = db.Column(db.Integer, default=1, nullable=False) members_only = db.Column(db.Boolean, default=False, nullable=False) # created_by_id = db.Column(db.Integer, db.ForeignKey('phaunos_user.id')) # created_by = db.relationship(User, backref=db.backref('projects', cascade='all')) tagsets = db.relationship('Tagset', secondary=tagset_project_rel, lazy=True, backref=db.backref('projects', lazy=True)) audios = db.relationship('Audio', secondary=audio_project_rel, lazy=True, backref=db.backref('projects', lazy=True)) annotations = db.relationship('Annotation', lazy=True, cascade='all', backref='project') @property def n_annotations(self): return len(self.annotations) @property def percentage_of_completion(self): n_annotations = 0 for audio_id in [audio.id for audio in self.audios]: n_annotations += min( len( db.session.query(Annotation.created_by_id).filter( Annotation.project_id == self.id).filter( Annotation.audio_id == audio_id).distinct().all()), self.min_annotations_per_file) return n_annotations // (self.min_annotations_per_file * len(self.audios)) * 100 # @property # def is_completed(self): # if (self.n_annotations_per_file and # self.annotations.count() >= self.audios.count() * self.n_annotations_per_file): # return True # return False def __repr__(self): return '<name {}>'.format(self.name)
class Tag(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False) created_by_id = db.Column(db.Integer, db.ForeignKey('phaunos_user.id')) created_by = db.relationship('User', backref=db.backref('tags', cascade='all')) annotations = db.relationship('Annotation', backref='tag', lazy=True, cascade='all') def __repr__(self): return '<name {}>'.format(self.name)
class Audio(db.Model): id = db.Column(db.Integer, primary_key=True) path = db.Column(db.String, unique=True, nullable=False) created_by_id = db.Column(db.Integer, db.ForeignKey('phaunos_user.id')) created_by = db.relationship(User, backref=db.backref('audios', cascade='all')) annotations = db.relationship('Annotation', backref='audio', cascade='all', lazy=True) def __repr__(self): return '<id {}>'.format(self.path)
class Tagset(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, unique=True, nullable=False) created_by_id = db.Column(db.Integer, db.ForeignKey('phaunos_user.id')) created_by = db.relationship(User, backref=db.backref('tagsets', cascade='all')) tags = db.relationship('Tag', secondary=tag_tagset_rel, lazy=True, backref='tagsets') def __repr__(self): return '<name {}>'.format(self.name)
class UserProjectRel(db.Model): __tablename__ = 'user_project_rel' user_id = db.Column(db.Integer, db.ForeignKey('phaunos_user.id'), primary_key=True) project_id = db.Column(db.Integer, db.ForeignKey('project.id'), primary_key=True) user_role = db.Column(ENUM(Role), nullable=False) user = db.relationship('User', backref=db.backref('user_project_rel', cascade='all')) project = db.relationship('Project', backref=db.backref('user_project_rel', cascade='all'))
class Annotation(db.Model): id = db.Column(db.Integer, primary_key=True) start_time = db.Column(db.Float, nullable=True) end_time = db.Column(db.Float, nullable=True) tag_id = db.Column(db.Integer, db.ForeignKey(Tag.id), nullable=False) project_id = db.Column(db.Integer, db.ForeignKey('project.id'), nullable=False) audio_id = db.Column(db.Integer, db.ForeignKey('audio.id'), nullable=False) created_by_id = db.Column(db.Integer, db.ForeignKey('phaunos_user.id')) created_by = db.relationship(User, backref=db.backref('annotations', cascade='all')) def __repr__(self): return '<id {}>'.format(self.id)