From 778c815e58fb217dbdd3986ff662fc43c06f779e Mon Sep 17 00:00:00 2001 From: Miguel Barão Date: Tue, 14 Jun 2016 19:34:09 +0100 Subject: [PATCH] - fixed debug loglevel when command line option --debug is active. - fixed giveup() not logging out stude - insert new student on /admin --- BUGS.md | 6 +++++- app.py | 13 ++++++++++++- config/logger-debug.yaml | 8 ++++---- serve.py | 19 +++++++++++-------- static/js/admin.js | 14 +++++++++++--- templates/admin.html | 25 ++++++++++++++++++++++--- templates/test.html | 4 +--- test.py | 6 ------ 8 files changed, 66 insertions(+), 29 deletions(-) diff --git a/BUGS.md b/BUGS.md index a175add..bb8ad8a 100644 --- a/BUGS.md +++ b/BUGS.md @@ -3,7 +3,7 @@ - usar thread.Lock para aceder a variaveis de estado? - permitir adicionar imagens nas perguntas. -- debug mode: log levels not working +- detect_unfocus.js so funciona se estiver inline no html. porquê??? # TODO @@ -24,6 +24,10 @@ # FIXED +- inserir novo aluno /admin não fecha. +- se aluno desistir, ainda fica marcado como online +- give dá None em vez de 0.0 +- debug mode: log levels not working - Se aluno fizer logout, o teste não é gravado e ficamos sem registo do teste que o aluno viu. - criar sqlalchemy sessions dentro de app de modo a estarem associadas a requests. ver se é facil usar with db:(...) para criar e fechar sessão. - sqlalchemy queixa-se de threads. diff --git a/app.py b/app.py index f904614..1c68a4a 100644 --- a/app.py +++ b/app.py @@ -158,11 +158,12 @@ class App(object): def giveup_test(self, uid): logger.info('Student {0}: gave up.'.format(uid)) t = self.online[uid]['test'] - t.giveup() + grade = t.giveup() if t['save_answers']: fname = ' -- '.join((t['student']['number'], t['ref'], str(t['finish_time']))) + '.json' fpath = path.abspath(path.join(t['answers_dir'], fname)) t.save_json(fpath) + return grade # ----------------------------------------------------------------------- @@ -245,3 +246,13 @@ class App(object): def set_user_ip(self, uid, ipaddress=''): self.online[uid]['student']['ip_address'] = ipaddress + + def insert_new_student(self, uid, name): + try: + with self.db_session() as s: + s.add(Student(id=uid, name=name, password='')) + s.commit() + except Exception: + logger.error('Insert failed: student {} already exists.'.format(uid)) + else: + logger.info('New student inserted into database: {}, {}'.format(uid, name)) diff --git a/config/logger-debug.yaml b/config/logger-debug.yaml index c8f9dcf..5c68ba5 100644 --- a/config/logger-debug.yaml +++ b/config/logger-debug.yaml @@ -9,19 +9,19 @@ formatters: handlers: default: - level: 'INFO' + level: 'DEBUG' class: 'logging.StreamHandler' formatter: 'standard' stream: 'ext://sys.stdout' cherrypy_console: - level: 'INFO' + level: 'DEBUG' class: 'logging.StreamHandler' formatter: 'standard' stream: 'ext://sys.stdout' cherrypy_access: - level: 'INFO' + level: 'DEBUG' class: 'logging.handlers.RotatingFileHandler' formatter: 'void' filename: 'logs/access.log' @@ -30,7 +30,7 @@ handlers: encoding: 'utf8' cherrypy_error: - level: 'INFO' + level: 'DEBUG' class: 'logging.handlers.RotatingFileHandler' formatter: 'void' filename: 'logs/errors.log' diff --git a/serve.py b/serve.py index 513038d..0dd25c5 100755 --- a/serve.py +++ b/serve.py @@ -99,12 +99,11 @@ class AdminWebService(object): elif args['cmd'] == 'reset': return self.app.reset_password(args['name']) + elif args['cmd'] == 'insert': + return self.app.insert_new_student(uid=args['number'], name=args['name']) + else: print(args) - # if args['cmd'] == 'focus': - # print('FOCUS', args['name']) - # elif args['cmd'] == 'blur': - # print('FOCUS', args['name']) # ============================================================================ # Webserver root @@ -177,7 +176,7 @@ class Root(object): # radio - all off -> no key, 1 on -> string '0' # text - always returns string. no answer '', otherwise 'dskdjs' uid = cherrypy.session.get(SESSION_KEY) - student_name = self.app.get_student_name(uid) + name = self.app.get_student_name(uid) title = self.app.get_test(uid)['title'] qq = self.app.get_test_qtypes(uid) # {'q1_ref': 'checkbox', ...} @@ -204,7 +203,7 @@ class Root(object): # --- Show result to student return self.template['grade'].render( title=title, - student_id=uid + ' - ' + student_name, + student_id=uid + ' - ' + name, grade=grade, allgrades=self.app.get_student_grades_from_all_tests(uid) ) @@ -214,7 +213,11 @@ class Root(object): @require() def giveup(self): uid = cherrypy.session.get(SESSION_KEY) + name = self.app.get_student_name(uid) + title = self.app.get_test(uid)['title'] + grade = self.app.giveup_test(uid) + self.app.logout(uid) # --- Expire session cherrypy.lib.sessions.expire() # session coockie expires client side @@ -222,8 +225,8 @@ class Root(object): # --- Show result to student return self.template['grade'].render( - title='title', - student_id=uid, + title=title, + student_id=uid + ' - ' + name, grade=grade, allgrades=self.app.get_student_grades_from_all_tests(uid) ) diff --git a/static/js/admin.js b/static/js/admin.js index 2722fe5..4711c41 100644 --- a/static/js/admin.js +++ b/static/js/admin.js @@ -21,9 +21,17 @@ $(document).ready(function() { }); } ); - $("#novo_aluno").click( + $("#inserir_novo_aluno").click( function () { - alert('Não implementado!'); + $.ajax({ + type: "POST", + url: "/adminwebservice", + data: { + "cmd": "insert", + "number": $("#novo_numero").val(), + "name": $("#novo_nome").val() + } + }); } ); } @@ -158,5 +166,5 @@ $(document).ready(function() { populate(); // run once when the page is loaded define_buttons_handlers(); - setInterval(populate, 5000); // poll server on 5s interval + setInterval(populate, 5000); // poll server on 5s interval }); diff --git a/templates/admin.html b/templates/admin.html index 3c787b8..bc76fe0 100644 --- a/templates/admin.html +++ b/templates/admin.html @@ -130,20 +130,39 @@
- +
- + -
+
+ + diff --git a/templates/test.html b/templates/test.html index 9fe89be..804737f 100644 --- a/templates/test.html +++ b/templates/test.html @@ -29,8 +29,8 @@ - +