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 Questions(db.Model): """Represents asked questions.""" __tablename__ = 'questions' question_id = db.Column(db.Integer, primary_key=True) session_id = db.Column(db.Integer, db.ForeignKey('lecturesession.session_id')) question = db.Column(db.Text) timestamp = db.Column(db.DateTime) answered = db.Column(db.Boolean, default=False) flagged = db.Column(db.Boolean, default=False) response = db.Column(db.Text, nullable=True) group = db.Column(db.Integer) def __init__(self, session_id, question, group, response=None): """Create a new question, connected to session_id.""" self.session_id = session_id self.question = question self.timestamp = datetime.utcnow() self.group = group if response is not None: self.response = response def __repr__(self): """A textual representation of the class.""" return '<Question {} for session {}: {} >'.format( self.question_id, self.session_id, self.question)
class LectureSession(db.Model): """Represents a single lecture session.""" __tablename__ = 'lecturesession' session_id = db.Column(db.Integer, primary_key=True) course_id = db.Column(db.Integer, db.ForeignKey('course.id')) active = db.Column(db.Boolean, default=False) def __init__(self, course_id): """Create a new lecturesession, connected to course_id.""" self.course_id = course_id def __repr__(self): """A textual representation of the class.""" return '<LSession status: {}>'.format(self.active)
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 Response(db.Model): """Represents keyword-response pairs""" __tablename__ = 'response' keyword = db.Column(db.Text, primary_key=True) course_id = db.Column(db.Integer, db.ForeignKey('course.id'), primary_key=True) response = db.Column(db.Text) def __init__(self, keyword, course_id, response): """Creates a new Response-record connected to course_id.""" self.keyword = keyword self.course_id = course_id self.response = response def __repr__(self): """A textual representation of the class.""" return '<Response {}-{} for course {} >'.format( self.keyword, self.response, self.course_id)
class SessionFeedback(db.Model): """Represents the number of times an action has been received.""" __tablename__ = 'sessionfeedback' session_id = db.Column(db.Integer, db.ForeignKey('lecturesession.session_id'), primary_key=True) action_name = db.Column(db.String, primary_key=True) count = db.Column(db.Integer, default=0) def __init__(self, session_id, action_name): """Create a new feedback, connected to session_id.""" self.session_id = session_id self.action_name = action_name def __repr__(self): """A textual representation of the class.""" return '<SFeedback for {} - {}: {}'.format(self.session_id, self.action_name, self.count)
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 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 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)
def __init__(self, keyword, course_id, response): """Creates a new Response-record connected to course_id.""" self.keyword = keyword self.course_id = course_id self.response = response def __repr__(self): """A textual representation of the class.""" return '<Response {}-{} for course {} >'.format( self.keyword, self.response, self.course_id) courses_users = db.Table( 'courses_users', db.Column('user_id', db.Integer(), db.ForeignKey('user.id')), db.Column('course_id', db.Integer(), db.ForeignKey('course.id'))) 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."""