Commit 52db24bdd0a24c472877da4d26a809b54a3ae2bc
Remove older script of swtstore implementation
- swtr.py 427 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- swtstore/swtr.py 427 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| | | | 1 | # -*- coding: utf-8 -*- | | # -*- coding: utf-8 -*- |
---|
2 | """ | | """ |
---|
3 | swtr | | swtr |
---|
4 | ~~~~~~ | | ~~~~~~ |
---|
5 | | | |
---|
6 | http://swtr.us | | http://swtr.us |
---|
7 | | | |
---|
8 | :license: BSD, see LICENSE for more details. | | :license: BSD, see LICENSE for more details. |
---|
9 | """ | | """ |
---|
10 | from __future__ import with_statement | | from __future__ import with_statement |
---|
11 | from pymongo import Connection | | from pymongo import Connection |
---|
12 | from bson.objectid import ObjectId | | from bson.objectid import ObjectId |
---|
13 | from bson.errors import InvalidId | | from bson.errors import InvalidId |
---|
14 | from flask import Flask, request, session, g, redirect, url_for, abort, \ | | from flask import Flask, request, session, g, redirect, url_for, abort, \ |
---|
15 | render_template, flash, _app_ctx_stack, make_response, jsonify | | render_template, flash, _app_ctx_stack, make_response, jsonify |
---|
16 | from urllib import unquote_plus | | from urllib import unquote_plus |
---|
17 | import json | | import json |
---|
18 | import conf | | import conf |
---|
19 | import requests | | import requests |
---|
20 | | | |
---|
21 | # TODO: | | # TODO: |
---|
22 | # restify | | # restify |
---|
23 | # APIs as follows: | | # APIs as follows: |
---|
24 | # GET /sweets/q -> query sweets | | # GET /sweets/q -> query sweets |
---|
25 | # args: who, where, what, how | | # args: who, where, what, how |
---|
26 | # GET /sweets/<id> -> get specific sweet | | # GET /sweets/<id> -> get specific sweet |
---|
27 | # POST /sweets -> post sweets (one or a batch of) | | # POST /sweets -> post sweets (one or a batch of) |
---|
28 | # OPTIONS /sweets - > CORS policy .. understand it better | | # OPTIONS /sweets - > CORS policy .. understand it better |
---|
29 | # classes! | | # classes! |
---|
30 | # sqlAlchemy | | # sqlAlchemy |
---|
31 | # Postgres | | # Postgres |
---|
32 | # Persona, Auth in API endpoints | | # Persona, Auth in API endpoints |
---|
33 | | | |
---|
34 | # TODO: move this in a config file | | # TODO: move this in a config file |
---|
35 | # configuration | | # configuration |
---|
36 | | | |
---|
37 | DATABASE = 'sweets_production' | | DATABASE = 'sweets_production' |
---|
38 | COLLECTION_NAME = 'posts' | | COLLECTION_NAME = 'posts' |
---|
39 | DEBUG = True | | DEBUG = True |
---|
40 | SECRET_KEY = conf.SECRET_KEY | | SECRET_KEY = conf.SECRET_KEY |
---|
41 | USERNAME = 'admin' | | USERNAME = 'admin' |
---|
42 | PASSWORD = 'default' | | PASSWORD = 'default' |
---|
43 | DB_PORT = 27017 | | DB_PORT = 27017 |
---|
44 | DB_HOST = 'localhost' | | DB_HOST = 'localhost' |
---|
45 | URL = 'http://localhost:5001' | | URL = 'http://localhost:5001' |
---|
46 | MOZ_PERSONA_VERIFIER = 'https://verifier.login.persona.org/verify' | | MOZ_PERSONA_VERIFIER = 'https://verifier.login.persona.org/verify' |
---|
47 | MOZ_PERSONA_AUDIENCE = 'http://localhost:5000' | | MOZ_PERSONA_AUDIENCE = 'http://localhost:5000' |
---|
48 | | | |
---|
49 | appURL_map = {'img-anno': 'http://localhost:5000/?where=', | | appURL_map = {'img-anno': 'http://localhost:5000/?where=', |
---|
50 | 're-narration': 'http://y.a11y.in/web?foruri=', | | 're-narration': 'http://y.a11y.in/web?foruri=', |
---|
51 | 'idh-mowl': 'http://app.swtr.us/?where=', | | 'idh-mowl': 'http://app.swtr.us/?where=', |
---|
52 | 'testFromAPI': 'http://app.swtr.us/?where='} | | 'testFromAPI': 'http://app.swtr.us/?where='} |
---|
53 | | | |
---|
54 | # create our little application :) | | # create our little application :) |
---|
55 | # ^ ... It's going to be big now :P | | # ^ ... It's going to be big now :P |
---|
56 | app = Flask(__name__) | | app = Flask(__name__) |
---|
57 | app.config.from_object(__name__) | | app.config.from_object(__name__) |
---|
58 | app.config.from_envvar('FLASKR_SETTINGS', silent=True) | | app.config.from_envvar('FLASKR_SETTINGS', silent=True) |
---|
59 | | | |
---|
60 | # Jinja filters | | # Jinja filters |
---|
61 | app.jinja_env.filters['len'] = len | | app.jinja_env.filters['len'] = len |
---|
62 | | | |
---|
63 | | | |
---|
64 | def validateSweet(payload): | | def validateSweet(payload): |
---|
65 | for i in payload: | | for i in payload: |
---|
66 | try: | | try: |
---|
67 | if len(i['who']) and len(i['what']) and len(i['where']) and\ | | if len(i['who']) and len(i['what']) and len(i['where']) and\ |
---|
68 | len(i['how']) and len(i['created']): | | len(i['how']) and len(i['created']): |
---|
69 | pass | | pass |
---|
70 | else: | | else: |
---|
71 | return False | | return False |
---|
72 | except KeyError: | | except KeyError: |
---|
73 | return False | | return False |
---|
74 | return True | | return True |
---|
75 | | | |
---|
76 | | | |
---|
77 | def getUsers(): | | def getUsers(): |
---|
78 | db = g.connection[app.config['DATABASE']] | | db = g.connection[app.config['DATABASE']] |
---|
79 | coll = db['sweet_users'] | | coll = db['sweet_users'] |
---|
80 | users = [] | | users = [] |
---|
81 | for i in coll.find(): | | for i in coll.find(): |
---|
82 | users.append(i['user']) | | users.append(i['user']) |
---|
83 | return users | | return users |
---|
84 | | | |
---|
85 | def gatherStats(coll): | | def gatherStats(coll): |
---|
86 | stats = {} | | stats = {} |
---|
87 | stats['total_sweets'] = coll.count() | | stats['total_sweets'] = coll.count() |
---|
88 | return stats | | return stats |
---|
89 | | | |
---|
90 | @app.before_request | | @app.before_request |
---|
91 | def init_db(): | | def init_db(): |
---|
92 | g.connection = Connection(app.config['DB_HOST'], app.config['DB_PORT']) | | g.connection = Connection(app.config['DB_HOST'], app.config['DB_PORT']) |
---|
93 | db = g.connection[app.config['DATABASE']] | | db = g.connection[app.config['DATABASE']] |
---|
94 | g.collection = db[app.config["COLLECTION_NAME"]] | | g.collection = db[app.config["COLLECTION_NAME"]] |
---|
95 | g.stats = gatherStats(g.collection) | | g.stats = gatherStats(g.collection) |
---|
96 | | | |
---|
97 | | | |
---|
98 | @app.teardown_request | | @app.teardown_request |
---|
99 | def close_db(exception): | | def close_db(exception): |
---|
100 | g.connection.disconnect() | | g.connection.disconnect() |
---|
101 | | | |
---|
102 | | | |
---|
103 | @app.errorhandler(404) | | @app.errorhandler(404) |
---|
104 | def page_not_found(e): | | def page_not_found(e): |
---|
105 | return render_template('404.html'), 404 | | return render_template('404.html'), 404 |
---|
106 | | | |
---|
107 | | | |
---|
108 | @app.errorhandler(500) | | @app.errorhandler(500) |
---|
109 | def internal_error(e): | | def internal_error(e): |
---|
110 | return render_template('500.html'), 500 | | return render_template('500.html'), 500 |
---|
111 | | | |
---|
112 | @app.route('/') | | @app.route('/') |
---|
113 | def show_entries(): | | def show_entries(): |
---|
114 | res = g.collection.find().sort('_id',direction=-1).limit(100) | | res = g.collection.find().sort('_id',direction=-1).limit(100) |
---|
115 | entries = make_list(res) | | entries = make_list(res) |
---|
116 | return render_template('show_entries.html', entries=entries, | | return render_template('show_entries.html', entries=entries, |
---|
117 | appURL_map=appURL_map, stats=g.stats) | | appURL_map=appURL_map, stats=g.stats) |
---|
118 | | | |
---|
119 | | | |
---|
120 | # TODO: understand if we really need the OPTIONS | | # TODO: understand if we really need the OPTIONS |
---|
121 | @app.route('/sweets', methods=['POST', 'OPTIONS']) | | @app.route('/sweets', methods=['POST', 'OPTIONS']) |
---|
122 | @app.route('/add', methods=['POST', 'OPTIONS']) | | @app.route('/add', methods=['POST', 'OPTIONS']) |
---|
123 | def addSweets(): | | def addSweets(): |
---|
124 | print request.method | | print request.method |
---|
125 | | | |
---|
126 | if request.method == 'OPTIONS': | | if request.method == 'OPTIONS': |
---|
127 | response = make_response() | | response = make_response() |
---|
128 | response.status_code = 200 | | response.status_code = 200 |
---|
129 | response.headers['Access-Control-Allow-Origin'] =\ | | response.headers['Access-Control-Allow-Origin'] =\ |
---|
130 | 'http://localhost:5000' | | 'http://localhost:5000' |
---|
131 | response.headers['Access-Control-Max-Age'] = '20days' | | response.headers['Access-Control-Max-Age'] = '20days' |
---|
132 | response.headers['Access-Control-Allow-Headers'] = 'Origin,\ | | response.headers['Access-Control-Allow-Headers'] = 'Origin,\ |
---|
133 | X-Requested-With, Content-Type, Accept' | | X-Requested-With, Content-Type, Accept' |
---|
134 | return response | | return response |
---|
135 | | | |
---|
136 | response = make_response() | | response = make_response() |
---|
137 | response.headers['Access-Control-Allow-Origin'] = 'http://localhost:5000' | | response.headers['Access-Control-Allow-Origin'] = 'http://localhost:5000' |
---|
138 | response.headers['Access-Control-Allow-Headers'] = 'Origin,\ | | response.headers['Access-Control-Allow-Headers'] = 'Origin,\ |
---|
139 | X-Requested-With, Content-Type, Accept' | | X-Requested-With, Content-Type, Accept' |
---|
140 | data = {} | | data = {} |
---|
141 | data_list = [] | | data_list = [] |
---|
142 | | | |
---|
143 | if 'email' in session: | | if 'email' in session: |
---|
144 | print 'identifed user' | | print 'identifed user' |
---|
145 | print session['email'] | | print session['email'] |
---|
146 | else: | | else: |
---|
147 | print 'unidentified user' | | print 'unidentified user' |
---|
148 | | | |
---|
149 | # TODO: find a better way of handling reqeust sweets | | # TODO: find a better way of handling reqeust sweets |
---|
150 | try: | | try: |
---|
151 | payload = json.loads(request.form['data']) | | payload = json.loads(request.form['data']) |
---|
152 | except: | | except: |
---|
153 | try: | | try: |
---|
154 | payload = [{'who': request.form['who'], 'what': request.form['what'], | | payload = [{'who': request.form['who'], 'what': request.form['what'], |
---|
155 | 'where': request.form['where'], 'how': request.form['how']}] | | 'where': request.form['where'], 'how': request.form['how']}] |
---|
156 | except: | | except: |
---|
157 | try: | | try: |
---|
158 | payload = request.json | | payload = request.json |
---|
159 | except: | | except: |
---|
160 | payload = json.loads(request.data) | | payload = json.loads(request.data) |
---|
161 | | | |
---|
162 | | | |
---|
163 | valid = validateSweet(payload) | | valid = validateSweet(payload) |
---|
164 | if not valid: | | if not valid: |
---|
165 | response.status_code = 400 | | response.status_code = 400 |
---|
166 | response.data = "Bad or Malformed Request. Please check the validity\ | | response.data = "Bad or Malformed Request. Please check the validity\ |
---|
167 | of your request" | | of your request" |
---|
168 | return response | | return response |
---|
169 | print 'swt payload rcvd..' | | print 'swt payload rcvd..' |
---|
170 | print payload | | print payload |
---|
171 | for i in payload: | | for i in payload: |
---|
172 | data = i | | data = i |
---|
173 | id = g.collection.insert(i) | | id = g.collection.insert(i) |
---|
174 | data['permalink'] = app.config['URL'] + '/posts/' + str(ObjectId(id)) | | data['permalink'] = app.config['URL'] + '/posts/' + str(ObjectId(id)) |
---|
175 | data['id'] = str(ObjectId(id)) | | data['id'] = str(ObjectId(id)) |
---|
176 | del(data['_id']) | | del(data['_id']) |
---|
177 | print 'data', data | | print 'data', data |
---|
178 | data_list.append(data) | | data_list.append(data) |
---|
179 | response.data = json.dumps(data_list) | | response.data = json.dumps(data_list) |
---|
180 | print 'swt stored..' | | print 'swt stored..' |
---|
181 | return response | | return response |
---|
182 | | | |
---|
183 | | | |
---|
184 | @app.route('/login', methods=['GET', 'POST']) | | @app.route('/login', methods=['GET', 'POST']) |
---|
185 | def login(): | | def login(): |
---|
186 | error = None | | error = None |
---|
187 | if request.method == 'POST': | | if request.method == 'POST': |
---|
188 | if request.form['username'] != app.config['USERNAME']: | | if request.form['username'] != app.config['USERNAME']: |
---|
189 | error = 'Invalid username' | | error = 'Invalid username' |
---|
190 | elif request.form['password'] != app.config['PASSWORD']: | | elif request.form['password'] != app.config['PASSWORD']: |
---|
191 | error = 'Invalid password' | | error = 'Invalid password' |
---|
192 | else: | | else: |
---|
193 | session['logged_in'] = True | | session['logged_in'] = True |
---|
194 | flash('You were logged in') | | flash('You were logged in') |
---|
195 | return redirect(url_for('show_entries')) | | return redirect(url_for('show_entries')) |
---|
196 | return render_template('login.html', error=error) | | return render_template('login.html', error=error) |
---|
197 | | | |
---|
198 | | | |
---|
199 | @app.route('/sweets/q', methods=['GET']) | | @app.route('/sweets/q', methods=['GET']) |
---|
200 | def searchSweets(): | | def searchSweets(): |
---|
201 | response = make_response() | | response = make_response() |
---|
202 | response.status_code = 200 | | response.status_code = 200 |
---|
203 | response.headers['Access-Control-Allow-Origin'] = 'http://localhost:5000' | | response.headers['Access-Control-Allow-Origin'] = 'http://localhost:5000' |
---|
204 | response.headers['Access-Control-Max-Age'] = '20days' | | response.headers['Access-Control-Max-Age'] = '20days' |
---|
205 | response.headers['Access-Control-Allow-Headers'] = 'Origin,\ | | response.headers['Access-Control-Allow-Headers'] = 'Origin,\ |
---|
206 | X-Requested-With, Content-Type, Accept' | | X-Requested-With, Content-Type, Accept' |
---|
207 | | | |
---|
208 | args = request.args | | args = request.args |
---|
209 | | | |
---|
210 | if args is None: | | if args is None: |
---|
211 | reponse.status_code = 400 | | reponse.status_code = 400 |
---|
212 | return response | | return response |
---|
213 | | | |
---|
214 | #if args['where'] is None: | | #if args['where'] is None: |
---|
215 | # reponse.status_code = 400 | | # reponse.status_code = 400 |
---|
216 | # return response | | # return response |
---|
217 | | | |
---|
218 | params = {} | | params = {} |
---|
219 | | | |
---|
220 | if args.get('where'): | | if args.get('where'): |
---|
221 | params['where'] = args.get('where') | | params['where'] = args.get('where') |
---|
222 | if args.get('who'): | | if args.get('who'): |
---|
223 | params['who'] = args.get('who') | | params['who'] = args.get('who') |
---|
224 | if args.get('what'): | | if args.get('what'): |
---|
225 | params['what'] = args.get('what') | | params['what'] = args.get('what') |
---|
226 | if args.get('how'): | | if args.get('how'): |
---|
227 | params['how'] = args.get('how') | | params['how'] = args.get('how') |
---|
228 | | | |
---|
229 | | | |
---|
230 | print params | | print params |
---|
231 | res = g.collection.find(params) | | res = g.collection.find(params) |
---|
232 | | | |
---|
233 | if res.count() < 1: | | if res.count() < 1: |
---|
234 | response.status_code = 404 | | response.status_code = 404 |
---|
235 | return response | | return response |
---|
236 | | | |
---|
237 | swt_list = [] | | swt_list = [] |
---|
238 | for swt in res: | | for swt in res: |
---|
239 | _id = swt['_id'] | | _id = swt['_id'] |
---|
240 | del(swt['_id']) | | del(swt['_id']) |
---|
241 | swt['id'] = str(_id) | | swt['id'] = str(_id) |
---|
242 | swt_list.append(swt) | | swt_list.append(swt) |
---|
243 | | | |
---|
244 | response.data = json.dumps(swt_list) | | response.data = json.dumps(swt_list) |
---|
245 | return response | | return response |
---|
246 | | | |
---|
247 | | | |
---|
248 | @app.route('/sweets/<post_id>', methods=['GET']) | | @app.route('/sweets/<post_id>', methods=['GET']) |
---|
249 | @app.route('/query/<post_id>',methods=['GET']) | | @app.route('/query/<post_id>',methods=['GET']) |
---|
250 | def return_database_entry(post_id): | | def return_database_entry(post_id): |
---|
251 | try: | | try: |
---|
252 | res = g.collection.find_one({'_id':ObjectId(post_id)}) | | res = g.collection.find_one({'_id':ObjectId(post_id)}) |
---|
253 | if(res): | | if(res): |
---|
254 | res['blog'] = url_for('show_specific_entry', post_id = str(res['_id'])) | | res['blog'] = url_for('show_specific_entry', post_id = str(res['_id'])) |
---|
255 | del(res['_id']) | | del(res['_id']) |
---|
256 | return jsonify(res) | | return jsonify(res) |
---|
257 | # entries = make_list(res) | | # entries = make_list(res) |
---|
258 | # return render_template('show_posts.html', entries=res, str=str) | | # return render_template('show_posts.html', entries=res, str=str) |
---|
259 | else: | | else: |
---|
260 | abort(404) | | abort(404) |
---|
261 | except InvalidId: | | except InvalidId: |
---|
262 | abort(404) | | abort(404) |
---|
263 | | | |
---|
264 | | | |
---|
265 | | | |
---|
266 | @app.route('/posts/<post_id>',methods=['GET']) | | @app.route('/posts/<post_id>',methods=['GET']) |
---|
267 | def show_specific_entry(post_id): | | def show_specific_entry(post_id): |
---|
268 | try: | | try: |
---|
269 | res = g.collection.find({'_id':ObjectId(post_id)}) | | res = g.collection.find({'_id':ObjectId(post_id)}) |
---|
270 | if(res.count() > 0): | | if(res.count() > 0): |
---|
271 | #entries = make_list(res) | | #entries = make_list(res) |
---|
272 | entries = [] | | entries = [] |
---|
273 | for i in res: | | for i in res: |
---|
274 | _id = i['_id'] | | _id = i['_id'] |
---|
275 | del(i['_id']) | | del(i['_id']) |
---|
276 | i['id'] = _id | | i['id'] = _id |
---|
277 | entries.append(i) | | entries.append(i) |
---|
278 | return render_template('show_posts.html', entries=entries, str=str) | | return render_template('show_posts.html', entries=entries, str=str) |
---|
279 | else: | | else: |
---|
280 | abort(404) | | abort(404) |
---|
281 | except InvalidId: | | except InvalidId: |
---|
282 | abort(404) | | abort(404) |
---|
283 | | | |
---|
284 | | | |
---|
285 | @app.route('/posts/delete/', methods=['POST']) | | @app.route('/posts/delete/', methods=['POST']) |
---|
286 | def delete_post(): | | def delete_post(): |
---|
287 | try: | | try: |
---|
288 | g.collection.remove({'_id':ObjectId(request.form['post_id'])}) | | g.collection.remove({'_id':ObjectId(request.form['post_id'])}) |
---|
289 | return jsonify(status='ok') | | return jsonify(status='ok') |
---|
290 | except: | | except: |
---|
291 | abort(500) | | abort(500) |
---|
292 | | | |
---|
293 | @app.route('/logout') | | @app.route('/logout') |
---|
294 | def logout(): | | def logout(): |
---|
295 | session.pop('logged_in', None) | | session.pop('logged_in', None) |
---|
296 | flash('You were logged out') | | flash('You were logged out') |
---|
297 | return redirect(url_for('show_entries')) | | return redirect(url_for('show_entries')) |
---|
298 | | | |
---|
299 | @app.route('/serveUser') | | @app.route('/serveUser') |
---|
300 | def serveUser(): | | def serveUser(): |
---|
301 | if "logged_in" in session: | | if "logged_in" in session: |
---|
302 | #print session["logged_in"] | | #print session["logged_in"] |
---|
303 | session['key'] = conf.SECRET_KEY | | session['key'] = conf.SECRET_KEY |
---|
304 | return render_template('user.html') | | return render_template('user.html') |
---|
305 | else: | | else: |
---|
306 | return render_template('login.html', error=None) | | return render_template('login.html', error=None) |
---|
307 | | | |
---|
308 | @app.route('/user/', methods=['POST', 'GET']) | | @app.route('/user/', methods=['POST', 'GET']) |
---|
309 | @app.route('/user/<user_id>', methods=['GET']) | | @app.route('/user/<user_id>', methods=['GET']) |
---|
310 | def user(user_id='all'): | | def user(user_id='all'): |
---|
311 | if request.method == 'POST': | | if request.method == 'POST': |
---|
312 | response = make_response() | | response = make_response() |
---|
313 | db = g.connection[app.config['DATABASE']] | | db = g.connection[app.config['DATABASE']] |
---|
314 | collection = db['sweet_users'] | | collection = db['sweet_users'] |
---|
315 | | | |
---|
316 | # check if user already exists | | # check if user already exists |
---|
317 | if request.form['user'] in getUsers(): | | if request.form['user'] in getUsers(): |
---|
318 | #print 'user already exists!' | | #print 'user already exists!' |
---|
319 | flash('User already exists!') | | flash('User already exists!') |
---|
320 | return redirect(url_for('serveUser')) | | return redirect(url_for('serveUser')) |
---|
321 | | | |
---|
322 | # else insert new user | | # else insert new user |
---|
323 | collection.insert({'user': request.form['user'], | | collection.insert({'user': request.form['user'], |
---|
324 | 'key': request.form['key']}) | | 'key': request.form['key']}) |
---|
325 | response.status_code = 200 | | response.status_code = 200 |
---|
326 | response.data = 'User added.' | | response.data = 'User added.' |
---|
327 | return response | | return response |
---|
328 | | | |
---|
329 | elif request.method == 'GET': | | elif request.method == 'GET': |
---|
330 | db = g.connection[app.config['DATABASE']] | | db = g.connection[app.config['DATABASE']] |
---|
331 | collection = db['sweet_users'] | | collection = db['sweet_users'] |
---|
332 | users = [] | | users = [] |
---|
333 | if user_id == 'all': | | if user_id == 'all': |
---|
334 | users = getUsers() | | users = getUsers() |
---|
335 | else: | | else: |
---|
336 | user = collection.find_one({'user': user_id}) | | user = collection.find_one({'user': user_id}) |
---|
337 | if user: | | if user: |
---|
338 | users.append(user['user']) | | users.append(user['user']) |
---|
339 | else: | | else: |
---|
340 | abort(404) | | abort(404) |
---|
341 | return render_template("users.html", users=users) | | return render_template("users.html", users=users) |
---|
342 | | | |
---|
343 | | | |
---|
344 | @app.route('/authenticate', methods=['POST','GET']) | | @app.route('/authenticate', methods=['POST','GET']) |
---|
345 | def authenticate(): | | def authenticate(): |
---|
346 | if request.method == "POST": | | if request.method == "POST": |
---|
347 | response = make_response() | | response = make_response() |
---|
348 | db = g.connection[app.config['DATABASE']] | | db = g.connection[app.config['DATABASE']] |
---|
349 | collection = db['sweet_users'] | | collection = db['sweet_users'] |
---|
350 | for i in collection.find(): | | for i in collection.find(): |
---|
351 | if i['user'] == request.form['user'] and i['key'] == request.form['hash']: | | if i['user'] == request.form['user'] and i['key'] == request.form['hash']: |
---|
352 | response.status_code = 200 | | response.status_code = 200 |
---|
353 | response.headers['Access-Control-Allow-Origin'] = '*' | | response.headers['Access-Control-Allow-Origin'] = '*' |
---|
354 | return response | | return response |
---|
355 | else: | | else: |
---|
356 | pass | | pass |
---|
357 | response.status_code = 403 | | response.status_code = 403 |
---|
358 | response.headers['Access-Control-Allow-Origin'] = '*' | | response.headers['Access-Control-Allow-Origin'] = '*' |
---|
359 | return response | | return response |
---|
360 | elif request.method == "GET": | | elif request.method == "GET": |
---|
361 | return app.send_static_file("sweet-authenticate.js") | | return app.send_static_file("sweet-authenticate.js") |
---|
362 | | | |
---|
363 | @app.route('/auth/login', methods=['POST']) | | @app.route('/auth/login', methods=['POST']) |
---|
364 | def authLogin(): | | def authLogin(): |
---|
365 | response = make_response() | | response = make_response() |
---|
366 | response.headers['Access-Control-Allow-Origin'] = 'http://localhost:5000' | | response.headers['Access-Control-Allow-Origin'] = 'http://localhost:5000' |
---|
367 | response.headers['Access-Control-Allow-Credentials'] = 'true' | | response.headers['Access-Control-Allow-Credentials'] = 'true' |
---|
368 | response.headers['Access-Control-Max-Age'] = '20days' | | response.headers['Access-Control-Max-Age'] = '20days' |
---|
369 | response.headers['Access-Control-Allow-Headers'] = 'Origin,\ | | response.headers['Access-Control-Allow-Headers'] = 'Origin,\ |
---|
370 | X-Requested-With, Content-Type, Accept' | | X-Requested-With, Content-Type, Accept' |
---|
371 | | | |
---|
372 | if 'assertion' not in request.form: | | if 'assertion' not in request.form: |
---|
373 | response.status_code = 400 | | response.status_code = 400 |
---|
374 | return response | | return response |
---|
375 | | | |
---|
376 | data = {'assertion': request.form['assertion'], 'audience': | | data = {'assertion': request.form['assertion'], 'audience': |
---|
377 | MOZ_PERSONA_AUDIENCE} | | MOZ_PERSONA_AUDIENCE} |
---|
378 | resp = requests.post(MOZ_PERSONA_VERIFIER, data=data, verify=True) | | resp = requests.post(MOZ_PERSONA_VERIFIER, data=data, verify=True) |
---|
379 | print resp.status_code | | print resp.status_code |
---|
380 | print resp.json() | | print resp.json() |
---|
381 | | | |
---|
382 | if resp.ok: | | if resp.ok: |
---|
383 | verified_data = json.loads(resp.content) | | verified_data = json.loads(resp.content) |
---|
384 | if verified_data['status'] == 'okay': | | if verified_data['status'] == 'okay': |
---|
385 | #session.update({'email': verified_data['email']}) | | #session.update({'email': verified_data['email']}) |
---|
386 | session['email'] = verified_data['email'] | | session['email'] = verified_data['email'] |
---|
387 | response.status_code = 200 | | response.status_code = 200 |
---|
388 | response.data = {'email': verified_data['email']} | | response.data = {'email': verified_data['email']} |
---|
389 | return response | | return response |
---|
390 | | | |
---|
391 | response.status_code = 500 | | response.status_code = 500 |
---|
392 | return response | | return response |
---|
393 | | | |
---|
394 | @app.route('/auth/logout', methods=['POST']) | | @app.route('/auth/logout', methods=['POST']) |
---|
395 | def authLogout(): | | def authLogout(): |
---|
396 | response = make_response() | | response = make_response() |
---|
397 | response.headers['Access-Control-Allow-Origin'] = 'http://localhost:5000' | | response.headers['Access-Control-Allow-Origin'] = 'http://localhost:5000' |
---|
398 | response.headers['Access-Control-Allow-Credentials'] = 'true' | | response.headers['Access-Control-Allow-Credentials'] = 'true' |
---|
399 | response.headers['Access-Control-Max-Age'] = '20days' | | response.headers['Access-Control-Max-Age'] = '20days' |
---|
400 | response.headers['Access-Control-Allow-Headers'] = 'Origin,\ | | response.headers['Access-Control-Allow-Headers'] = 'Origin,\ |
---|
401 | X-Requested-With, Content-Type, Accept' | | X-Requested-With, Content-Type, Accept' |
---|
402 | | | |
---|
403 | if 'email' in session: | | if 'email' in session: |
---|
404 | print 'logging out ' | | print 'logging out ' |
---|
405 | print session['email'] | | print session['email'] |
---|
406 | session.pop('email') | | session.pop('email') |
---|
407 | | | |
---|
408 | response.status_code = 200 | | response.status_code = 200 |
---|
409 | return response | | return response |
---|
410 | | | |
---|
411 | def make_list(res): | | def make_list(res): |
---|
412 | entries = [] | | entries = [] |
---|
413 | for row in res: | | for row in res: |
---|
414 | d = row | | d = row |
---|
415 | d['id'] = str(row['_id']) | | d['id'] = str(row['_id']) |
---|
416 | try: | | try: |
---|
417 | if d['who'] in getUsers() or d['author'] in getUsers(): | | if d['who'] in getUsers() or d['author'] in getUsers(): |
---|
418 | d['registered'] = True | | d['registered'] = True |
---|
419 | except KeyError: | | except KeyError: |
---|
420 | pass | | pass |
---|
421 | entries.append(d) | | entries.append(d) |
---|
422 | return entries | | return entries |
---|
423 | | | |
---|
424 | if __name__ == '__main__': | | if __name__ == '__main__': |
---|
425 | app.run(debug=True, port=5001) | | app.run(debug=True, port=5001) |
---|
| | | | 1 | # -*- coding: utf-8 -*- | | # -*- coding: utf-8 -*- |
---|
2 | """ | | """ |
---|
3 | swtr | | swtr |
---|
4 | ~~~~~~ | | ~~~~~~ |
---|
5 | | | |
---|
6 | http://swtr.us | | http://swtr.us |
---|
7 | | | |
---|
8 | :license: BSD, see LICENSE for more details. | | :license: BSD, see LICENSE for more details. |
---|
9 | """ | | """ |
---|
10 | from __future__ import with_statement | | from __future__ import with_statement |
---|
11 | from pymongo import Connection | | from pymongo import Connection |
---|
12 | from bson.objectid import ObjectId | | from bson.objectid import ObjectId |
---|
13 | from bson.errors import InvalidId | | from bson.errors import InvalidId |
---|
14 | from flask import Flask, request, session, g, redirect, url_for, abort, \ | | from flask import Flask, request, session, g, redirect, url_for, abort, \ |
---|
15 | render_template, flash, _app_ctx_stack, make_response, jsonify | | render_template, flash, _app_ctx_stack, make_response, jsonify |
---|
16 | from urllib import unquote_plus | | from urllib import unquote_plus |
---|
17 | import json | | import json |
---|
18 | import conf | | import conf |
---|
19 | import requests | | import requests |
---|
20 | | | |
---|
21 | # TODO: | | # TODO: |
---|
22 | # restify | | # restify |
---|
23 | # APIs as follows: | | # APIs as follows: |
---|
24 | # GET /sweets/q -> query sweets | | # GET /sweets/q -> query sweets |
---|
25 | # args: who, where, what, how | | # args: who, where, what, how |
---|
26 | # GET /sweets/<id> -> get specific sweet | | # GET /sweets/<id> -> get specific sweet |
---|
27 | # POST /sweets -> post sweets (one or a batch of) | | # POST /sweets -> post sweets (one or a batch of) |
---|
28 | # OPTIONS /sweets - > CORS policy .. understand it better | | # OPTIONS /sweets - > CORS policy .. understand it better |
---|
29 | # classes! | | # classes! |
---|
30 | # sqlAlchemy | | # sqlAlchemy |
---|
31 | # Postgres | | # Postgres |
---|
32 | # Persona, Auth in API endpoints | | # Persona, Auth in API endpoints |
---|
33 | | | |
---|
34 | # TODO: move this in a config file | | # TODO: move this in a config file |
---|
35 | # configuration | | # configuration |
---|
36 | | | |
---|
37 | DATABASE = 'sweets_production' | | DATABASE = 'sweets_production' |
---|
38 | COLLECTION_NAME = 'posts' | | COLLECTION_NAME = 'posts' |
---|
39 | DEBUG = True | | DEBUG = True |
---|
40 | SECRET_KEY = conf.SECRET_KEY | | SECRET_KEY = conf.SECRET_KEY |
---|
41 | USERNAME = 'admin' | | USERNAME = 'admin' |
---|
42 | PASSWORD = 'default' | | PASSWORD = 'default' |
---|
43 | DB_PORT = 27017 | | DB_PORT = 27017 |
---|
44 | DB_HOST = 'localhost' | | DB_HOST = 'localhost' |
---|
45 | URL = 'http://localhost:5001' | | URL = 'http://localhost:5001' |
---|
46 | MOZ_PERSONA_VERIFIER = 'https://verifier.login.persona.org/verify' | | MOZ_PERSONA_VERIFIER = 'https://verifier.login.persona.org/verify' |
---|
47 | MOZ_PERSONA_AUDIENCE = 'http://localhost:5000' | | MOZ_PERSONA_AUDIENCE = 'http://localhost:5000' |
---|
48 | | | |
---|
49 | appURL_map = {'img-anno': 'http://localhost:5000/?where=', | | appURL_map = {'img-anno': 'http://localhost:5000/?where=', |
---|
50 | 're-narration': 'http://y.a11y.in/web?foruri=', | | 're-narration': 'http://y.a11y.in/web?foruri=', |
---|
51 | 'idh-mowl': 'http://app.swtr.us/?where=', | | 'idh-mowl': 'http://app.swtr.us/?where=', |
---|
52 | 'testFromAPI': 'http://app.swtr.us/?where='} | | 'testFromAPI': 'http://app.swtr.us/?where='} |
---|
53 | | | |
---|
54 | # create our little application :) | | # create our little application :) |
---|
55 | # ^ ... It's going to be big now :P | | # ^ ... It's going to be big now :P |
---|
56 | app = Flask(__name__) | | app = Flask(__name__) |
---|
57 | app.config.from_object(__name__) | | app.config.from_object(__name__) |
---|
58 | app.config.from_envvar('FLASKR_SETTINGS', silent=True) | | app.config.from_envvar('FLASKR_SETTINGS', silent=True) |
---|
59 | | | |
---|
60 | # Jinja filters | | # Jinja filters |
---|
61 | app.jinja_env.filters['len'] = len | | app.jinja_env.filters['len'] = len |
---|
62 | | | |
---|
63 | | | |
---|
64 | def validateSweet(payload): | | def validateSweet(payload): |
---|
65 | for i in payload: | | for i in payload: |
---|
66 | try: | | try: |
---|
67 | if len(i['who']) and len(i['what']) and len(i['where']) and\ | | if len(i['who']) and len(i['what']) and len(i['where']) and\ |
---|
68 | len(i['how']) and len(i['created']): | | len(i['how']) and len(i['created']): |
---|
69 | pass | | pass |
---|
70 | else: | | else: |
---|
71 | return False | | return False |
---|
72 | except KeyError: | | except KeyError: |
---|
73 | return False | | return False |
---|
74 | return True | | return True |
---|
75 | | | |
---|
76 | | | |
---|
77 | def getUsers(): | | def getUsers(): |
---|
78 | db = g.connection[app.config['DATABASE']] | | db = g.connection[app.config['DATABASE']] |
---|
79 | coll = db['sweet_users'] | | coll = db['sweet_users'] |
---|
80 | users = [] | | users = [] |
---|
81 | for i in coll.find(): | | for i in coll.find(): |
---|
82 | users.append(i['user']) | | users.append(i['user']) |
---|
83 | return users | | return users |
---|
84 | | | |
---|
85 | def gatherStats(coll): | | def gatherStats(coll): |
---|
86 | stats = {} | | stats = {} |
---|
87 | stats['total_sweets'] = coll.count() | | stats['total_sweets'] = coll.count() |
---|
88 | return stats | | return stats |
---|
89 | | | |
---|
90 | @app.before_request | | @app.before_request |
---|
91 | def init_db(): | | def init_db(): |
---|
92 | g.connection = Connection(app.config['DB_HOST'], app.config['DB_PORT']) | | g.connection = Connection(app.config['DB_HOST'], app.config['DB_PORT']) |
---|
93 | db = g.connection[app.config['DATABASE']] | | db = g.connection[app.config['DATABASE']] |
---|
94 | g.collection = db[app.config["COLLECTION_NAME"]] | | g.collection = db[app.config["COLLECTION_NAME"]] |
---|
95 | g.stats = gatherStats(g.collection) | | g.stats = gatherStats(g.collection) |
---|
96 | | | |
---|
97 | | | |
---|
98 | @app.teardown_request | | @app.teardown_request |
---|
99 | def close_db(exception): | | def close_db(exception): |
---|
100 | g.connection.disconnect() | | g.connection.disconnect() |
---|
101 | | | |
---|
102 | | | |
---|
103 | @app.errorhandler(404) | | @app.errorhandler(404) |
---|
104 | def page_not_found(e): | | def page_not_found(e): |
---|
105 | return render_template('404.html'), 404 | | return render_template('404.html'), 404 |
---|
106 | | | |
---|
107 | | | |
---|
108 | @app.errorhandler(500) | | @app.errorhandler(500) |
---|
109 | def internal_error(e): | | def internal_error(e): |
---|
110 | return render_template('500.html'), 500 | | return render_template('500.html'), 500 |
---|
111 | | | |
---|
112 | @app.route('/') | | @app.route('/') |
---|
113 | def show_entries(): | | def show_entries(): |
---|
114 | res = g.collection.find().sort('_id',direction=-1).limit(100) | | res = g.collection.find().sort('_id',direction=-1).limit(100) |
---|
115 | entries = make_list(res) | | entries = make_list(res) |
---|
116 | return render_template('show_entries.html', entries=entries, | | return render_template('show_entries.html', entries=entries, |
---|
117 | appURL_map=appURL_map, stats=g.stats) | | appURL_map=appURL_map, stats=g.stats) |
---|
118 | | | |
---|
119 | | | |
---|
120 | # TODO: understand if we really need the OPTIONS | | # TODO: understand if we really need the OPTIONS |
---|
121 | @app.route('/sweets', methods=['POST', 'OPTIONS']) | | @app.route('/sweets', methods=['POST', 'OPTIONS']) |
---|
122 | @app.route('/add', methods=['POST', 'OPTIONS']) | | @app.route('/add', methods=['POST', 'OPTIONS']) |
---|
123 | def addSweets(): | | def addSweets(): |
---|
124 | print request.method | | print request.method |
---|
125 | | | |
---|
126 | if request.method == 'OPTIONS': | | if request.method == 'OPTIONS': |
---|
127 | response = make_response() | | response = make_response() |
---|
128 | response.status_code = 200 | | response.status_code = 200 |
---|
129 | response.headers['Access-Control-Allow-Origin'] =\ | | response.headers['Access-Control-Allow-Origin'] =\ |
---|
130 | 'http://localhost:5000' | | 'http://localhost:5000' |
---|
131 | response.headers['Access-Control-Max-Age'] = '20days' | | response.headers['Access-Control-Max-Age'] = '20days' |
---|
132 | response.headers['Access-Control-Allow-Headers'] = 'Origin,\ | | response.headers['Access-Control-Allow-Headers'] = 'Origin,\ |
---|
133 | X-Requested-With, Content-Type, Accept' | | X-Requested-With, Content-Type, Accept' |
---|
134 | return response | | return response |
---|
135 | | | |
---|
136 | response = make_response() | | response = make_response() |
---|
137 | response.headers['Access-Control-Allow-Origin'] = 'http://localhost:5000' | | response.headers['Access-Control-Allow-Origin'] = 'http://localhost:5000' |
---|
138 | response.headers['Access-Control-Allow-Headers'] = 'Origin,\ | | response.headers['Access-Control-Allow-Headers'] = 'Origin,\ |
---|
139 | X-Requested-With, Content-Type, Accept' | | X-Requested-With, Content-Type, Accept' |
---|
140 | data = {} | | data = {} |
---|
141 | data_list = [] | | data_list = [] |
---|
142 | | | |
---|
143 | if 'email' in session: | | if 'email' in session: |
---|
144 | print 'identifed user' | | print 'identifed user' |
---|
145 | print session['email'] | | print session['email'] |
---|
146 | else: | | else: |
---|
147 | print 'unidentified user' | | print 'unidentified user' |
---|
148 | | | |
---|
149 | # TODO: find a better way of handling reqeust sweets | | # TODO: find a better way of handling reqeust sweets |
---|
150 | try: | | try: |
---|
151 | payload = json.loads(request.form['data']) | | payload = json.loads(request.form['data']) |
---|
152 | except: | | except: |
---|
153 | try: | | try: |
---|
154 | payload = [{'who': request.form['who'], 'what': request.form['what'], | | payload = [{'who': request.form['who'], 'what': request.form['what'], |
---|
155 | 'where': request.form['where'], 'how': request.form['how']}] | | 'where': request.form['where'], 'how': request.form['how']}] |
---|
156 | except: | | except: |
---|
157 | try: | | try: |
---|
158 | payload = request.json | | payload = request.json |
---|
159 | except: | | except: |
---|
160 | payload = json.loads(request.data) | | payload = json.loads(request.data) |
---|
161 | | | |
---|
162 | | | |
---|
163 | valid = validateSweet(payload) | | valid = validateSweet(payload) |
---|
164 | if not valid: | | if not valid: |
---|
165 | response.status_code = 400 | | response.status_code = 400 |
---|
166 | response.data = "Bad or Malformed Request. Please check the validity\ | | response.data = "Bad or Malformed Request. Please check the validity\ |
---|
167 | of your request" | | of your request" |
---|
168 | return response | | return response |
---|
169 | print 'swt payload rcvd..' | | print 'swt payload rcvd..' |
---|
170 | print payload | | print payload |
---|
171 | for i in payload: | | for i in payload: |
---|
172 | data = i | | data = i |
---|
173 | id = g.collection.insert(i) | | id = g.collection.insert(i) |
---|
174 | data['permalink'] = app.config['URL'] + '/posts/' + str(ObjectId(id)) | | data['permalink'] = app.config['URL'] + '/posts/' + str(ObjectId(id)) |
---|
175 | data['id'] = str(ObjectId(id)) | | data['id'] = str(ObjectId(id)) |
---|
176 | del(data['_id']) | | del(data['_id']) |
---|
177 | print 'data', data | | print 'data', data |
---|
178 | data_list.append(data) | | data_list.append(data) |
---|
179 | response.data = json.dumps(data_list) | | response.data = json.dumps(data_list) |
---|
180 | print 'swt stored..' | | print 'swt stored..' |
---|
181 | return response | | return response |
---|
182 | | | |
---|
183 | | | |
---|
184 | @app.route('/login', methods=['GET', 'POST']) | | @app.route('/login', methods=['GET', 'POST']) |
---|
185 | def login(): | | def login(): |
---|
186 | error = None | | error = None |
---|
187 | if request.method == 'POST': | | if request.method == 'POST': |
---|
188 | if request.form['username'] != app.config['USERNAME']: | | if request.form['username'] != app.config['USERNAME']: |
---|
189 | error = 'Invalid username' | | error = 'Invalid username' |
---|
190 | elif request.form['password'] != app.config['PASSWORD']: | | elif request.form['password'] != app.config['PASSWORD']: |
---|
191 | error = 'Invalid password' | | error = 'Invalid password' |
---|
192 | else: | | else: |
---|
193 | session['logged_in'] = True | | session['logged_in'] = True |
---|
194 | flash('You were logged in') | | flash('You were logged in') |
---|
195 | return redirect(url_for('show_entries')) | | return redirect(url_for('show_entries')) |
---|
196 | return render_template('login.html', error=error) | | return render_template('login.html', error=error) |
---|
197 | | | |
---|
198 | | | |
---|
199 | @app.route('/sweets/q', methods=['GET']) | | @app.route('/sweets/q', methods=['GET']) |
---|
200 | def searchSweets(): | | def searchSweets(): |
---|
201 | response = make_response() | | response = make_response() |
---|
202 | response.status_code = 200 | | response.status_code = 200 |
---|
203 | response.headers['Access-Control-Allow-Origin'] = 'http://localhost:5000' | | response.headers['Access-Control-Allow-Origin'] = 'http://localhost:5000' |
---|
204 | response.headers['Access-Control-Max-Age'] = '20days' | | response.headers['Access-Control-Max-Age'] = '20days' |
---|
205 | response.headers['Access-Control-Allow-Headers'] = 'Origin,\ | | response.headers['Access-Control-Allow-Headers'] = 'Origin,\ |
---|
206 | X-Requested-With, Content-Type, Accept' | | X-Requested-With, Content-Type, Accept' |
---|
207 | | | |
---|
208 | args = request.args | | args = request.args |
---|
209 | | | |
---|
210 | if args is None: | | if args is None: |
---|
211 | reponse.status_code = 400 | | reponse.status_code = 400 |
---|
212 | return response | | return response |
---|
213 | | | |
---|
214 | #if args['where'] is None: | | #if args['where'] is None: |
---|
215 | # reponse.status_code = 400 | | # reponse.status_code = 400 |
---|
216 | # return response | | # return response |
---|
217 | | | |
---|
218 | params = {} | | params = {} |
---|
219 | | | |
---|
220 | if args.get('where'): | | if args.get('where'): |
---|
221 | params['where'] = args.get('where') | | params['where'] = args.get('where') |
---|
222 | if args.get('who'): | | if args.get('who'): |
---|
223 | params['who'] = args.get('who') | | params['who'] = args.get('who') |
---|
224 | if args.get('what'): | | if args.get('what'): |
---|
225 | params['what'] = args.get('what') | | params['what'] = args.get('what') |
---|
226 | if args.get('how'): | | if args.get('how'): |
---|
227 | params['how'] = args.get('how') | | params['how'] = args.get('how') |
---|
228 | | | |
---|
229 | | | |
---|
230 | print params | | print params |
---|
231 | res = g.collection.find(params) | | res = g.collection.find(params) |
---|
232 | | | |
---|
233 | if res.count() < 1: | | if res.count() < 1: |
---|
234 | response.status_code = 404 | | response.status_code = 404 |
---|
235 | return response | | return response |
---|
236 | | | |
---|
237 | swt_list = [] | | swt_list = [] |
---|
238 | for swt in res: | | for swt in res: |
---|
239 | _id = swt['_id'] | | _id = swt['_id'] |
---|
240 | del(swt['_id']) | | del(swt['_id']) |
---|
241 | swt['id'] = str(_id) | | swt['id'] = str(_id) |
---|
242 | swt_list.append(swt) | | swt_list.append(swt) |
---|
243 | | | |
---|
244 | response.data = json.dumps(swt_list) | | response.data = json.dumps(swt_list) |
---|
245 | return response | | return response |
---|
246 | | | |
---|
247 | | | |
---|
248 | @app.route('/sweets/<post_id>', methods=['GET']) | | @app.route('/sweets/<post_id>', methods=['GET']) |
---|
249 | @app.route('/query/<post_id>',methods=['GET']) | | @app.route('/query/<post_id>',methods=['GET']) |
---|
250 | def return_database_entry(post_id): | | def return_database_entry(post_id): |
---|
251 | try: | | try: |
---|
252 | res = g.collection.find_one({'_id':ObjectId(post_id)}) | | res = g.collection.find_one({'_id':ObjectId(post_id)}) |
---|
253 | if(res): | | if(res): |
---|
254 | res['blog'] = url_for('show_specific_entry', post_id = str(res['_id'])) | | res['blog'] = url_for('show_specific_entry', post_id = str(res['_id'])) |
---|
255 | del(res['_id']) | | del(res['_id']) |
---|
256 | return jsonify(res) | | return jsonify(res) |
---|
257 | # entries = make_list(res) | | # entries = make_list(res) |
---|
258 | # return render_template('show_posts.html', entries=res, str=str) | | # return render_template('show_posts.html', entries=res, str=str) |
---|
259 | else: | | else: |
---|
260 | abort(404) | | abort(404) |
---|
261 | except InvalidId: | | except InvalidId: |
---|
262 | abort(404) | | abort(404) |
---|
263 | | | |
---|
264 | | | |
---|
265 | | | |
---|
266 | @app.route('/posts/<post_id>',methods=['GET']) | | @app.route('/posts/<post_id>',methods=['GET']) |
---|
267 | def show_specific_entry(post_id): | | def show_specific_entry(post_id): |
---|
268 | try: | | try: |
---|
269 | res = g.collection.find({'_id':ObjectId(post_id)}) | | res = g.collection.find({'_id':ObjectId(post_id)}) |
---|
270 | if(res.count() > 0): | | if(res.count() > 0): |
---|
271 | #entries = make_list(res) | | #entries = make_list(res) |
---|
272 | entries = [] | | entries = [] |
---|
273 | for i in res: | | for i in res: |
---|
274 | _id = i['_id'] | | _id = i['_id'] |
---|
275 | del(i['_id']) | | del(i['_id']) |
---|
276 | i['id'] = _id | | i['id'] = _id |
---|
277 | entries.append(i) | | entries.append(i) |
---|
278 | return render_template('show_posts.html', entries=entries, str=str) | | return render_template('show_posts.html', entries=entries, str=str) |
---|
279 | else: | | else: |
---|
280 | abort(404) | | abort(404) |
---|
281 | except InvalidId: | | except InvalidId: |
---|
282 | abort(404) | | abort(404) |
---|
283 | | | |
---|
284 | | | |
---|
285 | @app.route('/posts/delete/', methods=['POST']) | | @app.route('/posts/delete/', methods=['POST']) |
---|
286 | def delete_post(): | | def delete_post(): |
---|
287 | try: | | try: |
---|
288 | g.collection.remove({'_id':ObjectId(request.form['post_id'])}) | | g.collection.remove({'_id':ObjectId(request.form['post_id'])}) |
---|
289 | return jsonify(status='ok') | | return jsonify(status='ok') |
---|
290 | except: | | except: |
---|
291 | abort(500) | | abort(500) |
---|
292 | | | |
---|
293 | @app.route('/logout') | | @app.route('/logout') |
---|
294 | def logout(): | | def logout(): |
---|
295 | session.pop('logged_in', None) | | session.pop('logged_in', None) |
---|
296 | flash('You were logged out') | | flash('You were logged out') |
---|
297 | return redirect(url_for('show_entries')) | | return redirect(url_for('show_entries')) |
---|
298 | | | |
---|
299 | @app.route('/serveUser') | | @app.route('/serveUser') |
---|
300 | def serveUser(): | | def serveUser(): |
---|
301 | if "logged_in" in session: | | if "logged_in" in session: |
---|
302 | #print session["logged_in"] | | #print session["logged_in"] |
---|
303 | session['key'] = conf.SECRET_KEY | | session['key'] = conf.SECRET_KEY |
---|
304 | return render_template('user.html') | | return render_template('user.html') |
---|
305 | else: | | else: |
---|
306 | return render_template('login.html', error=None) | | return render_template('login.html', error=None) |
---|
307 | | | |
---|
308 | @app.route('/user/', methods=['POST', 'GET']) | | @app.route('/user/', methods=['POST', 'GET']) |
---|
309 | @app.route('/user/<user_id>', methods=['GET']) | | @app.route('/user/<user_id>', methods=['GET']) |
---|
310 | def user(user_id='all'): | | def user(user_id='all'): |
---|
311 | if request.method == 'POST': | | if request.method == 'POST': |
---|
312 | response = make_response() | | response = make_response() |
---|
313 | db = g.connection[app.config['DATABASE']] | | db = g.connection[app.config['DATABASE']] |
---|
314 | collection = db['sweet_users'] | | collection = db['sweet_users'] |
---|
315 | | | |
---|
316 | # check if user already exists | | # check if user already exists |
---|
317 | if request.form['user'] in getUsers(): | | if request.form['user'] in getUsers(): |
---|
318 | #print 'user already exists!' | | #print 'user already exists!' |
---|
319 | flash('User already exists!') | | flash('User already exists!') |
---|
320 | return redirect(url_for('serveUser')) | | return redirect(url_for('serveUser')) |
---|
321 | | | |
---|
322 | # else insert new user | | # else insert new user |
---|
323 | collection.insert({'user': request.form['user'], | | collection.insert({'user': request.form['user'], |
---|
324 | 'key': request.form['key']}) | | 'key': request.form['key']}) |
---|
325 | response.status_code = 200 | | response.status_code = 200 |
---|
326 | response.data = 'User added.' | | response.data = 'User added.' |
---|
327 | return response | | return response |
---|
328 | | | |
---|
329 | elif request.method == 'GET': | | elif request.method == 'GET': |
---|
330 | db = g.connection[app.config['DATABASE']] | | db = g.connection[app.config['DATABASE']] |
---|
331 | collection = db['sweet_users'] | | collection = db['sweet_users'] |
---|
332 | users = [] | | users = [] |
---|
333 | if user_id == 'all': | | if user_id == 'all': |
---|
334 | users = getUsers() | | users = getUsers() |
---|
335 | else: | | else: |
---|
336 | user = collection.find_one({'user': user_id}) | | user = collection.find_one({'user': user_id}) |
---|
337 | if user: | | if user: |
---|
338 | users.append(user['user']) | | users.append(user['user']) |
---|
339 | else: | | else: |
---|
340 | abort(404) | | abort(404) |
---|
341 | return render_template("users.html", users=users) | | return render_template("users.html", users=users) |
---|
342 | | | |
---|
343 | | | |
---|
344 | @app.route('/authenticate', methods=['POST','GET']) | | @app.route('/authenticate', methods=['POST','GET']) |
---|
345 | def authenticate(): | | def authenticate(): |
---|
346 | if request.method == "POST": | | if request.method == "POST": |
---|
347 | response = make_response() | | response = make_response() |
---|
348 | db = g.connection[app.config['DATABASE']] | | db = g.connection[app.config['DATABASE']] |
---|
349 | collection = db['sweet_users'] | | collection = db['sweet_users'] |
---|
350 | for i in collection.find(): | | for i in collection.find(): |
---|
351 | if i['user'] == request.form['user'] and i['key'] == request.form['hash']: | | if i['user'] == request.form['user'] and i['key'] == request.form['hash']: |
---|
352 | response.status_code = 200 | | response.status_code = 200 |
---|
353 | response.headers['Access-Control-Allow-Origin'] = '*' | | response.headers['Access-Control-Allow-Origin'] = '*' |
---|
354 | return response | | return response |
---|
355 | else: | | else: |
---|
356 | pass | | pass |
---|
357 | response.status_code = 403 | | response.status_code = 403 |
---|
358 | response.headers['Access-Control-Allow-Origin'] = '*' | | response.headers['Access-Control-Allow-Origin'] = '*' |
---|
359 | return response | | return response |
---|
360 | elif request.method == "GET": | | elif request.method == "GET": |
---|
361 | return app.send_static_file("sweet-authenticate.js") | | return app.send_static_file("sweet-authenticate.js") |
---|
362 | | | |
---|
363 | @app.route('/auth/login', methods=['POST']) | | @app.route('/auth/login', methods=['POST']) |
---|
364 | def authLogin(): | | def authLogin(): |
---|
365 | response = make_response() | | response = make_response() |
---|
366 | response.headers['Access-Control-Allow-Origin'] = 'http://localhost:5000' | | response.headers['Access-Control-Allow-Origin'] = 'http://localhost:5000' |
---|
367 | response.headers['Access-Control-Allow-Credentials'] = 'true' | | response.headers['Access-Control-Allow-Credentials'] = 'true' |
---|
368 | response.headers['Access-Control-Max-Age'] = '20days' | | response.headers['Access-Control-Max-Age'] = '20days' |
---|
369 | response.headers['Access-Control-Allow-Headers'] = 'Origin,\ | | response.headers['Access-Control-Allow-Headers'] = 'Origin,\ |
---|
370 | X-Requested-With, Content-Type, Accept' | | X-Requested-With, Content-Type, Accept' |
---|
371 | | | |
---|
372 | if 'assertion' not in request.form: | | if 'assertion' not in request.form: |
---|
373 | response.status_code = 400 | | response.status_code = 400 |
---|
374 | return response | | return response |
---|
375 | | | |
---|
376 | data = {'assertion': request.form['assertion'], 'audience': | | data = {'assertion': request.form['assertion'], 'audience': |
---|
377 | MOZ_PERSONA_AUDIENCE} | | MOZ_PERSONA_AUDIENCE} |
---|
378 | resp = requests.post(MOZ_PERSONA_VERIFIER, data=data, verify=True) | | resp = requests.post(MOZ_PERSONA_VERIFIER, data=data, verify=True) |
---|
379 | print resp.status_code | | print resp.status_code |
---|
380 | print resp.json() | | print resp.json() |
---|
381 | | | |
---|
382 | if resp.ok: | | if resp.ok: |
---|
383 | verified_data = json.loads(resp.content) | | verified_data = json.loads(resp.content) |
---|
384 | if verified_data['status'] == 'okay': | | if verified_data['status'] == 'okay': |
---|
385 | #session.update({'email': verified_data['email']}) | | #session.update({'email': verified_data['email']}) |
---|
386 | session['email'] = verified_data['email'] | | session['email'] = verified_data['email'] |
---|
387 | response.status_code = 200 | | response.status_code = 200 |
---|
388 | response.data = {'email': verified_data['email']} | | response.data = {'email': verified_data['email']} |
---|
389 | return response | | return response |
---|
390 | | | |
---|
391 | response.status_code = 500 | | response.status_code = 500 |
---|
392 | return response | | return response |
---|
393 | | | |
---|
394 | @app.route('/auth/logout', methods=['POST']) | | @app.route('/auth/logout', methods=['POST']) |
---|
395 | def authLogout(): | | def authLogout(): |
---|
396 | response = make_response() | | response = make_response() |
---|
397 | response.headers['Access-Control-Allow-Origin'] = 'http://localhost:5000' | | response.headers['Access-Control-Allow-Origin'] = 'http://localhost:5000' |
---|
398 | response.headers['Access-Control-Allow-Credentials'] = 'true' | | response.headers['Access-Control-Allow-Credentials'] = 'true' |
---|
399 | response.headers['Access-Control-Max-Age'] = '20days' | | response.headers['Access-Control-Max-Age'] = '20days' |
---|
400 | response.headers['Access-Control-Allow-Headers'] = 'Origin,\ | | response.headers['Access-Control-Allow-Headers'] = 'Origin,\ |
---|
401 | X-Requested-With, Content-Type, Accept' | | X-Requested-With, Content-Type, Accept' |
---|
402 | | | |
---|
403 | if 'email' in session: | | if 'email' in session: |
---|
404 | print 'logging out ' | | print 'logging out ' |
---|
405 | print session['email'] | | print session['email'] |
---|
406 | session.pop('email') | | session.pop('email') |
---|
407 | | | |
---|
408 | response.status_code = 200 | | response.status_code = 200 |
---|
409 | return response | | return response |
---|
410 | | | |
---|
411 | def make_list(res): | | def make_list(res): |
---|
412 | entries = [] | | entries = [] |
---|
413 | for row in res: | | for row in res: |
---|
414 | d = row | | d = row |
---|
415 | d['id'] = str(row['_id']) | | d['id'] = str(row['_id']) |
---|
416 | try: | | try: |
---|
417 | if d['who'] in getUsers() or d['author'] in getUsers(): | | if d['who'] in getUsers() or d['author'] in getUsers(): |
---|
418 | d['registered'] = True | | d['registered'] = True |
---|
419 | except KeyError: | | except KeyError: |
---|
420 | pass | | pass |
---|
421 | entries.append(d) | | entries.append(d) |
---|
422 | return entries | | return entries |
---|
423 | | | |
---|
424 | if __name__ == '__main__': | | if __name__ == '__main__': |
---|
425 | app.run(debug=True, port=5001) | | app.run(debug=True, port=5001) |
---|