diff --git a/perguntations/__init__.py b/perguntations/__init__.py index b863c63..1c8cd7f 100644 --- a/perguntations/__init__.py +++ b/perguntations/__init__.py @@ -32,7 +32,7 @@ proof of submission and for review. ''' APP_NAME = 'perguntations' -APP_VERSION = '2020.04.dev2' +APP_VERSION = '2020.04.dev3' APP_DESCRIPTION = __doc__ __author__ = 'Miguel BarĂ£o' diff --git a/perguntations/app.py b/perguntations/app.py index 2601430..eb9fd2a 100644 --- a/perguntations/app.py +++ b/perguntations/app.py @@ -257,6 +257,17 @@ class App(): return test # ------------------------------------------------------------------------ + def event_test(self, uid, cmd, value): + '''handle browser events the occur during the test''' + if cmd == 'focus': + logger.info('Student %s: focus %s', uid, value) + elif cmd == 'size': + scr_y, scr_x, win_y, win_x = value + area = win_x * win_y / (scr_x * scr_y) * 100 + logger.info('Student %s: area=%g%%, window=%dx%d, screen=%dx%d', + uid, area, win_x, win_y, scr_x, scr_y) + + # ------------------------------------------------------------------------ # --- helpers (getters) # def get_student_name(self, uid): diff --git a/perguntations/serve.py b/perguntations/serve.py index 799c683..3919ab0 100644 --- a/perguntations/serve.py +++ b/perguntations/serve.py @@ -42,6 +42,7 @@ class WebApplication(tornado.web.Application): (r'/file', FileHandler), # (r'/root', MainHandler), # FIXME # (r'/ws', AdminSocketHandler), + (r'/studentwebservice', StudentWebservice), (r'/', RootHandler), ] @@ -152,13 +153,24 @@ class BaseHandler(tornado.web.RequestHandler): # AdminSocketHandler.update_cache(chat) # store msgs # AdminSocketHandler.send_updates(chat) # send to clients +class StudentWebservice(BaseHandler): + ''' + Receive ajax from students in the test: + focus, unfocus + ''' + + @tornado.web.authenticated + def post(self): + '''handle ajax post''' + uid = self.current_user + cmd = self.get_body_argument('cmd', None) + value = json.loads(self.get_body_argument('value', None)) + self.testapp.event_test(uid, cmd, value) # ---------------------------------------------------------------------------- class AdminHandler(BaseHandler): '''Handle /admin''' - # SUPPORTED_METHODS = ['GET', 'POST'] - @tornado.web.authenticated @admin_only async def get(self): @@ -405,7 +417,9 @@ class TestHandler(BaseHandler): # --- REVIEW ----------------------------------------------------------------- class ReviewHandler(BaseHandler): - # SUPPORTED_METHODS = ['GET'] + ''' + Show test for review + ''' _templates = { 'radio': 'review-question-radio.html', @@ -445,7 +459,6 @@ class ReviewHandler(BaseHandler): templ=self._templates) - # ---------------------------------------------------------------------------- def signal_handler(sig, frame): ''' diff --git a/perguntations/static/js/detect_unfocus.js b/perguntations/static/js/detect_unfocus.js index b4798e1..f109e84 100644 --- a/perguntations/static/js/detect_unfocus.js +++ b/perguntations/static/js/detect_unfocus.js @@ -1,38 +1,54 @@ +// from: https://www.tornadoweb.org/en/stable/guide/security.html +// with changes: removed datatype and callback from original postJSON +function getCookie(name) { + var r = document.cookie.match("\\b" + name + "=([^;]*)\\b"); + return r ? r[1] : undefined; +} + +jQuery.postJSON = function(url, args) { + args._xsrf = getCookie("_xsrf"); + $.ajax({url: url, data: $.param(args), type: "POST"}); +}; + + $(document).ready(function() { + $.postJSON("/studentwebservice", { + "cmd": "size", + "value": JSON.stringify([screen.height, screen.width, $(window).height(), $(window).width()]), + }); + $(window).focus(function(){ - $.ajax({ - type: "POST", - url: "/studentwebservice", - data: { - "cmd": "focus", - "number": $("#number").text(), - "value": true - } + $.postJSON("/studentwebservice", { + "cmd": "focus", + "value": true, }); }); - $(window).blur(function(e){ - $.ajax({ - type: "POST", - url: "/studentwebservice", - data: { - "cmd": "focus", - "number": $("#number").text(), - "value": false - } + $(window).blur(function(){ + $.postJSON("/studentwebservice", { + "cmd": "focus", + "value": false, }); }); $(window).resize(function(){ - var n = $(window).scrollTop(); - $.ajax({ - type: "POST", - url: "/adminwebservice", - data: { - "cmd": "resize", - "name": $("#number").text(), - "scroll": n - } + $.postJSON("/studentwebservice", { + "cmd": "size", + "value": JSON.stringify([screen.height, screen.width, $(window).height(), $(window).width()]), }); + + + // // var n = $(window).scrollTop(); + // $.postJSON({"/studentwebservice", { + // "cmd": "size", + // "value": JSON.stringify([screen.height, screen.width, $(window).height(), $(window).width()]), + // }}); + // // data: { + // // "cmd": "resize", + // // "name": $("#number").text(), + // // "scroll": n + // // } + // // }); }); -}); \ No newline at end of file + +}); diff --git a/perguntations/templates/test.html b/perguntations/templates/test.html index 9ca565a..e117f0c 100644 --- a/perguntations/templates/test.html +++ b/perguntations/templates/test.html @@ -38,6 +38,7 @@ + diff --git a/perguntations/tools.py b/perguntations/tools.py index f7e114c..e75663c 100644 --- a/perguntations/tools.py +++ b/perguntations/tools.py @@ -67,21 +67,16 @@ def run_script(script: str, stderr=subprocess.STDOUT, universal_newlines=True, timeout=timeout, - check=False, + check=True, ) - except OSError: - logger.error('Can not execute script "%s".', script) - return output except subprocess.TimeoutExpired: logger.error('Timeout %ds exceeded running "%s".', timeout, script) return output - except Exception: - logger.error('An Exception ocurred running "%s".', script) + except subprocess.CalledProcessError as exc: + logger.error('Return code %d running "%s".', exc.returncode, script) return output - - # --- check return code - if proc.returncode != 0: - logger.error('Return code %d running "%s".', proc.returncode, script) + except OSError: + logger.error('Can not execute script "%s".', script) return output # --- parse yaml -- libgit2 0.21.2