diff options
author | Timotej Lazar <timotej.lazar@fri.uni-lj.si> | 2015-10-15 15:47:47 +0200 |
---|---|---|
committer | Timotej Lazar <timotej.lazar@fri.uni-lj.si> | 2015-10-15 15:47:47 +0200 |
commit | ca8dcfdf819dae2aed6c864a3a11f1e30c632e96 (patch) | |
tree | c5dbc06099c417eac2422d9ebd7bfc9ada74e8b8 /db | |
parent | cbc096f9cb44a7d26b4fa01a40dbba594ab339ca (diff) |
Store identifiers instead of IDs in Problem model
Diffstat (limited to 'db')
-rw-r--r-- | db/models.py | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/db/models.py b/db/models.py index e4b5317..f5c690d 100644 --- a/db/models.py +++ b/db/models.py @@ -26,24 +26,27 @@ class CodeqUser(collections.namedtuple('CodeqUser', ['id', 'username', 'password 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', 'identifier'])): - __sql_prefix = 'select id, language_id, problem_group_id, identifier from problem' +class Problem(collections.namedtuple('Problem', ['id', 'language', 'group', 'identifier'])): + __sql_prefix = '''\ + select p.id, 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''' + __sql_order = 'p.language_id, p.problem_group_id, p.id' @staticmethod def get(**kwargs): + kwargs = {'p.'+k: v for k, v in kwargs.items()} return _general_get(kwargs, Problem, Problem.__sql_prefix) @staticmethod def list(): - return _general_list(Problem, Problem.__sql_prefix, order='language_id, problem_group_id, id') + return _general_list(Problem, Problem.__sql_prefix, order=Problem.__sql_order) @staticmethod def filter(**kwargs): - return _general_filter(kwargs, Problem, Problem.__sql_prefix) - - @staticmethod - def get_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) + kwargs = {'p.'+k: v for k, v in kwargs.items()} + return _general_filter(kwargs, Problem, Problem.__sql_prefix, order=Problem.__sql_order) # known as Attempt in the original code class Solution(collections.namedtuple('Solution', ['id', 'done', 'content', 'problem_id', 'codeq_user_id', 'trace'])): |