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
(18 / 11)
  
1212from bson.objectid import ObjectId
1313from bson.errors import InvalidId
1414from 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
1718# configuration
1819DATABASE = 'alipiBlog'
1920COLLECTION_NAME = 'posts'
2424PASSWORD = 'default'
2525DB_PORT = 27017
2626DB_HOST = 'localhost'
27
27URL = "http://localhost:5000"
2828# create our little application :)
2929app = Flask(__name__)
3030app.config.from_object(__name__)
4343 g.connection.disconnect()
4444
4545
46@app.errorhandler(400)
46@app.errorhandler(404)
4747def page_not_found(e):
4848 return render_template('404.html'), 404
4949
6161
6262@app.route('/add', methods=['POST'])
6363def 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
7177@app.route('/login', methods=['GET', 'POST'])
9797 entries = make_list(res)
9898 return render_template('show_posts.html', entries=entries, str=str)
9999 else:
100 abort(400)
100 abort(404)
101101 except InvalidId:
102 abort(400)
102 abort(404)
103103
104104
105105@app.route('/posts/delete/', methods=['POST'])