-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest_api.py
173 lines (153 loc) · 6.62 KB
/
rest_api.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import logging
import hashlib
from flask import jsonify, make_response, request, redirect, session
from flask_restful import Resource, reqparse
from docker_verify import docker_verify
from models import Answer, User, db
from task_manager import TaskManager
import os
# Challenge data file URL
CHALLENGES_PATH = 'data/challenges.json'
# Static variables
manager = TaskManager(CHALLENGES_PATH)
best_answer = [{
'cc':float('inf'),
'java':float('inf'),
'js':float('inf'),
'py':float('inf')} for _ in manager.get_tasks()]
tokens = {}
# REST API Endpoints
class AnswerResource(Resource):
def post(self, task_id):
''' Endpoint for submitting code '''
parser = reqparse.RequestParser()
parser.add_argument('language', required=True)
parser.add_argument('code', required=True)
args = parser.parse_args()
task = manager.get_task(task_id)
if task_id is None or task is None: # no task exists with the stated id
return redirect('/?message=%s' % 'task does not exist.')
test_cases = task.get('test_cases', None)
# get user id
if 'token' not in session:
try:
session.pop('username')
except KeyError: pass
return redirect('/?message=%s' % 'no auth token supplied.')
if session['token'] not in tokens:
try:
session.pop('username')
except KeyError: pass
session.pop('token')
return redirect('/?message=%s' % 'invalid token supplied.')
user = db.session.query(User).filter(User.email==tokens[session['token']]).first()
# verify response using same input
result = docker_verify(args.code, args.language, test_cases)
if result is None:
return redirect('/?task=%d&message=%s' % (task_id, 'language is not supported'))
if all(result):
if(len(args.code) < best_answer[task_id][args.language]):
best_answer[task_id][args.language] = len(args.code)
max_points = int(task.get('points', None))
points = max_points * best_answer[task_id][args.language]/len(args.code)
# look for better answer from same user
prev_answer = db.session.query(Answer).filter((Answer.user_id==user.id) & (Answer.task_id==task_id) & (Answer.points>points)).first()
if prev_answer is None:
answer = Answer(
user_id=user.id,
task_id=task_id,
length=len(args.code),
points=points,
language=args.language
)
db.session.add(answer)
user.points = sum([answer.points for answer in user.answers])
db.session.commit()
return redirect('/?task=%d&message=%s' % (task_id, 'answer judged correct.'))
return redirect('/?task=%d&message=%s' % (task_id, 'answer judged incorrect.'))
class AnswerInfoResource(Resource):
def get(self, answer_id):
''' Endpoint for getting information about a particular answer '''
answer = Answer.query.filter_by(id=answer_id).first()
if not answer:
return make_response("Tried to query an invalid answer.", 400)
return jsonify(answer.to_dict())
class TaskInfoResource(Resource):
def get(self, task_id):
''' Endpoint for getting information about current state of task '''
order_queries = {
'latest': Answer.query
.filter_by(task_id=task_id)
.order_by(Answer.created_at.desc()),
'shortest': Answer.query.filter_by(task_id=task_id)
.order_by(Answer.length)
}
parser = reqparse.RequestParser()
parser.add_argument('order', location='args', default='latest')
args = parser.parse_args()
task = manager.get_task(task_id)
if not task:
return make_response("Tried to query an invalid task.", 400)
answers = [answer.to_dict()
for answer in order_queries[args.order].all()]
return jsonify({
'task': task,
'answers': answers
})
class TaskListResource(Resource):
def get(self):
'''Endpoint for getting a list of all available tasks'''
tasks = manager.get_tasks()
retval = []
for task in tasks:
retval.append({'name':task['name'],
'desc':task['desc'],
'points': task['points'],
'difficulty': task['difficulty']})
return jsonify(retval)
class LoginResource(Resource):
def post(self):
'''Endpoint for getting auth token for existing user'''
parser = reqparse.RequestParser()
parser.add_argument('email', required=True)
parser.add_argument('password', required=True)
args = parser.parse_args()
hash_obj = hashlib.sha256()
hash_obj.update(args.password.encode('utf-8'))
password_hash = hash_obj.hexdigest()
user = db.session.query(User).filter((User.email==args.email) & (User.password_hash==password_hash)).first()
if user is None:
return redirect('/?message=%s' % 'Could not find account.')
# allocate and maintain session token
token = os.urandom(256)
tokens[token] = args.email
session['token'] = token
session['username'] = user.username
return redirect('/')
class SignupResource(Resource):
def post(self):
'''Endpoint for registering and getting auth token for new user'''
parser = reqparse.RequestParser()
parser.add_argument('username', required=True)
parser.add_argument('email', required=True)
parser.add_argument('password', required=True)
args = parser.parse_args()
# check username and email uniqueness
duplicate = db.session.query(User).filter((User.username==args.username) | (User.email==args.email)).first()
if duplicate is not None:
return redirect('/?message=%s' % 'Account already exists for this email.')
hash_obj = hashlib.sha256()
hash_obj.update(args.password.encode('utf-8'))
user_obj = User(
username=args.username,
email=args.email,
password_hash=hash_obj.hexdigest()
)
db.session.add(user_obj)
db.session.commit()
# allocate and maintain session token
token = os.urandom(256)
tokens[token] = args.email
session['token'] = token
session['username'] = args.username
return redirect('/')