-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost.py
executable file
·354 lines (297 loc) · 11.6 KB
/
host.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python3
"""
A weirdly categorized book app
type ./host.py to run the app
categories and books have json endpoints at /JSON
(for eg:
"http://localhost:5000/category/List%20of%20autobiographies/JSON")
"""
from flask import (Flask,
render_template,
jsonify,
request,
redirect,
url_for,
abort,
session as login_session,
make_response)
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from databases.database_setup import Base, Category, Item, User
from flask_httpauth import HTTPBasicAuth
from functools import wraps
from oauth2client.client import flow_from_clientsecrets
from oauth2client.client import FlowExchangeError
import requests
import random
import string
import json
import httplib2
auth = HTTPBasicAuth()
app = Flask(__name__)
engine = create_engine('sqlite:///databases/all_info.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
def login_required(func):
"""checks if the user logged in or not !,
allows function to execute if user has,
else does not"""
@wraps(func)
def decorator(*args, **kwargs):
if check_logged_in():
return func(*args, **kwargs)
else:
abort(401)
return redired(url_for('main'))
return decorator
@app.errorhandler(401)
def unauthorized_access(error):
return make_response('you need to be logged in to use this feature !', 401)
def check_logged_in():
"""checks if user has logged in, returns True if logged in,
False if not"""
return 'username' in login_session
@app.route("/")
def main():
session = DBSession()
categories = session.query(Category).all()
books = session.query(Item).limit(7).all()
_books = []
for book in books:
category = session.query(Category).filter_by(id=book.category_id).one()
_books.append([category, book])
books = _books
session.close()
username = login_session.get('username', None)
return render_template("main.html",
categories=categories,
books=books,
username=username)
@app.route('/login')
def login_user():
return render_template('login_user.html')
@app.route('/logout')
def logout_user():
login_session.clear()
return redirect(url_for('login_user'))
@app.route('/login/<provider>', methods=['POST'])
def login_process(provider):
if provider == 'google':
token = request.data
return authorize_google(token)
return abort(404)
def authorize_google(auth_code):
"""authorize google sign in"""
try:
# Upgrade the authorization code into a credentials object
oauth_flow = flow_from_clientsecrets('client_secrets.json', scope='')
oauth_flow.redirect_uri = 'postmessage'
credentials = oauth_flow.step2_exchange(auth_code)
except FlowExchangeError as ex:
response = make_response(
json.dumps('Failed to upgrade the authorization code.'), 401)
response.headers['Content-Type'] = 'application/json'
return response
# Check that the access token is valid.
access_token = credentials.access_token
url = (
'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=%s' %
access_token)
h = httplib2.Http()
result = json.loads(h.request(url, 'GET')[1])
# If there was an error in the access token info, abort.
if result.get('error') is not None:
response = make_response(json.dumps(result.get('error')), 500)
response.headers['Content-Type'] = 'application/json'
return response
# # Verify that the access token is used for the intended user.
# gplus_id = credentials.id_token['sub']
# if result['user_id'] != gplus_id:
# response = make_response(json.dumps("Token's user ID doesn't match given user ID."), 401)
# response.headers['Content-Type'] = 'application/json'
# return response
# # Verify that the access token is valid for this app.
# if result['issued_to'] != CLIENT_ID:
# response = make_response(json.dumps("Token's client ID does not match app's."), 401)
# response.headers['Content-Type'] = 'application/json'
# return response
# stored_credentials = login_session.get('credentials')
# stored_gplus_id = login_session.get('gplus_id')
# if stored_credentials is not None and gplus_id == stored_gplus_id:
# response = make_response(json.dumps('Current user is already connected.'), 200)
# response.headers['Content-Type'] = 'application/json'
# return response
# Get user info
h = httplib2.Http()
userinfo_url = "https://www.googleapis.com/oauth2/v1/userinfo"
params = {'access_token': credentials.access_token, 'alt': 'json'}
answer = requests.get(userinfo_url, params=params)
data = answer.json()
name = data['name']
picture = data['picture']
email = data['email']
# see if user exists, if it doesn't make a new one
session = DBSession()
user = session.query(User).filter_by(email=email).first()
if not user:
user = User(username=name, picture=picture, email=email)
session.add(user)
session.commit()
session.close()
login_session['email'] = user.email
login_session['username'] = user.username
login_session['user_id'] = user.id
return make_response('success', 200)
@app.route("/category/<category_name>")
def show_category(category_name):
session = DBSession()
category = session.query(Category).filter_by(name=category_name).one()
books = session.query(Item).filter_by(category_id=category.id).all()
session.close()
username = login_session.get('username', None)
return render_template("category.html",
category=category,
books=books,
username=username)
@app.route("/category/<category_name>/JSON")
def jsonify_category_items(category_name):
session = DBSession()
category = session.query(Category).filter_by(name=category_name).one()
books = session.query(Item).filter_by(category_id=category.id).all()
session.close()
serialized_books = [book.serialize for book in books]
items = dict(category=category.serialize, books=serialized_books)
return jsonify(items)
@app.route("/category/<category_name>/items/<book_name>")
def show_book(category_name, book_name):
session = DBSession()
category = session.query(Category).filter_by(name=category_name).one()
book = session.query(Item).filter_by(category_id=category.id).filter_by(name=book_name).one()
book_user_id = book.user_id
session.close()
username = login_session.get('username')
if username:
modifyable_by_user = (book_user_id == login_session['user_id'])
else:
modifyable_by_user = None
return render_template("book.html",
book=book,
username=username,
modifyable_by_user=modifyable_by_user,
category=category)
@app.route("/category/<category_name>/items/<book_name>/JSON")
def jsonify_book(category_name, book_name):
session = DBSession()
category = session.query(Category).filter_by(name=category_name).one()
book = session.query(Item).filter_by(category_id=category.id).filter_by(name=book_name).one()
book_info = book.serialize
session.close()
json_contents = jsonify(dict(category=category.name, book=book_info))
return json_contents
@app.route('/new', methods=['GET', 'POST'])
@login_required
def add_new_book():
if request.method == 'POST':
session = DBSession()
name = request.form['name']
description = request.form['description']
category_id = request.form['category_id']
category = session.query(Category).filter_by(id=category_id).one()
category_name = category.name
user_id = login_session['user_id']
new_book = Item(name=name,
description=description,
category_id=category.id,
user_id=user_id)
session.add(new_book)
session.commit()
session.close()
return redirect(url_for('show_category',
category_name=category_name))
username = login_session['username']
session = DBSession()
categories = session.query(Category).all()
session.close()
return render_template("new_book.html",
username=username,
categories=categories)
@app.route(
'/category/<category_name>/items/<book_name>/edit',
methods=[
'GET',
'POST'])
@login_required
def edit_book(category_name, book_name):
if request.method == 'POST':
session = DBSession()
category = session.query(Category).filter_by(name=category_name).one()
book = (session.query(Item)
.filter_by(category_id=category.id)
.filter_by(name=book_name).one())
userid = login_session['user_id']
editable = (userid == book.user_id)
if editable:
book.name = request.form['name']
book.description = request.form['description']
category_name = request.form['category_name']
category = session.query(Category).filter_by(
name=category_name).one()
book.category_id = category.id
session.add(book)
session.commit()
session.close()
return redirect(url_for('show_category',
category_name=category.name))
else:
session.close()
return abort(401)
session = DBSession()
book = session.query(Item).filter_by(id=book_id).one()
category = session.query(Category).filter_by(id=book.category_id).one()
categories = session.query(Category).all()
categories.remove(category)
session.close()
return render_template("edit_book.html",
book=book,
categories=categories,
category=category)
@app.route(
'/category/<category_name>/items/<book_name>/delete',
methods=[
'GET',
'POST'])
@login_required
def delete_book(category_name, book_name):
if request.method == 'POST':
session = DBSession()
category = session.query(Category).filter_by(name=category_name).one()
book = (session.query(Item)
.filter_by(category_id=category.id)
.filter_by(name=book_name)
.one())
session.delete(book)
session.commit()
session.close()
deletable_by_user = (login_session['user_id'] == book.user_id)
if deletable_by_user:
return redirect(url_for('show_category',
category_name=category_name))
else:
return abort(401)
session = DBSession()
category = session.query(Category).filter_by(name=category_name).one()
book = (session.query(Item)
.filter_by(category_id=category.id)
.filter_by(name=book_name)
.one())
session.close()
username = login_session['username']
return render_template("delete_book.html",
book=book,
username=username)
if __name__ == "__main__":
app.debug = True
secRandom = random.SystemRandom()
app.secret_key = ''.join(
[secRandom.choice(string.ascii_uppercase + string.digits)])
app.run(host="0.0.0.0")