diff --git a/questions.py b/questions.py index 02e8118..b93f963 100644 --- a/questions.py +++ b/questions.py @@ -401,7 +401,6 @@ class QuestionInformation(Question): '''An instance of QuestionInformation will always have the keys: type (str) text (str) - answer (None or an actual answer) points (0.0) ''' #------------------------------------------------------------------------ @@ -409,8 +408,7 @@ class QuestionInformation(Question): # create key/values as given in q super().__init__(q) self['text'] = self.get('text', '') - self['answer'] = None - self['points'] = 0.0 # FIXME shouldnt be defined on the test??? + self['points'] = 0.0 # always override the points #------------------------------------------------------------------------ # can return negative values for wrong answers diff --git a/serve.py b/serve.py index 153d957..87cc091 100755 --- a/serve.py +++ b/serve.py @@ -106,22 +106,22 @@ class Root(object): ans = {} for q in t['questions']: if 'answered-' + q['ref'] in kwargs: - ans[q['ref']] = kwargs.get(q['ref'], None) + # HACK: checkboxes in html return None instead of an empty list + # we have to manualy replace by [] + default_ans = [] if q['type'] == 'checkbox' else None + ans[q['ref']] = kwargs.get(q['ref'], default_ans) # store the answers in the Test, correct it, save JSON and # store results in the database t.update_answers(ans) - # try: t.correct() - # except: - # cherrypy.log.error('Failed to correct test of student %s' % t['number'], 'APPLICATION') - # t['grade'] = None if t['save_answers']: t.save_json(self.testconf['answers_dir']) self.database.save_test(t) if t['practice']: + # ---- Repeat the test ---- raise cherrypy.HTTPRedirect('/test') else: diff --git a/test.py b/test.py index 41ad5dc..b5355ac 100644 --- a/test.py +++ b/test.py @@ -1,10 +1,12 @@ -import os +import os, sys import random import yaml import json import sqlite3 from datetime import datetime + +# my code import questions import database @@ -13,10 +15,14 @@ def read_configuration(filename, debug=False, show_points=False, show_hints=Fals # FIXME validar se ficheiros e directorios existem??? if not os.path.isfile(filename): print('Cannot find file "%s"' % filename) - os.sys.exit(1) + sys.exit(1) - with open(filename, 'r') as f: - test = yaml.load(f) + try: + with open(filename, 'r') as f: + test = yaml.load(f) + except: + print('Error reading yaml file "{0}".'.format(filename)) + raise # defaults: test['ref'] = str(test.get('ref', filename)) @@ -35,7 +41,7 @@ def read_configuration(filename, debug=False, show_points=False, show_hints=Fals if 'database' not in test: print(' * Missing database in the test configuration.') - os.sys.exit(1) + sys.exit(1) if isinstance(test['files'], str): test['files'] = [test['files']] @@ -48,21 +54,31 @@ def read_configuration(filename, debug=False, show_points=False, show_hints=Fals # each question is a list of alternative versions, even if the list # contains only one element if isinstance(q, str): - # questions: some_ref --> questions: [{'ref':'some_ref', 'points':1.0, ...}] - test['questions'][i] = [pool[q]] + # normalize question to a dict + # - some_ref + # becomes + # - ref: some_ref + # points: 1.0 + test['questions'][i] = [pool[q]] # list with just one question test['questions'][i][0]['points'] = 1.0 + # Note: at this moment we do not know the questions types. + # Some questions, like information, should have default points + # set to 0. That must be done later when the question is + # instantiated. elif isinstance(q, dict): if isinstance(q['ref'], str): - q['ref'] = [q['ref']] - - p = float(q.get('points', 1.0)) + q['ref'] = [q['ref']] # ref is always a list + p = float(q.get('points', 1.0)) # default points is 1.0 + # create list of alternatives, normalized l = [] for r in q['ref']: qq = pool[r] qq['points'] = p l.append(qq) + + # add question (i.e. list of alternatives) to the test test['questions'][i] = l return test @@ -85,14 +101,8 @@ class Test(dict): '''given a dictionary ans={'ref':'some answer'} updates the answers of the test. FIXME: check if answer is to be corrected or not ''' - for i, q in enumerate(self['questions']): - if q['ref'] in ans: - if q['type'] == 'checkbox' and ans[q['ref']] == None: - # HACK: checkboxes in html return none instead of an empty list - # we have to manualy replace by [] - q['answer'] = [] - else: - q['answer'] = ans[q['ref']] + for q in self['questions']: + q['answer'] = ans[q['ref']] if q['ref'] in ans else None # ----------------------------------------------------------------------- def correct(self): -- libgit2 0.21.2