class QuizModel(database.Model): __tablename__ = 'quizzes' quiz_id = database.Column(database.Integer, primary_key=True, nullable=False) test_id = database.Column(database.Integer, database.ForeignKey('tests.test_id'), nullable=False) order_num = database.Column(database.Integer, nullable=False) credit_value = database.Column(database.Integer, nullable=False) gained_credit = database.Column(database.Integer) quiz_type = database.Column(database.String) text_body = database.Column(database.String) path_to_attachment = database.Column(database.String) title = database.Column(database.String(100)) instructions = database.Column(database.String) test = database.relationship('TestModel') answers = database.relationship('AnswerModel', lazy='dynamic') def __init__(self, quiz_id, test_id, order_num, credit_value, gained_credit, quiz_type, text_body, path_to_attachment, title, instructions): self.quiz_id = quiz_id self.test_id = test_id self.order_num = order_num self.credit_value = credit_value self.gained_credit = gained_credit self.quiz_type = quiz_type # 'type' is a key word in Python self.text_body = text_body self.path_to_attachment = path_to_attachment self.title = title self.instructions = instructions def json(self, getCorrectAnswers=False): return { 'quiz_id': self.quiz_id, 'test_id': self.test_id, 'order_num': self.order_num, 'credit_value': self.credit_value, 'gained_credit': self.gained_credit, 'quiz_type': self.quiz_type, 'text_body': self.text_body, 'path_to_attachment': self.path_to_attachment, 'title': self.title, 'instructions': self.instructions, 'answers': [a.json(getCorrectAnswers=getCorrectAnswers) for a in self.answers.all()] } def save_to_database(self): database.session.add(self) database.session.commit() def delete_from_database(self): database.session.delete(self) database.session.commit() def get_correct_answer(self): return self.answers.filter_by(quiz_id=self.quiz_id, is_correct=True).first() @classmethod def find_by_id(cls, quiz_id): return cls.query.filter_by(quiz_id=quiz_id).first()
class TopicModel(database.Model): __tablename__ = 'topics' topic_id = database.Column(database.Integer, primary_key=True, nullable=False) is_unlocked = database.Column(database.Boolean, nullable=False) name = database.Column(database.String(50), nullable=False) needed_credit = database.Column(database.Integer) tests = database.relationship('TestModel', lazy='dynamic') formativeAssessments = database.relationship('FormativeAssessmentModel', lazy='dynamic') def __init__(self, topic_id, is_unlocked, name, needed_credit): self.topic_id = topic_id self.is_unlocked = is_unlocked self.name = name self.needed_credit = needed_credit def json(self): return { 'topic_id': self.topic_id, 'is_unlocked': self.is_unlocked, 'name': self.name, 'needed_credit': self.needed_credit, 'tests': [t.json() for t in self.tests.all()], 'formative_assessments': [fa.json() for fa in self.formativeAssessments.all()] } def save_to_database(self): database.session.add(self) database.session.commit() def delete_from_database(self): database.session.delete(self) database.session.commit() @classmethod def find_by_id(cls, topic_id): return cls.query.filter_by(topic_id=topic_id).first() @classmethod def get_all(cls): return cls.query.all()
class Dataset(db.Model): __tablename__ = "datasets" id = db.Column(db.Integer, primary_key=True) bucket_name = db.Column(db.String(255)) folder_name = db.Column(db.String(255)) object_name = db.Column(db.String(255)) file_name = db.Column(db.String(255)) project_id = db.Column(db.Integer, db.ForeignKey("projects.id"), nullable=False) project = db.relationship("Project") def __init__(self, bucket_name, folder_name, object_name, file_name, project_id): self.bucket_name = bucket_name self.folder_name = folder_name self.object_name = object_name self.file_name = file_name self.project_id = project_id def save(self): db.session.add(self) db.session.commit() def delete(self): db.session.delete(self) db.session.commit() def get_data_path(self): return f"s3://{self.bucket_name}/{self.folder_name}/train" @classmethod def find_data_by_id(cls, data_id): return cls.query.filter_by(id=data_id).first()
class PuzzleModel(database.Model): __tablename__ = 'scores' id = database.Column(database.Integer, primary_key=True) score = database.Column(database.Integer) user_id = database.Column(database.Integer, database.ForeignKey('users.id')) user = database.relationship('UserModel') def __init__(self, score, user_id): self.score = score self.user_id = user_id def to_json(self): return {'id': self.user_id, 'score': self.score} @classmethod def find_by_id(cls, _id): return cls.query.filter_by(id=_id).first() @classmethod def find_all_by_id(cls, _user_id): return cls.query.filter_by(user_id=_user_id) @classmethod def find_all(cls): return cls.query.all() def save_to_db(self): database.session.add(self) database.session.commit()
class AnswerModel(database.Model): __tablename__ = 'answers' answer_id = database.Column(database.Integer, primary_key=True, nullable=False) quiz_id = database.Column(database.Integer, database.ForeignKey('quizzes.quiz_id'), nullable=False) body = database.Column(database.String) is_correct = database.Column(database.Boolean) path_to_attachment = database.Column(database.String) is_selected = database.Column(database.Boolean) quiz = database.relationship('QuizModel') def __init__(self, answer_id, quiz_id, body, is_correct, path_to_attachment, is_selected): self.answer_id = answer_id self.quiz_id = quiz_id self.body = body self.is_correct = is_correct self.path_to_attachment = path_to_attachment self.is_selected = is_selected def json(self): return { 'answer_id': self.answer_id, 'quiz_id': self.quiz_id, 'body': self.body, 'is_correct': self.is_correct, 'path_to_attachment': self.path_to_attachment, 'is_selected': self.is_selected, } def save_to_database(self): database.session.add(self) database.session.commit() def delete_from_database(self): database.session.delete(self) database.session.commit() @classmethod def find_by_id(cls, answer_id): return cls.query.filter_by(answer_id=answer_id).first() @classmethod def get_all(cls): return cls.query.all() @classmethod def get_all_for_quiz(cls, quiz_id): return cls.query.filter_by(quiz_id=quiz_id).all()
class AnswerModel(database.Model): __tablename__ = 'answers' answer_id = database.Column(database.Integer, primary_key=True, nullable=False) quiz_id = database.Column(database.Integer, database.ForeignKey('quizzes.quiz_id'), nullable=False) body = database.Column(database.String) is_correct = database.Column(database.Boolean) path_to_attachment = database.Column(database.String) is_selected = database.Column(database.Boolean) quiz = database.relationship('QuizModel') def __init__(self, answer_id, quiz_id, body, is_correct, path_to_attachment, is_selected): self.answer_id = answer_id self.quiz_id = quiz_id self.body = body self.is_correct = is_correct self.path_to_attachment = path_to_attachment self.is_selected = is_selected def json(self, getCorrectAnswers=False): answerJson = { 'answer_id': self.answer_id, 'quiz_id': self.quiz_id, 'body': self.body, 'path_to_attachment': self.path_to_attachment, 'is_selected': self.is_selected, } if getCorrectAnswers: # the reason 'is_correct' is isolated is because during a test the data is stored in the browser's session storage, so during this time we don't want the correct answers to be visible to the user if they look in their browser's storage answerJson['is_correct'] = self.is_correct return answerJson def save_to_database(self): database.session.add(self) database.session.commit() def delete_from_database(self): database.session.delete(self) database.session.commit() @classmethod def find_by_id(cls, answer_id): return cls.query.filter_by(answer_id=answer_id).first()
class UserModel(database.Model): __tablename__ = 'users' id = database.Column(database.Integer, primary_key=True) username = database.Column(database.String(80)) password = database.Column(database.String(80)) puzzles = database.relationship('PuzzleModel', lazy='dynamic') def __init__(self, username, password): self.username = username self.password = password def to_json(self): return {'id': self.id, 'username': self.username} def to_json_score(self): lst = [p.to_json().get('score') for p in self.puzzles.all()] return { 'id': self.id, 'username': self.username, 'scores': lst, 'total_score': sum(lst) # reduce(add, lst, 0) # fold_left } @classmethod def find_by_username(cls, username): return cls.query.filter_by(username=username).first() @classmethod def find_by_id(cls, user_id): return cls.query.filter_by(id=user_id).first() def save_to_db(self): database.session.add(self) database.session.commit() def update_to_db(self, pwd): self.password = pwd database.session.commit() def delete_from_db(self): database.session.delete(self) database.session.commit()
class TestModel(database.Model): __tablename__ = 'tests' test_id = database.Column(database.Integer, primary_key=True, nullable=False) topic_id = database.Column(database.Integer, database.ForeignKey('topics.topic_id'), nullable=False) is_unlocked = database.Column(database.Boolean) max_credit = database.Column(database.Integer, nullable=False) order_num = database.Column(database.Integer, nullable=False) gained_credit = database.Column(database.Integer) pass_credit = database.Column(database.Integer, nullable=False) time_limit = database.Column(database.Time) description = database.Column(database.String) is_retakeable = database.Column(database.Boolean) is_official = database.Column(database.Boolean, nullable=False) topic = database.relationship('TopicModel') quizzes = database.relationship('QuizModel', lazy='dynamic') def __init__(self, test_id, topic_id, is_unlocked, max_credit, order_num, gained_credit, pass_credit, time_limit, description, is_retakeable, is_official): self.test_id = test_id self.topic_id = topic_id self.is_unlocked = is_unlocked self.max_credit = max_credit self.order_num = order_num self.gained_credit = gained_credit self.pass_credit = pass_credit self.time_limit = time_limit self.description = description self.is_retakeable = is_retakeable self.is_official = is_official def json(self, getCorrectAnswers=False): return { 'test_id': self.test_id, 'topic_id': self.topic_id, 'is_unlocked': self.is_unlocked, 'max_credit': self.max_credit, 'order_num': self.order_num, 'gained_credit': self.gained_credit, 'pass_credit': self.pass_credit, 'time_limit': self.time_limit, 'description': self.description, 'is_retakeable': self.is_retakeable, 'is_official': self.is_official, 'quizzes': [q.json(getCorrectAnswers=getCorrectAnswers) for q in self.quizzes.all()] } def save_to_database(self): database.session.add(self) database.session.commit() def delete_from_database(self): database.session.delete(self) database.session.commit() def get_correct_answers(self): correctAnswers = [] for q in self.quizzes.all(): answer = q.get_correct_answer() if answer: answer = answer.answer_id correctAnswers.append(answer) return correctAnswers @classmethod def find_by_id(cls, test_id): return cls.query.filter_by(test_id=test_id).first()
class FormativeAssessmentModel(database.Model): __tablename__ = 'formativeAssessments' fa_id = database.Column(database.Integer, primary_key=True, nullable=False) topic_id = database.Column(database.Integer, database.ForeignKey('topics.topic_id')) is_unlocked = database.Column(database.Boolean, nullable=False) max_credit = database.Column(database.Integer) order_num = database.Column(database.Integer, nullable=False) gained_credit = database.Column(database.Integer) answer = database.Column(database.String) pass_credit = database.Column(database.Integer) instructions = database.Column(database.String) title = database.Column(database.String(100), nullable=False) path_to_attachment = database.Column(database.String) deadline = database.Column(database.String) reviewer_comment = database.Column(database.String) is_marked = database.Column(database.Boolean) topic = database.relationship('TopicModel') def __init__(self, fa_id, topic_id, is_unlocked, order_num, max_credit, gained_credit, answer, pass_credit, instructions, title, path_to_attachment, deadline, reviewer_comment, is_marked): self.fa_id = fa_id self.topic_id = topic_id self.is_unlocked = is_unlocked self.max_credit = max_credit self.order_num = order_num self.gained_credit = gained_credit self.answer = answer self.pass_credit = pass_credit self.instructions = instructions self.title = title self.path_to_attachment = path_to_attachment self.deadline = deadline self.reviewer_comment = reviewer_comment self.is_marked = is_marked def json(self): return { 'fa_id': self.fa_id, 'topic_id': self.topic_id, 'is_unlocked': self.is_unlocked, 'max_credit': self.max_credit, 'order_num': self.order_num, 'gained_credit': self.gained_credit, 'answer': self.answer, 'pass_credit': self.pass_credit, 'instructions': self.instructions, 'title': self.title, 'path_to_attachment': self.path_to_attachment, 'deadline': self.deadline, 'reviewer_comment': self.reviewer_comment, 'is_marked': self.is_marked } def save_to_database(self): database.session.add(self) database.session.commit() def delete_from_database(self, query, args): database.session.delete(self) database.session.commit() @classmethod def find_by_id(cls, fa_id): return cls.query.filter_by(fa_id=fa_id).first() @classmethod def get_all(cls): return cls.query.all() @classmethod def get_all_for_topic(cls, topic_id): return cls.query.filter_by(topic_id=topic_id).all()
class ClassficationProblem(db.Model): _tablename_ = "classifications" id = db.Column(db.Integer, primary_key=True) job_name = db.Column(db.String(255)) algorithm_name = db.Column(db.String(255)) train_accuracy = db.Column(db.Float) test_accuracy = db.Column(db.Float) train_f1 = db.Column(db.Float) test_f1 = db.Column(db.Float) project_id = db.Column(db.Integer, db.ForeignKey("projects.id"), nullable=False) project = db.relationship("Project") def __init__(self, job_name, algorithm_name, project_id): self.job_name = job_name self.algorithm_name = algorithm_name self.project_id = project_id def save(self): db.session.add(self) db.session.commit() def delete(self): db.session.delete(self) db.session.commit() def json(self): return { "id": self.id, "job_name": self.job_name, "algorithm_name": self.algorithm_name, "train_accuracy": self.train_accuracy, "test_accuracy": self.test_accuracy, "train_f1": self.train_f1, "test_f1": self.test_f1, "project_id": self.project_id } @classmethod def map_solution_to_solution_id(cls, solutions): return [solution.id for solution in solutions] @classmethod def find_solution_with_id(cls, solution_id): return cls.query.filter_by(id=solution_id).first() @classmethod def find_solutions_of_project(cls, project_id): solutions = cls.query.filter_by(project_id=project_id).order_by( ClassficationProblem.id.desc()).all() return cls.map_solution_to_solution_id(solutions) @classmethod def find_best_solution_of_project(cls, project_id): best_solution = cls.query.filter_by(project_id=project_id).filter( cls.test_accuracy != None).order_by( cls.test_accuracy.desc()).first() return best_solution def if_belongs_to(self, project_id): return self.project_id == project_id def analytics_filled(self): return self.train_accuracy != None def update_analytics(self, analytics): self.train_accuracy = analytics["train:accuracy"] self.test_accuracy = analytics["test:accuracy"] self.train_f1 = analytics["train:f1"] self.test_f1 = analytics["test:f1"] self.save()