Commit de07eb494c75073a958f09ad2ca8d6859395dc95
Better API's
- /add now returns a list of permalinks to the posts/tweets that are created.
What changed?
Ticket number:X
- swtr.py 29 -----------++++++++++++++++++
| | | | 12 | from bson.objectid import ObjectId | 12 | from bson.objectid import ObjectId |
---|
13 | from bson.errors import InvalidId | 13 | from bson.errors import InvalidId |
---|
14 | from flask import Flask, request, session, g, redirect, url_for, abort, \ | 14 | from flask import Flask, request, session, g, redirect, url_for, abort, \ |
---|
15 | render_template, flash, _app_ctx_stack | | render_template, flash, _app_ctx_stack |
---|
16 | | | |
---|
| | 15 | render_template, flash, _app_ctx_stack, make_response, jsonify | | | 16 | from urllib import unquote_plus |
---|
| | 17 | import json |
---|
17 | # configuration | 18 | # configuration |
---|
18 | DATABASE = 'alipiBlog' | 19 | DATABASE = 'alipiBlog' |
---|
19 | COLLECTION_NAME = 'posts' | 20 | COLLECTION_NAME = 'posts' |
---|
… | | … | |
---|
24 | PASSWORD = 'default' | 24 | PASSWORD = 'default' |
---|
25 | DB_PORT = 27017 | 25 | DB_PORT = 27017 |
---|
26 | DB_HOST = 'localhost' | 26 | DB_HOST = 'localhost' |
---|
27 | | | |
---|
| | 27 | URL = "http://localhost:5000" | 28 | # create our little application :) | 28 | # create our little application :) |
---|
29 | app = Flask(__name__) | 29 | app = Flask(__name__) |
---|
30 | app.config.from_object(__name__) | 30 | app.config.from_object(__name__) |
---|
… | | … | |
---|
43 | g.connection.disconnect() | 43 | g.connection.disconnect() |
---|
44 | | 44 | |
---|
45 | | 45 | |
---|
46 | @app.errorhandler(400) | | @app.errorhandler(400) |
---|
| | 46 | @app.errorhandler(404) | 47 | def page_not_found(e): | 47 | def page_not_found(e): |
---|
48 | return render_template('404.html'), 404 | 48 | return render_template('404.html'), 404 |
---|
49 | | 49 | |
---|
… | | … | |
---|
61 | | 61 | |
---|
62 | @app.route('/add', methods=['POST']) | 62 | @app.route('/add', methods=['POST']) |
---|
63 | def add_entry(): | 63 | def add_entry(): |
---|
64 | if not session.get('logged_in'): | | if not session.get('logged_in'): |
---|
65 | abort(401) | | abort(401) |
---|
66 | g.collection.insert({'user':request.form['user'],'title':request.form['title'], 'text':request.form['text']}) | | g.collection.insert({'user':request.form['user'],'title':request.form['title'], 'text':request.form['text']}) |
---|
67 | flash('New entry was successfully posted') | | flash('New entry was successfully posted') |
---|
68 | return redirect(url_for('show_entries')) | | return redirect(url_for('show_entries')) |
---|
| | 64 | response = make_response() | | | 65 | response.headers['Access-Control-Allow-Origin'] = '*' |
---|
| | 66 | data = {} |
---|
| | 67 | data_list = [] |
---|
| | 68 | escaped_form_data = json.loads(unquote_plus(request.form['data'])) |
---|
| | 69 | for i in escaped_form_data: |
---|
| | 70 | id = g.collection.insert({'user':i['user'],'title':i['title'], 'text':i['text'],'top':i['top'],'bottom':i['bottom'],'left':i['left'],'right':i['right']}) |
---|
| | 71 | data['permalink'] = app.config['URL']+'/posts/'+str(ObjectId(id)) |
---|
| | 72 | data_list.append(data) |
---|
| | 73 | response.data = json.dumps(data_list) |
---|
| | 74 | return response |
---|
69 | | 75 | |
---|
70 | | 76 | |
---|
71 | @app.route('/login', methods=['GET', 'POST']) | 77 | @app.route('/login', methods=['GET', 'POST']) |
---|
… | | … | |
---|
97 | entries = make_list(res) | 97 | entries = make_list(res) |
---|
98 | return render_template('show_posts.html', entries=entries, str=str) | 98 | return render_template('show_posts.html', entries=entries, str=str) |
---|
99 | else: | 99 | else: |
---|
100 | abort(400) | | abort(400) |
---|
| | 100 | abort(404) | 101 | except InvalidId: | 101 | except InvalidId: |
---|
102 | abort(400) | | abort(400) |
---|
| | 102 | abort(404) | 103 | | 103 | |
---|
104 | | 104 | |
---|
105 | @app.route('/posts/delete/', methods=['POST']) | 105 | @app.route('/posts/delete/', methods=['POST']) |
---|