From a81c224118174dd8bd9d44e82e2a108c5f45b36c Mon Sep 17 00:00:00 2001 From: Miguel Barão Date: Tue, 12 May 2020 14:38:39 +0100 Subject: [PATCH] - fix regression with allow_all button - simplified log messages --- perguntations/__init__.py | 2 +- perguntations/app.py | 50 ++++++++++++++++++++++++++++++-------------------- perguntations/serve.py | 6 ++++-- perguntations/static/js/admin.js | 6 ++++-- 4 files changed, 39 insertions(+), 25 deletions(-) diff --git a/perguntations/__init__.py b/perguntations/__init__.py index 549c2f6..effaa33 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.05.dev4' +APP_VERSION = '2020.05.dev5' APP_DESCRIPTION = __doc__ __author__ = 'Miguel Barão' diff --git a/perguntations/app.py b/perguntations/app.py index 3ece150..ca6c868 100644 --- a/perguntations/app.py +++ b/perguntations/app.py @@ -85,7 +85,7 @@ class App(): # ------------------------------------------------------------------------ def __init__(self, conf): self.online = dict() # {uid: {'student':{...}, 'test': {...}}, ...} - self.allowed = set([]) # '0' is hardcoded to allowed elsewhere + self.allowed = set() # '0' is hardcoded to allowed elsewhere logger.info('Loading test configuration "%s".', conf["testfile"]) try: @@ -138,7 +138,7 @@ class App(): async def login(self, uid, try_pw): '''login authentication''' if uid not in self.allowed and uid != '0': # not allowed - logger.warning('Student %s: not allowed to login.', uid) + logger.warning('"%s" not allowed to login.', uid) return False # get name+password from db @@ -157,34 +157,34 @@ class App(): if pw_ok: # success self.allowed.discard(uid) # remove from set of allowed students if uid in self.online: - logger.warning('Student %s: already logged in.', uid) + logger.warning('"%s" already logged in.', uid) else: # make student online self.online[uid] = {'student': {'name': name, 'number': uid}} - logger.info('Student %s: logged in.', uid) + logger.info('"%s" logged in.', uid) return True # wrong password - logger.info('Student %s: wrong password.', uid) + logger.info('"%s" wrong password.', uid) return False # ------------------------------------------------------------------------ def logout(self, uid): '''student logout''' self.online.pop(uid, None) # remove from dict if exists - logger.info('Student %s: logged out.', uid) + logger.info('"%s" logged out.', uid) # ------------------------------------------------------------------------ async def generate_test(self, uid): '''generate a test for a given student''' if uid in self.online: - logger.info('Student %s: generating new test.', uid) + logger.info('"%s" generating new test.', uid) student_id = self.online[uid]['student'] # {number, name} test = await self.testfactory.generate(student_id) self.online[uid]['test'] = test - logger.info('Student %s: test is ready.', uid) + logger.info('"%s" test is ready.', uid) return self.online[uid]['test'] # this implies an error in the code. should never be here! - logger.critical('Student %s: offline, can\'t generate test', uid) + logger.critical('"%s" offline, can\'t generate test', uid) # ------------------------------------------------------------------------ async def correct_test(self, uid, ans): @@ -198,10 +198,10 @@ class App(): # --- submit answers and correct test test.update_answers(ans) - logger.info('Student %s: %d answers submitted.', uid, len(ans)) + logger.info('"%s" submitted %d answers.', uid, len(ans)) grade = await test.correct() - logger.info('Student %s: grade = %g points.', uid, grade) + logger.info('"%s" grade = %g points.', uid, grade) # --- save test in JSON format fields = (uid, test['ref'], str(test['finish_time'])) @@ -210,7 +210,7 @@ class App(): with open(path.expanduser(fpath), 'w') as file: # default=str required for datetime objects json.dump(test, file, indent=2, default=str) - logger.info('Student %s: saved JSON.', uid) + logger.info('"%s" saved JSON.', uid) # --- insert test and questions into database with self.db_session() as sess: @@ -233,7 +233,7 @@ class App(): test_id=test['ref']) for q in test['questions'] if 'grade' in q]) - logger.info('Student %s: database updated.', uid) + logger.info('"%s" database updated.', uid) return grade # ------------------------------------------------------------------------ @@ -261,18 +261,18 @@ class App(): state=test['state'], comment='')) - logger.info('Student %s: gave up.', uid) + logger.info('"%s" gave up.', uid) return test # ------------------------------------------------------------------------ def event_test(self, uid, cmd, value): '''handles browser events the occur during the test''' if cmd == 'focus': - logger.info('Student %s: focus %s', uid, value) + logger.info('"%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', + logger.info('"%s": area=%g%%, window=%dx%d, screen=%dx%d', uid, area, win_x, win_y, scr_x, scr_y) # ------------------------------------------------------------------------ @@ -396,12 +396,22 @@ class App(): def allow_student(self, uid): '''allow a single student to login''' self.allowed.add(uid) - logger.info('Student %s: allowed to login.', uid) + logger.info('"%s" allowed to login.', uid) def deny_student(self, uid): '''deny a single student to login''' self.allowed.discard(uid) - logger.info('Student %s: denied to login', uid) + logger.info('"%s" denied to login', uid) + + def allow_all_students(self): + '''allow all students to login''' + logger.info('Allowing all students...') + self.allowed = set(s[0] for s in self.get_all_students()) + + def deny_all_students(self): + '''deny all students to login''' + logger.info('Denying all students...') + self.allowed = set() async def update_student_password(self, uid, password=''): '''change password on the database''' @@ -410,7 +420,7 @@ class App(): with self.db_session() as sess: student = sess.query(Student).filter_by(id=uid).one() student.password = password - logger.info('Student %s: password updated.', uid) + logger.info('"%s" password updated.', uid) def insert_new_student(self, uid, name): '''insert new student into the database''' @@ -420,4 +430,4 @@ class App(): except exc.SQLAlchemyError: logger.error('Insert failed: student %s already exists?', uid) else: - logger.info('New student inserted: %s, %s', uid, name) + logger.info('New student: "%s", "%s"', uid, name) diff --git a/perguntations/serve.py b/perguntations/serve.py index de01cae..50f27f1 100644 --- a/perguntations/serve.py +++ b/perguntations/serve.py @@ -241,10 +241,12 @@ class AdminHandler(BaseHandler): if cmd == 'allow': self.testapp.allow_student(value) - elif cmd == 'deny': self.testapp.deny_student(value) - + elif cmd == 'allow_all': + self.testapp.allow_all_students() + elif cmd == 'deny_all': + self.testapp.deny_all_students() elif cmd == 'reset_password': await self.testapp.update_student_password(uid=value, password='') diff --git a/perguntations/static/js/admin.js b/perguntations/static/js/admin.js index 5970e78..88aa961 100644 --- a/perguntations/static/js/admin.js +++ b/perguntations/static/js/admin.js @@ -15,10 +15,12 @@ $(document).ready(function() { function button_handlers() { // button handlers (runs once) $("#allow_all").click(function() { - $(":checkbox").prop("checked", true).trigger('change'); + $.postJSON("/admin", {"cmd": "allow_all"}); + // $(":checkbox").prop("checked", true).trigger('change'); }); $("#deny_all").click(function() { - $(":checkbox").prop("checked", false).trigger('change'); + $.postJSON("/admin", {"cmd": "deny_all"}); + // $(":checkbox").prop("checked", false).trigger('change'); }); $("#reset_password").click(function () { $.postJSON("/admin", { -- libgit2 0.21.2