diff options
author | Timotej Lazar <timotej.lazar@araneo.org> | 2015-08-27 18:06:19 +0200 |
---|---|---|
committer | Timotej Lazar <timotej.lazar@araneo.org> | 2015-08-27 18:06:19 +0200 |
commit | b00e270965ee4bbfb570a00c921396424192a69e (patch) | |
tree | 9402c502810ec8a6374532811716997dba9c6038 | |
parent | 86287e6a4fa07d03aa906965a182425320e445ce (diff) |
Add some helper methods to db.models
-rw-r--r-- | db/models.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/db/models.py b/db/models.py index 9ebc155..e5aebb5 100644 --- a/db/models.py +++ b/db/models.py @@ -23,6 +23,10 @@ class CodeqUser(collections.namedtuple('CodeqUser', ['id', 'username', 'password def filter(**kwargs): return _general_filter(kwargs, CodeqUser, CodeqUser.__sql_prefix) + @staticmethod + def solved_problems(user_id, language): + return _run_sql('select g.identifier, p.identifier from solution s inner join problem p on p.id = s.problem_id inner join problem_group g on g.id = p.problem_group_id inner join language l on l.id = p.language_id where s.codeq_user_id = %s and l.identifier = %s and s.done = True', (user_id, language), fetch_one=False) + class Problem(collections.namedtuple('Problem', ['id', 'language_id', 'problem_group_id', 'name', 'identifier', 'is_visible'])): __sql_prefix = 'select id, language_id, problem_group_id, name, identifier, is_visible from problem' @@ -38,6 +42,10 @@ class Problem(collections.namedtuple('Problem', ['id', 'language_id', 'problem_g def filter(**kwargs): return _general_filter(kwargs, Problem, Problem.__sql_prefix) + @staticmethod + def identifier(problem_id): + return _run_sql('select l.identifier, g.identifier, p.identifier from problem p inner join problem_group g on g.id = p.problem_group_id inner join language l on l.id = p.language_id where p.id = %s', (problem_id,), fetch_one=True) + # known as Attempt in the original code class Solution(collections.namedtuple('Solution', ['id', 'done', 'content', 'problem_id', 'codeq_user_id', 'trace'])): __sql_prefix = 'select id, done, content, problem_id, codeq_user_id, trace::text from solution' @@ -124,3 +132,18 @@ def _general_list(clazz, sql_select, row_conversion_fn=_no_row_conversion): cur.close() finally: return_connection(conn) + +def _run_sql(sql, params, fetch_one=False): + conn = get_connection() + try: + cur = conn.cursor() + try: + cur.execute(sql, params) + if fetch_one: + return cur.fetchone() + else: + return cur.fetchall() + finally: + cur.close() + finally: + return_connection(conn) |