diff options
-rw-r--r-- | server/prolog_session.py | 15 | ||||
-rw-r--r-- | server/python_session.py | 14 | ||||
-rw-r--r-- | wsgi_server.py | 4 |
3 files changed, 20 insertions, 13 deletions
diff --git a/server/prolog_session.py b/server/prolog_session.py index f328670..8a7b635 100644 --- a/server/prolog_session.py +++ b/server/prolog_session.py @@ -3,6 +3,7 @@ import operator import threading import prolog.engine +import server.user_session from db.models import CodeqUser, Problem from . import problems @@ -88,8 +89,10 @@ class PrologSession(object): prolog.engine.destroy(self._engine_id) self._engine_id = None - def hint(self, user_id, problem_id, program): + def hint(self, sid, problem_id, program): + session = server.user_session.get_session_by_id(sid) language, problem_group, problem = Problem.get_identifier(problem_id) + problem_module = problems.load_problem(language, problem_group, problem, 'common') # If compilation fails just return compiler messages. engine_id, output = prolog.engine.create(code=program) @@ -100,26 +103,26 @@ class PrologSession(object): return [{'id': 'syntax_error', 'args': {'messages': errors_msg}}] # Otherwise try problem-specific hints. - problem_module = problems.load_problem(language, problem_group, problem, 'common') if hasattr(problem_module, 'hint'): - hints = problem_module.hint(program) + hints = problem_module.hint(session, program) if hints: return hints # Finally return a generic "try thinking a bit" message. return [{'id': 'no_hint'}] - def test(self, user_id, problem_id, program): + def test(self, sid, problem_id, program): + session = server.user_session.get_session_by_id(sid) language, problem_group, problem = Problem.get_identifier(problem_id) problem_module = problems.load_problem(language, problem_group, problem, 'common') - solved_problems = [p for p in CodeqUser.solved_problems(user_id, language) + solved_problems = [p for p in CodeqUser.solved_problems(session.get_uid(), language) if p != (problem_group, problem)] other_solutions = problems.solutions_for_problems(language, solved_problems) code = program + '\n' + other_solutions try: - n_correct, n_all = problem_module.test(code) + n_correct, n_all = problem_module.test(session, code) return [{'id': 'test_results', 'args': {'passed': n_correct, 'total': n_all}}] except AttributeError as ex: return [{'id': 'test_results', 'args': {'passed': 0, 'total': 0}}] diff --git a/server/python_session.py b/server/python_session.py index 648a901..161ba0d 100644 --- a/server/python_session.py +++ b/server/python_session.py @@ -2,6 +2,7 @@ import multiprocessing.managers import threading +import server.user_session from db.models import Problem from . import problems @@ -59,24 +60,27 @@ class PythonSession(object): self._python.destroy(self._interpreter) self._interpreter = None - def hint(self, user_id, problem_id, program): + def hint(self, sid, problem_id, program): + session = server.user_session.get_session_by_id(sid) language, problem_group, problem = Problem.get_identifier(problem_id) + problem_module = problems.load_problem(language, problem_group, problem, 'common') # Try problem-specific hints. - problem_module = problems.load_problem(language, problem_group, problem, 'common') if hasattr(problem_module, 'hint'): - hints = problem_module.hint(program) + hints = problem_module.hint(session, program) if hints: return hints # Finally return a generic "try thinking a bit" message. return [{'id': 'no_hint'}] - def test(self, user_id, problem_id, program): + def test(self, sid, problem_id, program): + session = server.user_session.get_session_by_id(sid) language, problem_group, problem = Problem.get_identifier(problem_id) problem_module = problems.load_problem(language, problem_group, problem, 'common') + try: - n_correct, n_all = problem_module.test(program) + n_correct, n_all = problem_module.test(session, program) return [{'id': 'test_results', 'args': {'passed': n_correct, 'total': n_all}}] except AttributeError as ex: return [{'id': 'test_results', 'args': {'passed': 0, 'total': 0}}] diff --git a/wsgi_server.py b/wsgi_server.py index 2597403..fda576b 100644 --- a/wsgi_server.py +++ b/wsgi_server.py @@ -161,7 +161,7 @@ class Hint(CodeqService): else: return {'code': 3, 'message': 'Unknown language specified'} - hints = lang_session.hint(session.get_uid(), problem_id, program) + hints = lang_session.hint(session.get_sid(), problem_id, program) return {'code': 0, 'message': 'ok', 'hints': hints} class Test(CodeqService): @@ -182,7 +182,7 @@ class Test(CodeqService): else: return {'code': 3, 'message': 'Unknown language specified'} - hints = lang_session.test(session.get_uid(), problem_id, program) + hints = lang_session.test(session.get_sid(), problem_id, program) return {'code': 0, 'message': 'ok', 'hints': hints} class GetProblem(CodeqService): |