Commit e6915ba05f9c4729557ff39bb12f8c476312ccc8

Authored by Miguel Barão
1 parent 51627bcc
Exists in master and in 1 other branch dev

- code cleanup.

- fixed info questions to continue with a single button click.
Showing 3 changed files with 36 additions and 26 deletions   Show diff stats
BUGS.md
1 1  
2 2 # BUGS
3 3  
4   -- falha no file handler de vez em quando, nao sei porquê...
  4 +- falha intermitent no file handler quando o browser envia 2 GET requests ao mesmo tempo (porquê?)
5 5 - nos topicos learn.yaml, qd falha acrescenta no fim. nao faz sentido.
6 6 - ocorreu uma vez o sqlalchemy dar mesg erro a indicar que as threads sao diferents quando se faz o get da primeira pergunta do topico. Muitas vezes nao mostar erro, mas a pagina da erro ou fica em branco...
7 7  
... ...
serve.py
... ... @@ -16,6 +16,7 @@ import functools
16 16 import tornado.ioloop
17 17 import tornado.web
18 18 import tornado.httpserver
  19 +from tornado.escape import to_unicode
19 20  
20 21 # this project
21 22 from learnapp import LearnApp
... ... @@ -122,9 +123,9 @@ class ChangePasswordHandler(BaseHandler):
122 123  
123 124 changed_ok = await self.learn.change_password(uid, pw)
124 125 if changed_ok:
125   - notification = tornado.escape.to_unicode(self.render_string('notification.html', type='success', msg='A password foi alterada!'))
  126 + notification = to_unicode(self.render_string('notification.html', type='success', msg='A password foi alterada!'))
126 127 else:
127   - notification = tornado.escape.to_unicode(self.render_string('notification.html', type='danger', msg='A password não foi alterada!'))
  128 + notification = to_unicode(self.render_string('notification.html', type='danger', msg='A password não foi alterada!'))
128 129 self.write({'msg': notification})
129 130  
130 131  
... ... @@ -141,7 +142,6 @@ class RootHandler(BaseHandler):
141 142 name=self.learn.get_student_name(uid),
142 143 state=self.learn.get_student_state(uid),
143 144 title=self.learn.get_title(),
144   - # get_topic_type=self.learn.get_topic_type, # function
145 145 )
146 146  
147 147  
... ... @@ -172,12 +172,13 @@ class TopicHandler(BaseHandler):
172 172 # ----------------------------------------------------------------------------
173 173  
174 174 # FIXME error in many situations... images are not shown...
175   -
  175 +# seems to happen when the browser sends two GET requests at the same time
176 176 class FileHandler(BaseHandler):
177 177 SUPPORTED_METHODS = ['GET']
178 178  
179 179 @tornado.web.authenticated
180   - async def get(self, filename):
  180 + # async
  181 + def get(self, filename):
181 182 uid = self.current_user
182 183 public_dir = self.learn.get_current_public_dir(uid)
183 184 filepath = path.expanduser(path.join(public_dir, filename))
... ... @@ -191,12 +192,13 @@ class FileHandler(BaseHandler):
191 192 else:
192 193 content_type = mimetypes.guess_type(filename)
193 194 self.set_header("Content-Type", content_type[0])
194   -
195 195 # divide the file into chunks and write one chunk at a time, so
196 196 # that the write does not block the ioloop for very long.
197 197 with f:
198   - self.write(f.read())
199   - await self.flush()
  198 + data = f.read()
  199 + self.write(data)
  200 + # await self.flush()
  201 + self.flush()
200 202  
201 203  
202 204 # ----------------------------------------------------------------------------
... ... @@ -226,24 +228,24 @@ class QuestionHandler(BaseHandler):
226 228 q = self.learn.get_current_question(user)
227 229  
228 230 if q is not None:
229   - question_html = self.render_string(self.templates[q['type']],
230   - question=q, md=md_to_html)
  231 + question_html = to_unicode(self.render_string(self.templates[q['type']],
  232 + question=q, md=md_to_html))
231 233 response = {
232 234 'method': 'new_question',
233 235 'params': {
234 236 'type': q['type'],
235   - 'question': tornado.escape.to_unicode(question_html),
  237 + 'question': question_html,
236 238 'progress': self.learn.get_student_progress(user),
237 239 'tries': q['tries'],
238   - },
  240 + }
239 241 }
240 242  
241 243 else:
242   - finished_topic_html = self.render_string('finished_topic.html')
  244 + finished_topic_html = to_unicode(self.render_string('finished_topic.html'))
243 245 response = {
244 246 'method': 'finished_topic',
245 247 'params': {
246   - 'question': tornado.escape.to_unicode(finished_topic_html)
  248 + 'question': finished_topic_html
247 249 }
248 250 }
249 251  
... ... @@ -275,8 +277,9 @@ class QuestionHandler(BaseHandler):
275 277 comments=q['comments'], md=md_to_html)
276 278  
277 279 response['params'] = {
  280 + 'type': q['type'],
278 281 'progress': self.learn.get_student_progress(user),
279   - 'comments': tornado.escape.to_unicode(comments_html),
  282 + 'comments': to_unicode(comments_html),
280 283 'tries': q['tries'],
281 284 }
282 285  
... ... @@ -285,21 +288,23 @@ class QuestionHandler(BaseHandler):
285 288 comments=q['comments'], md=md_to_html)
286 289  
287 290 response['params'] = {
  291 + 'type': q['type'],
288 292 'progress': self.learn.get_student_progress(user),
289   - 'comments': tornado.escape.to_unicode(comments_html), # FIXME
  293 + 'comments': to_unicode(comments_html),
290 294 'tries': q['tries'],
291 295 }
292 296  
293 297 elif action == 'wrong': # no more tries
294   - comments_html = self.render_string('comments.html',
295   - comments=q['comments'], md=md_to_html)
296   - solution_html = self.render_string('solution.html',
297   - solution=q['solution'], md=md_to_html)
  298 + comments_html = to_unicode(self.render_string('comments.html',
  299 + comments=q['comments'], md=md_to_html))
  300 + solution_html = to_unicode(self.render_string('solution.html',
  301 + solution=q['solution'], md=md_to_html))
298 302  
299 303 response['params'] = {
  304 + 'type': q['type'],
300 305 'progress': self.learn.get_student_progress(user),
301   - 'comments': tornado.escape.to_unicode(comments_html),
302   - 'solution': tornado.escape.to_unicode(solution_html),
  306 + 'comments': comments_html,
  307 + 'solution': solution_html,
303 308 'tries': q['tries'],
304 309 }
305 310  
... ...
static/js/topic.js
... ... @@ -26,7 +26,6 @@ function updateQuestion(response) {
26 26  
27 27 switch (method) {
28 28 case "new_question":
29   - console.log(params["type"]);
30 29 new_question(params["type"], params["question"], params["tries"], params["progress"]);
31 30 break;
32 31 case "finished_topic":
... ... @@ -52,8 +51,8 @@ function new_question(type, question, tries, progress) {
52 51 }
53 52 $("#submit").off();
54 53 $("#submit").click(postAnswer);
55   -
56 54 $("#tries").html(tries);
  55 +
57 56 $('#topic_progress').css('width', (100*progress)+'%').attr('aria-valuenow', 100*progress);
58 57 MathJax.Hub.Queue(["Typeset",MathJax.Hub,"question_div"]);
59 58  
... ... @@ -90,11 +89,17 @@ function getFeedback(response) {
90 89 var method = response["method"];
91 90 var params = response["params"];
92 91  
  92 + if (params['type'] == "info") {
  93 + getQuestion();
  94 + return;
  95 + }
  96 +
93 97 switch (method) {
94 98 case "right":
95 99 $('#comments').html(params['comments']);
96 100 MathJax.Hub.Queue(["Typeset", MathJax.Hub, "#comments"]);
97 101 $("#submit").html("Continuar");
  102 + // $("#submit").toggleClass("btn-info btn-primary");
98 103 $("#submit").off();
99 104 $("#submit").click(getQuestion);
100 105 break;
... ... @@ -123,7 +128,7 @@ function getFeedback(response) {
123 128 }
124 129 }
125 130  
126   -
  131 +// ===========================================================================
127 132 $(document).ready(function() {
128 133 getQuestion();
129 134 $("#submit").click(postAnswer);
... ...