-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollaborate.py
216 lines (175 loc) · 6.57 KB
/
collaborate.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from __future__ import (
print_function,
absolute_import,
)
from datetime import timedelta
import json
from flask import (
Flask,
Response,
abort,
current_app,
jsonify,
make_response,
request
)
from functools import wraps, update_wrapper
from sqlalchemy.sql.expression import or_
import requests
import urllib
from dbhelper import db
from models import (
User,
Course,
Offering,
Lecturer,
Rating,
)
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///collaborate.db'
app.config['DEBUG'] = True
db.init_app(app)
# decorator for checking access token
def needs_auth(f):
@wraps(f)
def decorator(*args, **kwargs):
try:
token = request.headers.get('X-Access-Token')
if not token:
abort(401)
r = requests.get('https://graph.facebook.com/me?access_token=%s' % urllib.quote(token))
j = r.json()
name = j['name']
fb_id = j['id']
r = requests.get('https://graph.facebook.com/me/picture?redirect=false&access_token=%s' %
urllib.quote(token))
j = r.json()
pic = j['data']['url']
except:
abort(401)
# check if user object already exists
user = User.query.filter_by(fb_id=fb_id).first()
if not user:
# create the user
user = User(name=name, fb_id=fb_id, pic=pic)
user.name = name
user.pic = pic
db.session.add(user)
db.session.commit()
return f(user, *args, **kwargs)
return decorator
# thanks to http://flask.pocoo.org/snippets/56/
def crossdomain(origin=None, methods=None, headers=None,
max_age=21600, attach_to_all=True,
automatic_options=True):
if methods is not None:
methods = ', '.join(sorted(x.upper() for x in methods))
if headers is not None and not isinstance(headers, basestring): # noqa
headers = ', '.join(x.upper() for x in headers)
if not isinstance(origin, basestring): # noqa
origin = ', '.join(origin)
if isinstance(max_age, timedelta):
max_age = max_age.total_seconds()
def get_methods():
if methods is not None:
return methods
options_resp = current_app.make_default_options_response()
return options_resp.headers['allow']
def decorator(f):
def wrapped_function(*args, **kwargs):
if automatic_options and request.method == 'OPTIONS':
resp = current_app.make_default_options_response()
else:
resp = make_response(f(*args, **kwargs))
if not attach_to_all and request.method != 'OPTIONS':
return resp
h = resp.headers
h['Access-Control-Allow-Origin'] = origin
h['Access-Control-Allow-Methods'] = get_methods()
h['Access-Control-Max-Age'] = str(max_age)
#if headers is not None:
# h['Access-Control-Allow-Headers'] = headers
h['Access-Control-Allow-Headers'] = 'X-Access-Token, Content-Type'
return resp
f.provide_automatic_options = False
return update_wrapper(wrapped_function, f)
return decorator
@app.route("/offerings/<offering_id>/ratings", methods=['POST', 'OPTIONS'])
@crossdomain(origin='*')
@needs_auth
def post_rating(user, offering_id):
try:
rating_json = json.loads(request.data)
except:
return jsonify(error="invalid json")
if not rating_json:
return jsonify(error="no rating given")
offering = Offering.query.filter_by(id=offering_id).first()
if offering is None:
return jsonify(error="offering not found")
existing_rating = Rating.query.filter_by(user_id=user.id,
offering_id=offering_id).first()
if existing_rating is not None:
return jsonify(error="rating already exists")
rating_json['user_id'] = user.id
rating_json['offering_id'] = offering_id
new_rating = Rating.from_json(rating_json)
db.session.add(new_rating)
db.session.commit()
return jsonify()
@app.route("/offerings/<offering_id>", methods=['GET'])
@crossdomain(origin='*')
def get_offering_info(offering_id):
offering = Offering.query.filter_by(id=offering_id).first()
if offering is None:
return jsonify(error="offering not found")
offering_json = offering.to_JSON()
offering_json['ratings'] = [rating.to_JSON(include_offering=False)
for rating in offering.ratings]
offering_json['aggregated_ratings'] = offering.get_aggregate_ratings(detailed=True)
return jsonify(**offering_json)
@app.route("/courses/<course_id>", methods=['GET'])
@crossdomain(origin='*')
def get_course_info(course_id):
course = Course.query.filter_by(id=course_id).first()
if course is None:
return jsonify(error="course not found")
course_json = course.to_JSON()
course_json['offerings'] = [offering.to_JSON(include_course=False) for offering in course.offerings]
return jsonify(**course_json)
@app.route('/courses', methods=['GET'])
@crossdomain(origin='*')
def search_courses():
search_query = '%' + request.args.get('q', '') + '%'
courses = set(Course.query.filter(or_(Course.title.like(search_query),
Course.code.like(search_query))).all())
lecturers = Lecturer.query.filter(Lecturer.name.like(search_query)).all()
courses.update(offering.course for lecturer in lecturers for offering in lecturer.offerings)
resp = Response(json.dumps(list(courses), default=lambda o: o.to_JSON()))
resp.headers['Content-Type'] = 'application/json'
return resp
@app.route("/ratings/recent", methods=['GET'])
@crossdomain(origin='*')
def recent_ratings():
try:
count = int(request.args.get('count', 5))
except:
return jsonify(error="count needs to be a positive number")
if count < 0:
return jsonify(error="count needs to be a positive number")
ratings = Rating.query.order_by(Rating.id.desc()).limit(count).all()
resp = Response(json.dumps(ratings, default=lambda o: o.to_JSON()))
resp.headers['Content-Type'] = 'application/json'
return resp
# test endpoints, please ignore
@app.route("/get_gentrified", methods=['GET'])
@crossdomain(origin='*')
def get_gentrified():
return jsonify(memes="spicy")
@app.route("/verify_token", methods=['GET'])
@crossdomain(origin='*')
@needs_auth
def verify_token(user):
return "well meme'd, {}".format(user.name)
if __name__ == '__main__':
app.run()