Commit d4fa55fc2eb09dffa1498a46c2897bd7c6f6915d
1 parent
32311fb1
Exists in
master
and in
1 other branch
login and correction are now async
Showing
1 changed file
with
17 additions
and
12 deletions
Show diff stats
serve.py
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | |
3 | -# base | |
3 | +# python standard library | |
4 | 4 | from os import path |
5 | 5 | import sys |
6 | -import argparse | |
7 | -import logging.config | |
8 | -import json | |
9 | 6 | import base64 |
10 | 7 | import uuid |
8 | +import logging.config | |
9 | +import argparse | |
11 | 10 | import mimetypes |
12 | 11 | import signal |
12 | +import asyncio | |
13 | +import json | |
13 | 14 | |
14 | -# packages | |
15 | +# user installed libraries | |
15 | 16 | import tornado.ioloop |
16 | 17 | import tornado.web |
17 | 18 | import tornado.httpserver |
18 | 19 | from tornado import template, gen |
19 | 20 | |
20 | -# project | |
21 | -from tools import load_yaml, md_to_html | |
21 | +# this project | |
22 | 22 | from app import App, AppException |
23 | +from tools import load_yaml, md_to_html | |
23 | 24 | |
24 | 25 | |
25 | 26 | class WebApplication(tornado.web.Application): |
... | ... | @@ -69,14 +70,16 @@ class LoginHandler(BaseHandler): |
69 | 70 | def get(self): |
70 | 71 | self.render('login.html', error='') |
71 | 72 | |
72 | - # @gen.coroutine | |
73 | - def post(self): | |
73 | + async def post(self): | |
74 | 74 | uid = self.get_body_argument('uid') |
75 | 75 | if uid.startswith('l'): # remove prefix 'l' |
76 | 76 | uid = uid[1:] |
77 | 77 | pw = self.get_body_argument('pw') |
78 | 78 | |
79 | - if self.testapp.login(uid, pw): | |
79 | + loop = asyncio.get_event_loop() | |
80 | + login_ok = await loop.run_in_executor(None, self.testapp.login, uid, pw) | |
81 | + | |
82 | + if login_ok: | |
80 | 83 | self.set_secure_cookie("user", str(uid), expires_days=30) |
81 | 84 | self.redirect(self.get_argument("next", "/")) |
82 | 85 | else: |
... | ... | @@ -206,7 +209,7 @@ class TestHandler(BaseHandler): |
206 | 209 | |
207 | 210 | # POST |
208 | 211 | @tornado.web.authenticated |
209 | - def post(self): | |
212 | + async def post(self): | |
210 | 213 | uid = self.current_user |
211 | 214 | |
212 | 215 | # self.request.arguments = {'answered-0': [b'on'], '0': [b'13.45']} |
... | ... | @@ -229,7 +232,9 @@ class TestHandler(BaseHandler): |
229 | 232 | 'numeric-interval'): |
230 | 233 | ans[i] = ans[i][0] |
231 | 234 | |
232 | - self.testapp.correct_test(uid, ans) | |
235 | + loop = asyncio.get_event_loop() | |
236 | + await loop.run_in_executor(None, self.testapp.correct_test, uid, ans) | |
237 | + | |
233 | 238 | self.testapp.logout(uid) |
234 | 239 | self.clear_cookie('user') |
235 | 240 | ... | ... |