Commit 61aa3d29f03f94d58b91df20714a50651ca6dd7a

Authored by Miguel Barão
1 parent 585e31fa
Exists in master and in 1 other branch dev

- changed handling of files to fix possible errors on downloads.

- fixed some errors when ./serve is run with wrong yaml files.
Showing 3 changed files with 21 additions and 20 deletions   Show diff stats
app.py
... ... @@ -51,17 +51,13 @@ class App(object):
51 51  
52 52 # ------------------------------------------------------------------------
53 53 def __init__(self, conf={}):
54   - logger.info('Starting application')
  54 + logger.info('Starting application.')
55 55 self.online = dict() # {uid: {'student':{...}, 'test': {...}}, ...}
56 56 self.allowed = set([]) # '0' is hardcoded to allowed elsewhere
57 57  
58   - # build test configuration dictionary
59   - testconf = {}
60   - if conf['filename']:
61   - logger.info(f'Loading test configuration "{conf["filename"]}".')
62   - testconf.update(load_yaml(conf['filename']))
63   -
64   - testconf.update(conf) # configuration overrides
  58 + logger.info(f'Loading test configuration "{conf["filename"]}".')
  59 + testconf = load_yaml(conf['filename'], default={})
  60 + testconf.update(conf) # configuration overrides from command line
65 61  
66 62 # start test factory
67 63 try:
... ...
serve.py
... ... @@ -17,7 +17,7 @@ import json
17 17 import tornado.ioloop
18 18 import tornado.web
19 19 import tornado.httpserver
20   -from tornado import template, gen
  20 +from tornado import template, gen, websocket
21 21 import yaml
22 22  
23 23 # this project
... ... @@ -48,6 +48,7 @@ class WebApplication(tornado.web.Application):
48 48 (r'/review', ReviewHandler),
49 49 (r'/admin', AdminHandler),
50 50 (r'/file', FileHandler),
  51 + # (r'/ws', SocketHandler),
51 52 (r'/', RootHandler), # TODO multiple tests
52 53 ]
53 54  
... ... @@ -138,6 +139,7 @@ class FileHandler(BaseHandler):
138 139 uid = self.current_user
139 140 ref = self.get_query_argument('ref', None)
140 141 image = self.get_query_argument('image', None)
  142 + content_type, encoding = mimetypes.guess_type(image)
141 143  
142 144 if uid != '0':
143 145 t = self.testapp.get_student_test(uid)
... ... @@ -149,6 +151,7 @@ class FileHandler(BaseHandler):
149 151 raise tornado.web.HTTPError(404) # Not Found
150 152  
151 153 for q in t['questions']:
  154 + # search for the question that contains the image
152 155 if q['ref'] == ref:
153 156 filepath = path.join(q['path'], 'public', image)
154 157  
... ... @@ -159,11 +162,11 @@ class FileHandler(BaseHandler):
159 162 except PermissionError:
160 163 logging.error(f'No permission: {filepath}')
161 164 else:
162   - content_type = mimetypes.guess_type(image)
163   - self.set_header("Content-Type", content_type[0])
164   - with f:
165   - self.write(f.read())
166   - await self.flush()
  165 + data = f.read()
  166 + f.close()
  167 + self.set_header("Content-Type", content_type)
  168 + self.write(data)
  169 + await self.flush()
167 170  
168 171  
169 172  
... ... @@ -416,6 +419,8 @@ def main():
416 419 except:
417 420 print('An error ocurred while setting up the logging system.')
418 421 sys.exit(1)
  422 +
  423 + # start logging
419 424 logging.info('===============================================')
420 425  
421 426 # --- start application
... ...
test.py
... ... @@ -44,21 +44,21 @@ class TestFactory(dict):
44 44 self.question_factory.load_files(files=self['files'], questions_dir=self['questions_dir'])
45 45  
46 46 # check if all questions exist ('ref' keys are correct?)
47   - errors_found = False
  47 + errors_found = []
48 48 for q in self['questions']:
49 49 for r in q['ref']:
50   - logger.info(f'Checking question "{r}".')
  50 + logger.info(f' Checking "{r}".')
51 51 try:
52 52 self.question_factory.generate(r)
53 53 except:
54 54 logger.critical(f'Can\'t generate question "{r}".')
55   - errors_found = True
  55 + errors_found.append(r)
56 56  
57 57 if errors_found:
58   - logger.critical('Errors found while generating questions.')
  58 + logger.critical(f'{errors_found} errors found generating questions.')
59 59 raise TestFactoryException()
60   -
61   - logger.info(f'Test factory ready for "{self["ref"]}".')
  60 + else:
  61 + logger.info(f'No errors found. Test factory ready for "{self["ref"]}".')
62 62  
63 63  
64 64 # -----------------------------------------------------------------------
... ...