app.py 23.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
'''
File:           perguntations/app.py
Description:    Main application logic.
'''


# python standard libraries
import asyncio
# from contextlib import contextmanager  # `with` statement in db sessions
import csv
import io
import json
import logging
from os import path

# installed packages
import bcrypt
from sqlalchemy import create_engine, select, func
from sqlalchemy.orm import Session
from sqlalchemy.exc import NoResultFound
# from sqlalchemy.orm import sessionmaker

# this project
from perguntations.models import Student, Test, Question
from perguntations.questions import question_from
from perguntations.tools import load_yaml
from perguntations.testfactory import TestFactory, TestFactoryException
import perguntations.test

# setup logger for this module
logger = logging.getLogger(__name__)


# ============================================================================
class AppException(Exception):
    '''Exception raised in this module'''


# ============================================================================
# helper functions
# ============================================================================
# async def check_password(try_pw, hashed_pw):
#     '''check password in executor'''
#     try_pw = try_pw.encode('utf-8')
#     loop = asyncio.get_running_loop()
#     hashed = await loop.run_in_executor(None, bcrypt.hashpw, try_pw, hashed_pw)
#     return hashed_pw == hashed


# async def hash_password(password):
#     '''hash password in executor'''
#     loop = asyncio.get_running_loop()
#     return await loop.run_in_executor(None, bcrypt.hashpw,
#                                       password.encode('utf-8'),
#                                       bcrypt.gensalt())


# ============================================================================
# main application
# ============================================================================
class App():
    '''
    This is the main application
    state:
      self.Session
      self.online - {uid:
                         {'student':{...}, 'test': {...}},
                          ...
                     }
      self.allowd - {'123', '124', ...}
      self.testfactory - TestFactory
    '''

    # # ------------------------------------------------------------------------
    # @contextmanager
    # def _db_session(self):
    #     '''
    #     helper to manage db sessions using the `with` statement, for example:
    #     with self._db_session() as s:  s.query(...)
    #     '''
    #     session = self.Session()
    #     try:
    #         yield session
    #         session.commit()
    #     except exc.SQLAlchemyError:
    #         logger.error('DB rollback!!!')
    #         session.rollback()
    #         raise
    #     finally:
    #         session.close()

    # ------------------------------------------------------------------------
    def __init__(self, conf):
        self.online = dict()    # {uid: {'student':{...}, 'test': {...}}, ...}
        self.allowed = set()    # '0' is hardcoded to allowed elsewhere
        self.unfocus = set()    # set of students that have no browser focus
        self.area = dict()      # {uid: percent_area}
        self.pregenerated_tests = []  # list of tests to give to students

        self._make_test_factory(conf)
        self._db_setup()

        # command line option --allow-all
        if conf['allow_all']:
            self.allow_all_students()
        elif conf['allow_list'] is not None:
            self.allow_list(conf['allow_list'])
        else:
            logger.info('Students not yet allowed to login.')

        # pre-generate tests for allowed students
        if self.allowed:
            logger.info('Generating %d tests. May take awhile...',
                        len(self.allowed))
            self._pregenerate_tests(len(self.allowed))
        else:
            logger.info('No tests generated yet.')

        if conf['correct']:
            self._correct_tests()

    # ------------------------------------------------------------------------
    def _db_setup(self) -> None:
        logger.info('Setup database')

        # connect to database and check registered students
        dbfile = path.expanduser(self.testfactory['database'])
        if not path.exists(dbfile):
            raise AppException('Database does not exist. Use "initdb" to create.')
        self._engine = create_engine(f'sqlite:///{dbfile}', future=True)

        try:
            with Session(self._engine, future=True) as session:
                num = session.execute(
                    select(func.count(Student.id)).where(Student.id != '0')
                ).scalar()
        except Exception as exc:
            raise AppException(f'Database unusable {dbfile}.') from exc

        logger.info('Database "%s" has %s students.', dbfile, num)

    # ------------------------------------------------------------------------
    def _correct_tests(self):
        with Session(self._engine, future=True) as session:
            # Find which tests have to be corrected
            dbtests = session.execute(
                select(Test).
                where(Test.ref == self.testfactory['ref']).
                where(Test.state == "SUBMITTED")
            ).all()
            # dbtests = session.query(Test)\
            #         .filter(Test.ref == self.testfactory['ref'])\
            #         .filter(Test.state == "SUBMITTED")\
            #         .all()

            logger.info('Correcting %d tests...', len(dbtests))
            for dbtest in dbtests:
                try:
                    with open(dbtest.filename) as file:
                        testdict = json.load(file)
                except FileNotFoundError:
                    logger.error('File not found: %s', dbtest.filename)
                    continue

                # creates a class Test with the methods to correct it
                # the questions are still dictionaries, so we have to call
                # question_from() to produce Question() instances that can be
                # corrected. Finally the test can be corrected.
                test = perguntations.test.Test(testdict)
                test['questions'] = [question_from(q) for q in test['questions']]
                test.correct()
                logger.info('Student %s:  grade = %f', test['student']['number'], test['grade'])

                # save JSON file (overwriting the old one)
                uid = test['student']['number']
                ref = test['ref']
                finish_time = test['finish_time']
                answers_dir = test['answers_dir']
                fname = f'{uid}--{ref}--{finish_time}.json'
                fpath = path.join(answers_dir, fname)
                test.save_json(fpath)
                logger.info('%s saved JSON file.', uid)

                # update database
                dbtest.grade = test['grade']
                dbtest.state = test['state']
                dbtest.questions = [
                    Question(
                        number=n,
                        ref=q['ref'],
                        grade=q['grade'],
                        comment=q.get('comment', ''),
                        starttime=str(test['start_time']),
                        finishtime=str(test['finish_time']),
                        test_id=test['ref']
                        )
                    for n, q in enumerate(test['questions'])
                    ]
                logger.info('%s database updated.', uid)

    # ------------------------------------------------------------------------
    async def login(self, uid, try_pw, headers=None):
        '''login authentication'''
        if uid not in self.allowed and uid != '0':      # not allowed
            logger.warning('"%s" unauthorized.', uid)
            return 'unauthorized'

        with Session(self._engine, future=True) as session:
        # with self._db_session() as sess:
            name, hashed_pw = session.execute(
                    select(Student.name, Student.password).
                    where(id == uid)
            ).one()
            # name, hashed_pw = sess.query(Student.name, Student.password)\
            #                      .filter_by(id=uid)\
            #                      .one()

        if hashed_pw == '':              # update password on first login
            await self.update_student_password(uid, try_pw)
            pw_ok = True
        else:                           # check password
            loop = asyncio.get_running_loop()
            pw_ok = await loop.run_in_executor(None,
                                                bcrypt.checkpw,
                                                try_pw.encode('utf-8'),
                                                hashed_pw.password)
            # pw_ok = await check_password(try_pw, hashed_pw)  # async bcrypt

        if not pw_ok:        # wrong password
            logger.info('"%s" wrong password.', uid)
            return 'wrong_password'

        # success
        self.allowed.discard(uid)  # remove from set of allowed students

        if uid in self.online:
            logger.warning('"%s" login again from %s (reusing state).',
                           uid, headers['remote_ip'])
            # FIXME invalidate previous login
        else:
            self.online[uid] = {'student': {
                                    'name': name,
                                    'number': uid,
                                    'headers': headers}}
            logger.info('"%s" login from %s.', uid, headers['remote_ip'])

    # ------------------------------------------------------------------------
    def logout(self, uid):
        '''student logout'''
        self.online.pop(uid, None)  # remove from dict if exists
        logger.info('"%s" logged out.', uid)

    # ------------------------------------------------------------------------
    def _make_test_factory(self, conf):
        '''
        Setup a factory for the test
        '''

        # load configuration from yaml file
        logger.info('Loading test configuration "%s".', conf["testfile"])
        try:
            testconf = load_yaml(conf['testfile'])
        except Exception as exc:
            msg = 'Error loading test configuration YAML.'
            logger.critical(msg)
            raise AppException(msg) from exc

        # command line options override configuration
        testconf.update(conf)

        # start test factory
        logger.info('Running test factory...')
        try:
            self.testfactory = TestFactory(testconf)
        except TestFactoryException as exc:
            logger.critical(exc)
            raise AppException('Failed to create test factory!') from exc

    # ------------------------------------------------------------------------
    def _pregenerate_tests(self, num):  # TODO needs improvement
        event_loop = asyncio.get_event_loop()
        self.pregenerated_tests += [
                event_loop.run_until_complete(self.testfactory.generate())
                for _ in range(num)]

    # ------------------------------------------------------------------------
    async def get_test_or_generate(self, uid):
        '''get current test or generate a new one'''
        try:
            student = self.online[uid]
        except KeyError as exc:
            msg = f'"{uid}" is not online. get_test_or_generate() FAILED'
            logger.error(msg)
            raise AppException(msg) from exc

        # get current test. if test does not exist then generate a new one
        if not 'test' in student:
            await self._new_test(uid)

        return student['test']

    def get_test(self, uid):
        '''get test from online student or raise exception'''
        return self.online[uid]['test']

    # ------------------------------------------------------------------------
    async def _new_test(self, uid):
        '''
        assign a test to a given student. if there are pregenerated tests then
        use one of them, otherwise generate one.
        the student must be online
        '''
        student = self.online[uid]['student']  # {'name': ?, 'number': ?}

        try:
            test = self.pregenerated_tests.pop()
        except IndexError:
            logger.info('"%s" generating new test...', uid)
            test = await self.testfactory.generate()
            logger.info('"%s" test is ready.', uid)
        else:
            logger.info('"%s" using a pregenerated test.', uid)

        test.start(student)           # student signs the test
        self.online[uid]['test'] = test

    # ------------------------------------------------------------------------
    async def submit_test(self, uid, ans):
        '''
        Handles test submission and correction.

        ans is a dictionary {question_index: answer, ...} with the answers for
        the complete test. For example:  {0:'hello', 1:[1,2]}
        '''
        test = self.online[uid]['test']

        # --- submit answers and correct test
        test.submit(ans)
        logger.info('"%s" submitted %d answers.', uid, len(ans))

        if test['autocorrect']:
            await test.correct_async()
            logger.info('"%s" grade = %g points.', uid, test['grade'])

        # --- save test in JSON format
        fname = f'{uid}--{test["ref"]}--{test["finish_time"]}.json'
        fpath = path.join(test['answers_dir'], fname)
        test.save_json(fpath)
        logger.info('"%s" saved JSON.', uid)

        # --- insert test and questions into the database
        #     only corrected questions are added
        test_row = Test(
            ref=test['ref'],
            title=test['title'],
            grade=test['grade'],
            state=test['state'],
            comment=test['comment'],
            starttime=str(test['start_time']),
            finishtime=str(test['finish_time']),
            filename=fpath,
            student_id=uid)

        if test['state'] == 'CORRECTED':
            test_row.questions = [
                Question(
                    number=n,
                    ref=q['ref'],
                    grade=q['grade'],
                    comment=q.get('comment', ''),
                    starttime=str(test['start_time']),
                    finishtime=str(test['finish_time']),
                    test_id=test['ref']
                    )
                for n, q in enumerate(test['questions'])
                ]

        with self._db_session() as sess:
            sess.add(test_row)
        logger.info('"%s" database updated.', uid)

    # ------------------------------------------------------------------------
    def get_student_grade(self, uid):
        return self.online[uid]['test'].get('grade', None)

    # ------------------------------------------------------------------------
    # def giveup_test(self, uid):
    #     '''giveup test - not used??'''
    #     test = self.online[uid]['test']
    #     test.giveup()

    #     # save JSON with the test
    #     fields = (test['student']['number'], test['ref'],
    #               str(test['finish_time']))
    #     fname = '--'.join(fields) + '.json'
    #     fpath = path.join(test['answers_dir'], fname)
    #     test.save_json(fpath)

    #     # insert test into database
    #     with self._db_session() as sess:
    #         sess.add(Test(ref=test['ref'],
    #                       title=test['title'],
    #                       grade=test['grade'],
    #                       starttime=str(test['start_time']),
    #                       finishtime=str(test['finish_time']),
    #                       filename=fpath,
    #                       # student_id=test['student']['number'],
    #                       state=test['state'],
    #                       comment=''))

    #     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':
            if value:
                self._focus_student(uid)
            else:
                self._unfocus_student(uid)
        elif cmd == 'size':
            self._set_screen_area(uid, value)

    # ------------------------------------------------------------------------
    # --- GETTERS
    # ------------------------------------------------------------------------

    # def get_student_name(self, uid):
    #     return self.online[uid]['student']['name']

    def get_questions_csv(self):
        '''generates a CSV with the grades of the test'''
        test_ref = self.testfactory['ref']
        with self._db_session() as sess:
            questions = sess.query(Test.id, Test.student_id, Test.starttime,
                                   Question.number, Question.grade)\
                            .join(Question)\
                            .filter(Test.ref == test_ref)\
                            .all()

            qnums = set()  # keeps track of all the questions in the test
            tests = {}      # {test_id: {student_id, starttime, 0: grade, ...}}
            for question in questions:
                test_id, student_id, starttime, num, grade = question
                default_test_id = {'Aluno': student_id, 'Início': starttime}
                tests.setdefault(test_id, default_test_id)[num] = grade
                qnums.add(num)

        if not tests:
            logger.warning('Empty CSV: there are no tests!')
            return test_ref, ''

        cols = ['Aluno', 'Início'] + list(qnums)

        csvstr = io.StringIO()
        writer = csv.DictWriter(csvstr, fieldnames=cols, restval=None,
                                delimiter=';', quoting=csv.QUOTE_ALL)
        writer.writeheader()
        writer.writerows(tests.values())
        return test_ref, csvstr.getvalue()

    def get_test_csv(self):
        '''generates a CSV with the grades of the test currently running'''
        test_ref = self.testfactory['ref']
        with self._db_session() as sess:
            tests = sess.query(Test.student_id,
                                Test.grade,
                                Test.starttime, Test.finishtime)\
                         .filter(Test.ref == test_ref)\
                         .order_by(Test.student_id)\
                         .all()

        if not tests:
            logger.warning('Empty CSV: there are no tests!')
            return test_ref, ''

        csvstr = io.StringIO()
        writer = csv.writer(csvstr, delimiter=';', quoting=csv.QUOTE_ALL)
        writer.writerow(('Aluno', 'Nota', 'Início', 'Fim'))
        writer.writerows(tests)

        return test_ref, csvstr.getvalue()

    # ------------------------------------------------------------------------
    def get_student_grades_from_all_tests(self, uid):
        '''get grades of student from all tests'''
        with self._db_session() as sess:
            return sess.query(Test.title, Test.grade, Test.finishtime)\
                       .filter_by(student_id=uid)\
                       .order_by(Test.finishtime)

    def get_json_filename_of_test(self, test_id):
        '''get JSON filename from database given the test_id'''
        with self._db_session() as sess:
            return sess.query(Test.filename)\
                       .filter_by(id=test_id)\
                       .scalar()

    def get_student_grades_from_test(self, uid, testid):
        '''get grades of student for a given testid'''
        with self._db_session() as sess:
            return sess.query(Test.grade, Test.finishtime, Test.id)\
                       .filter_by(student_id=uid)\
                       .filter_by(ref=testid)\
                       .all()

    def get_students_state(self):
        '''get list of states of every student'''
        return [{
            'uid': uid,
            'name': name,
            'allowed': uid in self.allowed,
            'online': uid in self.online,
            'start_time': self.online.get(uid, {}).get('test', {})
                          .get('start_time', ''),
            'password_defined': pw != '',
            'unfocus': uid in self.unfocus,
            'area': self.area.get(uid, None),
            'grades': self.get_student_grades_from_test(
                uid, self.testfactory['ref'])
            } for uid, name, pw in self._get_all_students()]

    # --- private methods ----------------------------------------------------
    def _get_all_students(self):
        '''get all students from database'''
        with self._db_session() as sess:
            return sess.query(Student.id, Student.name, Student.password)\
                       .filter(Student.id != '0')\
                       .order_by(Student.id)

    # def get_allowed_students(self):
    #     # set of 'uid' allowed to login
    #     return self.allowed

    # def get_file(self, uid, ref, key):
    #     # get filename of (uid, ref, name) if declared in the question
    #     t = self.get_student_test(uid)
    #     for q in t['questions']:
    #         if q['ref'] == ref and key in q['files']:
    #             return path.abspath(path.join(q['path'], q['files'][key]))

    # ------------------------------------------------------------------------
    # --- SETTERS
    # ------------------------------------------------------------------------

    def allow_student(self, uid):
        '''allow a single student to login'''
        self.allowed.add(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('"%s" denied to login', uid)

    def allow_all_students(self):
        '''allow all students to login'''
        all_students = self._get_all_students()
        self.allowed.update(s[0] for s in all_students)
        logger.info('Allowed all %d students.', len(self.allowed))

    def deny_all_students(self):
        '''deny all students to login'''
        logger.info('Denying all students...')
        self.allowed.clear()

    def allow_list(self, filename):
        '''allow students listed in file (one number per line)'''
        try:
            with open(filename, 'r') as file:
                allowed_in_file = {s.strip() for s in file} - {''}
        except Exception as exc:
            error_msg = f'Cannot read file {filename}'
            logger.critical(error_msg)
            raise AppException(error_msg) from exc

        enrolled = set(s[0] for s in self._get_all_students())  # in database
        self.allowed.update(allowed_in_file & enrolled)
        logger.info('Allowed %d students provided in "%s"', len(self.allowed),
                    filename)

        not_enrolled = allowed_in_file - enrolled
        if not_enrolled:
            logger.warning('  but found students not in the database:  %s',
                           ', '.join(not_enrolled))

    def _focus_student(self, uid):
        '''set student in focus state'''
        self.unfocus.discard(uid)
        logger.info('"%s" focus', uid)

    def _unfocus_student(self, uid):
        '''set student in unfocus state'''
        self.unfocus.add(uid)
        logger.info('"%s" unfocus', uid)

    def _set_screen_area(self, uid, sizes):
        '''set current browser area as detected in resize event'''
        scr_y, scr_x, win_y, win_x = sizes
        area = win_x * win_y / (scr_x * scr_y) * 100
        self.area[uid] = area
        logger.info('"%s" area=%g%%, window=%dx%d, screen=%dx%d',
                    uid, area, win_x, win_y, scr_x, scr_y)

    async def update_student_password(self, uid, password=''):
        '''change password on the database'''
        if password:
            password = await hash_password(password)
        with self._db_session() as sess:
            student = sess.query(Student).filter_by(id=uid).one()
            student.password = password
        logger.info('"%s" password updated.', uid)

    def insert_new_student(self, uid, name):
        '''insert new student into the database'''
        try:
            with self._db_session() as sess:
                sess.add(Student(id=uid, name=name, password=''))
        except exc.SQLAlchemyError:
            logger.error('Insert failed: student %s already exists?', uid)
        else:
            logger.info('New student: "%s", "%s"', uid, name)