-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
115 lines (98 loc) · 3.46 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import aiohttp
import asyncio
import async_timeout
import base64
import json
import os
import time
import sqlite3
import tornado.web
import tornado.httpserver
import tornado.ioloop
import tornado.escape
import tornado.options
import logging
import traceback
import pandas as pd
from time import gmtime, strftime
from hashlib import sha1
from jupyterhub.services.auth import HubAuthenticated
from lxml import etree
from oauthlib.oauth1.rfc5849 import signature, parameters
from sqlite3 import Error
from hash_dict import hash_sha256
prefix = os.environ.get('JUPYTERHUB_SERVICE_PREFIX', '/')
ERROR_FILE = "tornado_errors.csv"
ERROR_PATH = "tornadoerrors"
def create_table(db_file, create_table_sql):
""" create a table from the create_table_sql statement
:param conn: Connection object
:param create_table_sql: a CREATE TABLE statement
:return: conn: connection to db
"""
global conn
try:
conn = sqlite3.connect(db_file)
c = conn.cursor()
c.execute(create_table_sql)
print(sqlite3.version)
except Error as e:
print(e)
def write_info(grade_info, conn):
sql_cmd = """
INSERT INTO telemetry(user, question, answer, results, correct, assignment, section, timestamp)
VALUES (?,?,?,?,?,?,?,?)
"""
try:
# context manager here takes care of conn.commit()
with conn:
conn.execute(sql_cmd, grade_info)
conn.commit()
except Error as e:
print(e)
print("Error inserting into database for the following record")
print(grade_info)
class GoferHandler(HubAuthenticated, tornado.web.RequestHandler):
async def get(self):
print("Received request")
self.write("This is a post only page. You probably shouldn't be here!")
self.finish()
async def post(self):
"""Accept notebook submissions, saves, then grades them"""
user_model = self.get_current_user()
if user_model:
user_id = hash_sha256(user_model)
else:
user_id = 'nan' # if something goes wrong, we can detect errors
print("Received submission from %s" % user)
print(self.request.body.decode("utf-8"))
req_data = tornado.escape.json_decode(self.request.body)
question = req_data["question"]
answer = str(req_data["answer"])
results = str(req_data["results"])
correct = bool(req_data["correct"])
assignment = req_data["assignment"]
section=req_data["section"]
timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(time.time()))
# Let user know their submission was received
self.write("User submission has been received. Grade will be posted to the gradebook once it's finished running!")
self.finish()
write_info((user_id, question, answer, results, correct, assignment, section, timestamp), conn)
if __name__ == '__main__':
create_table_sql = """ CREATE TABLE IF NOT EXISTS telemetry (
user text,
question text NOT NULL,
answer text NOT NULL,
results text NOT NULL,
correct boolean NOT NULL,
assignment text NOT NULL,
section text NOT NULL,
timestamp text NOT NULL
); """
create_table("/share/telemetry.db", create_table_sql)
tornado.options.parse_command_line()
app = tornado.web.Application([(r"/", GoferHandler)])
port = 10101
app.listen(port)
print("listening on port {}".format(port))
tornado.ioloop.IOLoop.current().start()