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 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 User(db.Model): __tablename__ = 'phaunos_user' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(255), nullable=False) is_admin = db.Column(db.Boolean, default=False, nullable=False) confirmed_on = db.Column(db.DateTime, nullable=True) def __init__(self, username, email, password, is_admin=False): self.username = username self.email = email self.password = generate_password_hash(password, method='sha256') self.is_admin = is_admin def save(self): db.session.add(self) db.session.commit() def to_dict(self): return dict(username=self.username, email=self.email) def __repr__(self): return self.username
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)
@enum.unique class VisualizationType(enum.Enum): WAVEFORM = enum.auto() SPECTROGRAM = enum.auto() @enum.unique class Role(enum.Enum): PROJECTADMIN = enum.auto() PROJECTMEMBER = enum.auto() audio_project_rel = db.Table( 'audio_project_rel', db.Column('project_id', db.Integer, db.ForeignKey('project.id'), primary_key=True), db.Column('audio_id', db.Integer, db.ForeignKey('audio.id'), primary_key=True)) tagset_project_rel = db.Table( 'tagset_project_rel', db.Column('project_id', db.Integer, db.ForeignKey('project.id'), primary_key=True), db.Column('tagset_id', db.Integer, db.ForeignKey('tagset.id'),