예제 #1
0
    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
예제 #2
0
 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)
예제 #3
0
 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)
예제 #4
0
 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)
예제 #5
0
 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))
예제 #6
0
 def from_id(contest_id):
     query = "SELECT * FROM contest WHERE id = %s"
     return database.query_one(query, contest_id, convert=Contest)
예제 #7
0
 def from_name(contest_name):
     query = "SELECT * FROM contest WHERE name = %s"
     return database.query_one(query, contest_name, convert=Contest)
예제 #8
0
 def from_id(submission_id):
     query = "SELECT * FROM submission WHERE id = %s"
     return database.query_one(query, submission_id, convert=Submission)
예제 #9
0
 def n_goals(self):
     query = "SELECT COUNT(*) FROM goal WHERE problem_id = %s"
     return database.query_one(query, self.id, convert=int)
예제 #10
0
 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)
예제 #11
0
 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)
예제 #12
0
 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)
예제 #13
0
 def from_id(problem_id):
     query = "SELECT * FROM problem WHERE id = %s"
     return database.query_one(query, problem_id, convert=Problem)
예제 #14
0
 def from_name(name):
     query = "SELECT * FROM problem WHERE name = %s"
     return database.query_one(query, name, convert=Problem)
예제 #15
0
 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)