Commit bed764d3e75e5d64f73d9e98c0c468ba87092ac7
- Diff rendering mode:
- inline
- side by side
swtr.py
(86 / 4)
  | |||
16 | 16 | from urllib import unquote_plus | |
17 | 17 | import json | |
18 | 18 | import 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 | 33 | # configuration | |
20 | 34 | DATABASE = 'alipiBlog' | |
21 | 35 | COLLECTION_NAME = 'posts' | |
… | … | ||
40 | 40 | DB_PORT = 27017 | |
41 | 41 | DB_HOST = 'localhost' | |
42 | 42 | URL = "http://localhost:5001" | |
43 | |||
43 | 44 | # create our little application :) | |
45 | # ^ ... It's going to be big now :P | ||
44 | 46 | app = Flask(__name__) | |
45 | 47 | app.config.from_object(__name__) | |
46 | 48 | app.config.from_envvar('FLASKR_SETTINGS', silent=True) | |
… | … | ||
94 | 94 | ||
95 | 95 | @app.route('/') | |
96 | 96 | def show_entries(): | |
97 | print 'request:' | ||
98 | print request.method | ||
97 | 99 | res = g.collection.find().sort('_id',direction=-1) | |
98 | 100 | entries = make_list(res) | |
99 | 101 | return render_template('show_entries.html', entries=entries) | |
100 | 102 | ||
101 | 103 | ||
102 | @app.route('/add', methods=['POST']) | ||
103 | def 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']) | ||
107 | def 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 | 119 | response = make_response() | |
105 | 120 | response.headers['Access-Control-Allow-Origin'] = '*' | |
121 | response.headers['Access-Control-Allow-Headers'] = 'Origin,\ | ||
122 | X-Requested-With, Content-Type, Accept' | ||
106 | 123 | data = {} | |
107 | 124 | data_list = [] | |
125 | # TODO: find a better way of handling reqeust sweets | ||
108 | 126 | try: | |
109 | 127 | payload = json.loads(request.form['data']) | |
110 | 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 | 139 | valid = validateSweet(payload) | |
114 | 140 | if not valid: | |
115 | 141 | response.status_code = 400 | |
… | … | ||
145 | 145 | print 'swt payload rcvd..' | |
146 | 146 | print payload | |
147 | 147 | for i in payload: | |
148 | data = i | ||
148 | 149 | id = g.collection.insert(i) | |
149 | 150 | data['permalink'] = app.config['URL'] + '/posts/' + str(ObjectId(id)) | |
150 | 151 | data['id'] = str(ObjectId(id)) | |
152 | del(data['_id']) | ||
153 | print 'data', data | ||
151 | 154 | data_list.append(data) | |
152 | 155 | response.data = json.dumps(data_list) | |
153 | 156 | print 'swt stored..' | |
… | … | ||
172 | 172 | return render_template('login.html', error=error) | |
173 | 173 | ||
174 | 174 | ||
175 | @app.route('/sweets/q', methods=['GET']) | ||
176 | def 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 | 212 | @app.route('/query/<post_id>',methods=['GET']) | |
176 | 213 | def return_database_entry(post_id): | |
177 | 214 | try: |