def install(location, name=None, title=None): if name is None: name = os.path.basename(location) if title is None: title = name if Problem.from_name(name) is not None: raise RuntimeError( f"problem {name} already present in the database!") if not os.path.exists(location): raise RuntimeError(f"{location} is not a valid path") query = "INSERT INTO problem(name, title, location) VALUES (%s, %s, %s) RETURNING *" problem = Problem(*database.query_one(query, name, title, location)) problem._git_clone() scoring_metadata = problem.metadata.get("scoring", {}) goals = scoring_metadata.get("goals", []) if len(goals) == 0: logging.warning( f"The problem {name} doesn't define any goal! " "Make sure that at least one goal is defined in turingarena.toml for scoring purposes!" ) for goal in goals: Goal.insert(problem, goal) return problem
def new_session(user): session["cookie"] = secrets.token_hex(32) query = "INSERT INTO session(cookie, user_id) VALUES (%s, %s) RETURNING *" return database.query_one(query, session["cookie"], user.id, convert=Session)
def new(user, problem, contest, filename): query = "INSERT INTO submission(problem_id, contest_id, user_id, filename) VALUES (%s, %s, %s, %s) RETURNING *" return database.query_one(query, problem.id, contest.id, user.id, filename, convert=Submission)
def insert(submission, event_type, payload): query = "INSERT INTO evaluation_event(submission_id, type, data) VALUES (%s, %s, %s) RETURNING *" if event_type != 'text': payload = json.dumps(payload) return database.query_one(query, submission.id, event_type.upper(), payload, convert=EvaluationEvent)
def max_goals_of_user_in_contest(self, user, contest): query = """ SELECT MAX(n_goals) FROM ( SELECT COUNT(*) AS n_goals, s.id FROM acquired_goal ac JOIN submission s ON ac.submission_id = s.id WHERE ac.result = TRUE GROUP BY s.id HAVING s.contest_id = %s AND s.user_id = %s and s.problem_id = %s ) AS goals_of_submission """ return database.query_one(query, contest.id, user.id, self.id, convert=lambda x: 0 if x is None else int(x))
def from_id(contest_id): query = "SELECT * FROM contest WHERE id = %s" return database.query_one(query, contest_id, convert=Contest)
def from_name(contest_name): query = "SELECT * FROM contest WHERE name = %s" return database.query_one(query, contest_name, convert=Contest)
def from_id(submission_id): query = "SELECT * FROM submission WHERE id = %s" return database.query_one(query, submission_id, convert=Submission)
def n_goals(self): query = "SELECT COUNT(*) FROM goal WHERE problem_id = %s" return database.query_one(query, self.id, convert=int)
def from_problem_and_name(problem, name): query = "SELECT * FROM goal WHERE problem_id = %s AND name = %s" return database.query_one(query, problem.id, name, convert=Goal)
def insert(problem, name): query = "INSERT INTO goal(problem_id, name) VALUES (%s, %s) RETURNING *" return database.query_one(query, problem.id, name, convert=Goal)
def from_submission(submission): query = "SELECT ag.goal_id, ag.result FROM acquired_goal ag WHERE ag.submission_id = %s" for id, result in database.query_all(query, submission.id): yield database.query_one("SELECT * FROM goal WHERE id = %s", id, convert=Goal), bool(result)
def from_id(problem_id): query = "SELECT * FROM problem WHERE id = %s" return database.query_one(query, problem_id, convert=Problem)
def from_name(name): query = "SELECT * FROM problem WHERE name = %s" return database.query_one(query, name, convert=Problem)
def current_session(): cookie = session.get("cookie") if cookie is None: return None query = "SELECT * FROM session WHERE cookie = %s" return database.query_one(query, cookie, convert=Session)