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
@@ -51,17 +51,13 @@ class App(object): @@ -51,17 +51,13 @@ class App(object):
51 51
52 # ------------------------------------------------------------------------ 52 # ------------------------------------------------------------------------
53 def __init__(self, conf={}): 53 def __init__(self, conf={}):
54 - logger.info('Starting application') 54 + logger.info('Starting application.')
55 self.online = dict() # {uid: {'student':{...}, 'test': {...}}, ...} 55 self.online = dict() # {uid: {'student':{...}, 'test': {...}}, ...}
56 self.allowed = set([]) # '0' is hardcoded to allowed elsewhere 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 # start test factory 62 # start test factory
67 try: 63 try:
@@ -17,7 +17,7 @@ import json @@ -17,7 +17,7 @@ import json
17 import tornado.ioloop 17 import tornado.ioloop
18 import tornado.web 18 import tornado.web
19 import tornado.httpserver 19 import tornado.httpserver
20 -from tornado import template, gen 20 +from tornado import template, gen, websocket
21 import yaml 21 import yaml
22 22
23 # this project 23 # this project
@@ -48,6 +48,7 @@ class WebApplication(tornado.web.Application): @@ -48,6 +48,7 @@ class WebApplication(tornado.web.Application):
48 (r'/review', ReviewHandler), 48 (r'/review', ReviewHandler),
49 (r'/admin', AdminHandler), 49 (r'/admin', AdminHandler),
50 (r'/file', FileHandler), 50 (r'/file', FileHandler),
  51 + # (r'/ws', SocketHandler),
51 (r'/', RootHandler), # TODO multiple tests 52 (r'/', RootHandler), # TODO multiple tests
52 ] 53 ]
53 54
@@ -138,6 +139,7 @@ class FileHandler(BaseHandler): @@ -138,6 +139,7 @@ class FileHandler(BaseHandler):
138 uid = self.current_user 139 uid = self.current_user
139 ref = self.get_query_argument('ref', None) 140 ref = self.get_query_argument('ref', None)
140 image = self.get_query_argument('image', None) 141 image = self.get_query_argument('image', None)
  142 + content_type, encoding = mimetypes.guess_type(image)
141 143
142 if uid != '0': 144 if uid != '0':
143 t = self.testapp.get_student_test(uid) 145 t = self.testapp.get_student_test(uid)
@@ -149,6 +151,7 @@ class FileHandler(BaseHandler): @@ -149,6 +151,7 @@ class FileHandler(BaseHandler):
149 raise tornado.web.HTTPError(404) # Not Found 151 raise tornado.web.HTTPError(404) # Not Found
150 152
151 for q in t['questions']: 153 for q in t['questions']:
  154 + # search for the question that contains the image
152 if q['ref'] == ref: 155 if q['ref'] == ref:
153 filepath = path.join(q['path'], 'public', image) 156 filepath = path.join(q['path'], 'public', image)
154 157
@@ -159,11 +162,11 @@ class FileHandler(BaseHandler): @@ -159,11 +162,11 @@ class FileHandler(BaseHandler):
159 except PermissionError: 162 except PermissionError:
160 logging.error(f'No permission: {filepath}') 163 logging.error(f'No permission: {filepath}')
161 else: 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,6 +419,8 @@ def main():
416 except: 419 except:
417 print('An error ocurred while setting up the logging system.') 420 print('An error ocurred while setting up the logging system.')
418 sys.exit(1) 421 sys.exit(1)
  422 +
  423 + # start logging
419 logging.info('===============================================') 424 logging.info('===============================================')
420 425
421 # --- start application 426 # --- start application
@@ -44,21 +44,21 @@ class TestFactory(dict): @@ -44,21 +44,21 @@ class TestFactory(dict):
44 self.question_factory.load_files(files=self['files'], questions_dir=self['questions_dir']) 44 self.question_factory.load_files(files=self['files'], questions_dir=self['questions_dir'])
45 45
46 # check if all questions exist ('ref' keys are correct?) 46 # check if all questions exist ('ref' keys are correct?)
47 - errors_found = False 47 + errors_found = []
48 for q in self['questions']: 48 for q in self['questions']:
49 for r in q['ref']: 49 for r in q['ref']:
50 - logger.info(f'Checking question "{r}".') 50 + logger.info(f' Checking "{r}".')
51 try: 51 try:
52 self.question_factory.generate(r) 52 self.question_factory.generate(r)
53 except: 53 except:
54 logger.critical(f'Can\'t generate question "{r}".') 54 logger.critical(f'Can\'t generate question "{r}".')
55 - errors_found = True 55 + errors_found.append(r)
56 56
57 if errors_found: 57 if errors_found:
58 - logger.critical('Errors found while generating questions.') 58 + logger.critical(f'{errors_found} errors found generating questions.')
59 raise TestFactoryException() 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 # -----------------------------------------------------------------------