summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimotej Lazar <timotej.lazar@araneo.org>2015-09-03 18:24:42 +0200
committerTimotej Lazar <timotej.lazar@araneo.org>2015-09-03 18:24:42 +0200
commitc494a44b3c0e8faf77419c3cd5bef4da195e89c6 (patch)
treef8ec1c8a6e90d279c357941e10e82b9c49b37960
parent401bef2dcb434c23eb783131fb36d952043b9f31 (diff)
Pass the session object to test and hint functions
-rw-r--r--server/prolog_session.py15
-rw-r--r--server/python_session.py14
-rw-r--r--wsgi_server.py4
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):