Commit bed764d3e75e5d64f73d9e98c0c468ba87092ac7

Add few new API endpoints
  • swtr.py 90 ----++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  • Diff rendering mode:
  • inline
  • side by side

swtr.py

16from urllib import unquote_plus16from urllib import unquote_plus
17import json17import json
18import conf18import conf
19
20# TODO:
21# restify
22# APIs as follows:
23# GET /sweets/q -> query sweets
24# args: who, where, what, how
25# GET /sweets/<id> -> get specific sweet
26# POST /sweets -> post sweets (one or a batch of)
27# OPTIONS /sweets - > CORS policy .. understand it better
28# classes!
29# sqlAlchemy
30# Postgres
31
32# TODO: move this in a config file
19# configuration33# configuration
20DATABASE = 'alipiBlog'34DATABASE = 'alipiBlog'
21COLLECTION_NAME = 'posts'35COLLECTION_NAME = 'posts'
40DB_PORT = 2701740DB_PORT = 27017
41DB_HOST = 'localhost'41DB_HOST = 'localhost'
42URL = "http://localhost:5001"42URL = "http://localhost:5001"
43
43# create our little application :)44# create our little application :)
45# ^ ... It's going to be big now :P
44app = Flask(__name__)46app = Flask(__name__)
45app.config.from_object(__name__)47app.config.from_object(__name__)
46app.config.from_envvar('FLASKR_SETTINGS', silent=True)48app.config.from_envvar('FLASKR_SETTINGS', silent=True)
9494
95@app.route('/')95@app.route('/')
96def show_entries():96def show_entries():
97 print 'request:'
98 print request.method
97 res = g.collection.find().sort('_id',direction=-1)99 res = g.collection.find().sort('_id',direction=-1)
98 entries = make_list(res)100 entries = make_list(res)
99 return render_template('show_entries.html', entries=entries)101 return render_template('show_entries.html', entries=entries)
100102
101103
102@app.route('/add', methods=['POST'])
103def add_entry():
104# TODO: understand if we really need the OPTIONS
105@app.route('/sweets', methods=['POST', 'OPTIONS'])
106@app.route('/add', methods=['POST', 'OPTIONS'])
107def addSweets():
108 print request.method
109
110 if request.method == 'OPTIONS':
111 response = make_response()
112 response.status_code = 200
113 response.headers['Access-Control-Allow-Origin'] = '*'
114 response.headers['Access-Control-Max-Age'] = '20days'
115 response.headers['Access-Control-Allow-Headers'] = 'Origin,\
116 X-Requested-With, Content-Type, Accept'
117 return response
118
104 response = make_response()119 response = make_response()
105 response.headers['Access-Control-Allow-Origin'] = '*'120 response.headers['Access-Control-Allow-Origin'] = '*'
121 response.headers['Access-Control-Allow-Headers'] = 'Origin,\
122 X-Requested-With, Content-Type, Accept'
106 data = {}123 data = {}
107 data_list = []124 data_list = []
125 # TODO: find a better way of handling reqeust sweets
108 try:126 try:
109 payload = json.loads(request.form['data'])127 payload = json.loads(request.form['data'])
110 except:128 except:
111 payload = [{'who': request.form['who'], 'what': request.form['what'],
112 'where': request.form['where'], 'how': request.form['how']}]
129 try:
130 payload = [{'who': request.form['who'], 'what': request.form['what'],
131 'where': request.form['where'], 'how': request.form['how']}]
132 except:
133 try:
134 payload = request.json
135 except:
136 payload = json.loads(request.data)
137
138
113 valid = validateSweet(payload)139 valid = validateSweet(payload)
114 if not valid:140 if not valid:
115 response.status_code = 400141 response.status_code = 400
145 print 'swt payload rcvd..'145 print 'swt payload rcvd..'
146 print payload146 print payload
147 for i in payload:147 for i in payload:
148 data = i
148 id = g.collection.insert(i)149 id = g.collection.insert(i)
149 data['permalink'] = app.config['URL'] + '/posts/' + str(ObjectId(id))150 data['permalink'] = app.config['URL'] + '/posts/' + str(ObjectId(id))
150 data['id'] = str(ObjectId(id))151 data['id'] = str(ObjectId(id))
152 del(data['_id'])
153 print 'data', data
151 data_list.append(data)154 data_list.append(data)
152 response.data = json.dumps(data_list)155 response.data = json.dumps(data_list)
153 print 'swt stored..'156 print 'swt stored..'
172 return render_template('login.html', error=error)172 return render_template('login.html', error=error)
173173
174174
175@app.route('/sweets/q', methods=['GET'])
176def searchSweets():
177 response = make_response()
178 response.status_code = 200
179 response.headers['Access-Control-Allow-Origin'] = '*'
180 response.headers['Access-Control-Max-Age'] = '20days'
181 response.headers['Access-Control-Allow-Headers'] = 'Origin,\
182 X-Requested-With, Content-Type, Accept'
183
184 args = request.args
185
186 if args is None:
187 reponse.status_code = 400
188 return response
189
190 if args['where'] is None:
191 reponse.status_code = 400
192 return response
193
194 res = g.collection.find(args)
195
196 if res.count() < 1:
197 response.status_code = 404
198 return response
199
200 swt_list = []
201 for swt in res:
202 _id = swt['_id']
203 del(swt['_id'])
204 swt['id'] = str(_id)
205 swt_list.append(swt)
206
207 response.data = json.dumps(swt_list)
208 return response
209
210
211@app.route('/sweets/<post_id>', methods=['GET'])
175@app.route('/query/<post_id>',methods=['GET'])212@app.route('/query/<post_id>',methods=['GET'])
176def return_database_entry(post_id):213def return_database_entry(post_id):
177 try:214 try: