class Course(db.Model, RoleMixin): """Represents courses.""" __tablename__ = 'course' id = db.Column(db.Integer(), primary_key=True) code = db.Column(db.String(20), unique=True) name = db.Column(db.String(80)) description = db.Column(db.String(255))
class User(db.Model, UserMixin): """Represents users.""" id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(255), unique=True) password = db.Column(db.String(255)) active = db.Column(db.Boolean()) confirmed_at = db.Column(db.DateTime()) roles = db.relationship('Course', secondary=courses_users, backref=db.backref('users', lazy='dynamic'))
class Action(JsonSerializable, db.Model): id = db.Column(db.Integer, primary_key=True) # Can be segmentation, classification or detection: type = db.Column(db.String(256), index=True) image_id = db.Column(db.String(256), db.ForeignKey('image.id')) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) last_modification = db.Column( db.DateTime, index=True, default=datetime.utcnow ) time_spent = db.Column(db.Interval, default=timedelta()) score = db.Column(db.Integer, default=0) score_pending = db.Column(db.Boolean, default=True) active = db.Column(db.Boolean, default=True) def __repr__(self): return f'<Action user={self.user_id}, image={self.image_id}, score={self.score}>'
class User(JsonSerializable, db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), index=True, unique=True) email = db.Column(db.String(128), index=True, nullable=True) password_hash = db.Column(db.String(128), default="") created = db.Column( db.DateTime, index=True, default=datetime.utcnow ) # Each user should start with a different random image image_seed = db.Column(db.Integer, default=randint(0, 10_000_000)) admin = db.Column(db.Boolean, index=False, unique=False, nullable=False, default=False) tested = db.Column(db.Boolean, index=False, unique=False, nullable=False, default=False) actions = db.relationship('Action', backref='user', lazy='dynamic') def __repr__(self): return '<User {}>'.format(self.name) def set_password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) def to_json(self, full=False): data = super().to_json() del data['password_hash'] masks = Action.query.filter_by(user=self, type="segmentation").all() data['segmentation'] = { 'score': 0, 'score_pending': 0, 'n_masks': len(masks) } for mask in masks: if mask.pending: data['segmentation']['score_pending'] += mask.score else: data['segmentation']['score'] += mask.score return data
class Action(JsonSerializable, db.Model): id = db.Column(db.Integer, primary_key=True) # Can be segmentation, classification or detection: type = db.Column(db.String(64), index=True) image_id = db.Column(db.String(256), index=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) last_modification = db.Column( db.DateTime, index=True, default=datetime.utcnow ) time_spent = db.Column(db.Interval, default=timedelta()) score = db.Column(db.Integer, default=0) pending = db.Column(db.Boolean, default=True) active = db.Column(db.Boolean, default=False) notes = db.Column(db.String(256), nullable=True) # Difficulty goes from 1 to 5, 3 is average difficulty = db.Column(db.Integer, index=True, default=3) def __repr__(self): return f'<Action user={self.user_id}, image_id={self.image_id}, score={self.score}>'
class Image(JsonSerializable, db.Model): id = db.Column(db.String(256), primary_key=True) actions = db.relationship( 'Action', backref='image', lazy='dynamic' ) def to_json(self): data = super().to_json() data['n_segmentations'] = \ Action.query.filter_by(image=self, type='segmentation').count() data['n_classifications'] = \ Action.query.filter_by(image=self, type='classification').count() data['n_detections'] = \ Action.query.filter_by(image=self, type='detection').count() return data
class Text(db.Model): id = db.Column(db.Integer, primary_key=True) content = db.Column(db.String(200)) def __repr__(self): return '<Text {:r}>'.format(self.content)