-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathviews.py
90 lines (74 loc) · 2.46 KB
/
views.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
import errors
from flask import json
from flask import request as fr
import flask.views as fv
import uuid
class ServerInfo(fv.MethodView):
info = {
'application': {
'name': 'jiminy-recommender',
'version': '0.0.0'
}
}
def get(self):
return json.jsonify(self.info)
class PredictionsRatings(fv.MethodView):
def __init__(self, storage, request_q):
self.storage = storage
self.request_q = request_q
def post(self):
data = json.loads(fr.data)
r_dict = {
'user': data['user'],
'id': uuid.uuid4().hex,
'products': [
{'id': p_id, 'rating': 0.0} for p_id in data['products']
]
}
try:
self.storage.store(r_dict)
self.request_q.put(r_dict)
resp = json.jsonify(prediction=r_dict)
resp.status = '201'
resp.headers.add('Location',
'/predictions/ratings/{}'.format(r_dict['id']))
except errors.PredictionExists:
resp = errors.single_error_response(
'500', 'Server Error',
'A prediction with that ID already exists')
return resp
class PredictionsRanks(fv.MethodView):
def __init__(self, storage, request_q):
self.storage = storage
self.request_q = request_q
def post(self):
data = json.loads(fr.data)
r_dict = {
'user': data['user'],
'id': uuid.uuid4().hex,
'topk': data['topk'],
'products': []
}
try:
self.storage.store(r_dict)
self.request_q.put(r_dict)
resp = json.jsonify(prediction=r_dict)
resp.status = '201'
resp.headers.add('Location',
'/predictions/ranks/{}'.format(r_dict['id']))
except errors.PredictionExists:
resp = errors.single_error_response(
'500', 'Server Error',
'A prediction with that ID already exists')
return resp
class PredictionDetail(fv.MethodView):
def __init__(self, storage):
self.storage = storage
def get(self, p_id):
try:
data = self.storage.get(p_id)
resp = json.jsonify(data)
except errors.PredictionNotFound:
resp = errors.single_error_response(
'400', 'Not Found', 'That prediction does not exist')
return resp