From ca8dcfdf819dae2aed6c864a3a11f1e30c632e96 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Thu, 15 Oct 2015 15:47:47 +0200 Subject: Store identifiers instead of IDs in Problem model --- db/models.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'db') 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'])): -- cgit v1.2.1