Commit df6c6afa866d530a287c8de750dc1c6042ff17c6

Authored by Miguel Barao
1 parent fcdedf5a
Exists in master and in 1 other branch dev

- serves files in /public

BUGS.md
1 1 BUGS:
2 2  
3   -- servir ficheiros de public temporariamente
  3 +- load/save the knowledge state of the student
4 4 - se students.db não existe, rebenta.
5 5 - database hardcoded in LearnApp.
6 6 - implementar xsrf. Ver [http://www.tornadoweb.org/en/stable/guide/security.html#cross-site-request-forgery-protection]()
... ... @@ -14,6 +14,7 @@ TODO:
14 14  
15 15 SOLVED:
16 16  
  17 +- servir ficheiros de public temporariamente
17 18 - path dos generators scripts mal construido
18 19 - questions hardcoded in LearnApp.
19 20 - Factory para cada pergunta individual em vez de pool
... ...
app.py
... ... @@ -74,6 +74,12 @@ class LearnApp(object):
74 74 return self.online[uid].get('name', '')
75 75  
76 76 # ------------------------------------------------------------------------
  77 + def get_current_public_dir(self, uid):
  78 + topic = self.online[uid]['state'].topic
  79 + p = self.depgraph.graph['path']
  80 + return path.join(p, topic, 'public')
  81 +
  82 + # ------------------------------------------------------------------------
77 83 # check answer and if correct returns new question, otherise returns None
78 84 def check_answer(self, uid, answer):
79 85 logger.debug(f'check_answer("{uid}", "{answer}")')
... ...
knowledge.py
... ... @@ -21,7 +21,8 @@ class Knowledge(object):
21 21 self.state = state # {node: level, node: level, ...}
22 22 self.current_question = None
23 23  
24   - # self.seq = nx.topological_sort(self.depgraph)
  24 + self.seq = nx.topological_sort(self.depgraph)
  25 + self.topic = None
25 26  
26 27 def get_current_question(self):
27 28 return self.current_question
... ... @@ -36,10 +37,10 @@ class Knowledge(object):
36 37 g = self.depgraph
37 38 # choose topic, ie, node of the graph
38 39 # print(g.nodes())
39   - topic = random.choice(g.nodes()) # FIXME
  40 + self.topic = random.choice(g.nodes()) # FIXME
40 41 # print(topic)
41 42 # choose question from that topic
42   - question = random.choice(g.node[topic]['factory'])
  43 + question = random.choice(g.node[self.topic]['factory'])
43 44 # print(question)
44 45  
45 46 self.current_question = question.generate()
... ...
serve.py
... ... @@ -34,10 +34,11 @@ from tools import load_yaml, md
34 34 class WebApplication(tornado.web.Application):
35 35 def __init__(self):
36 36 handlers = [
37   - (r'/', LearnHandler),
38 37 (r'/login', LoginHandler),
39 38 (r'/logout', LogoutHandler),
40 39 (r'/question', QuestionHandler),
  40 + (r'/', LearnHandler),
  41 + (r'/(.+)', FileHandler),
41 42 ]
42 43 settings = {
43 44 'template_path': os.path.join(os.path.dirname(__file__), 'templates'),
... ... @@ -112,6 +113,18 @@ class LearnHandler(BaseHandler):
112 113 )
113 114  
114 115 # ----------------------------------------------------------------------------
  116 +class FileHandler(BaseHandler):
  117 + @tornado.web.authenticated
  118 + def get(self, filename):
  119 + uid = self.current_user
  120 + public_dir = self.learn.get_current_public_dir(uid)
  121 + try:
  122 + with open(os.path.join(public_dir, filename), 'rb') as f:
  123 + self.write(f.read())
  124 + except FileNotFoundError:
  125 + raise tornado.web.HTTPError(404)
  126 +
  127 +# ----------------------------------------------------------------------------
115 128 # respond to AJAX to get a JSON question
116 129 class QuestionHandler(BaseHandler):
117 130 templates = {
... ...
static/css/learn.css
1   -body {
2   - padding-top: 65px;
  1 +body {
  2 + padding-top: 65px;
  3 +}
  4 +
  5 +img {
  6 + display: block;
  7 + margin: auto;
  8 + width: 80%;
3 9 }
4 10 \ No newline at end of file
... ...