Commit de07eb494c75073a958f09ad2ca8d6859395dc95

  • avatar
  • arvind
  • Wed Jan 09 22:00:28 IST 2013
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 -----------++++++++++++++++++
  • Diff rendering mode:
  • inline
  • side by side

swtr.py

12from bson.objectid import ObjectId12from bson.objectid import ObjectId
13from bson.errors import InvalidId13from bson.errors import InvalidId
14from flask import Flask, request, session, g, redirect, url_for, abort, \14from flask import Flask, request, session, g, redirect, url_for, abort, \
15 render_template, flash, _app_ctx_stack
16
15 render_template, flash, _app_ctx_stack, make_response, jsonify
16from urllib import unquote_plus
17import json
17# configuration18# configuration
18DATABASE = 'alipiBlog'19DATABASE = 'alipiBlog'
19COLLECTION_NAME = 'posts'20COLLECTION_NAME = 'posts'
24PASSWORD = 'default'24PASSWORD = 'default'
25DB_PORT = 2701725DB_PORT = 27017
26DB_HOST = 'localhost'26DB_HOST = 'localhost'
27
27URL = "http://localhost:5000"
28# create our little application :)28# create our little application :)
29app = Flask(__name__)29app = Flask(__name__)
30app.config.from_object(__name__)30app.config.from_object(__name__)
43 g.connection.disconnect()43 g.connection.disconnect()
4444
4545
46@app.errorhandler(400)
46@app.errorhandler(404)
47def page_not_found(e):47def page_not_found(e):
48 return render_template('404.html'), 40448 return render_template('404.html'), 404
4949
6161
62@app.route('/add', methods=['POST'])62@app.route('/add', methods=['POST'])
63def add_entry():63def add_entry():
64 if not session.get('logged_in'):
65 abort(401)
66 g.collection.insert({'user':request.form['user'],'title':request.form['title'], 'text':request.form['text']})
67 flash('New entry was successfully posted')
68 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
6975
7076
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)
100 abort(404)
101 except InvalidId:101 except InvalidId:
102 abort(400)
102 abort(404)
103103
104104
105@app.route('/posts/delete/', methods=['POST'])105@app.route('/posts/delete/', methods=['POST'])