Commit 1a7990c568f0000388ae067c8cefc2d9befd0d93

  • avatar
  • arvind
  • Fri Apr 26 20:29:43 IST 2013
Adding API
			 - /query to search for a SWeeT
  • Diff rendering mode:
  • inline
  • side by side

swtr.py

38 g.collection = db[app.config["COLLECTION_NAME"]]38 g.collection = db[app.config["COLLECTION_NAME"]]
3939
4040
41
41@app.teardown_request42@app.teardown_request
42def close_db(exception):43def close_db(exception):
43 g.connection.disconnect()44 g.connection.disconnect()
66 response.headers['Access-Control-Allow-Origin'] = '*'66 response.headers['Access-Control-Allow-Origin'] = '*'
67 data = {}67 data = {}
68 data_list = []68 data_list = []
69 escaped_form_data = json.loads(unquote_plus(request.form['data']))
70 for i in escaped_form_data:
71 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'],'url':i['url']}) #Change i['user'] to 'i'.
69 for i in json.loads(request.form['data']):
70 id = g.collection.insert(i)
72 data['permalink'] = app.config['URL']+'/posts/'+str(ObjectId(id))71 data['permalink'] = app.config['URL']+'/posts/'+str(ObjectId(id))
72 data['id'] = str(ObjectId(id))
73 data_list.append(data)73 data_list.append(data)
74 response.data = json.dumps(data_list)74 response.data = json.dumps(data_list)
75 return response75 return response
90 return render_template('login.html', error=error)90 return render_template('login.html', error=error)
9191
9292
93@app.route('/query/<post_id>',methods=['GET'])
94def return_database_entry(post_id):
95 try:
96 res = g.collection.find_one({'_id':ObjectId(post_id)})
97 if(res):
98 res['blog'] = url_for('show_specific_entry', post_id = str(res['_id']))
99 del(res['_id'])
100 return jsonify(res)
101 # entries = make_list(res)
102 # return render_template('show_posts.html', entries=res, str=str)
103 else:
104 abort(404)
105 except InvalidId:
106 abort(404)
107
108
109
93@app.route('/posts/<post_id>',methods=['GET'])110@app.route('/posts/<post_id>',methods=['GET'])
94def show_specific_entry(post_id):111def show_specific_entry(post_id):
95 try:112 try:
96 res = g.collection.find({'_id':ObjectId(post_id)});
113 res = g.collection.find({'_id':ObjectId(post_id)})
114 print res
97 if(res.count() > 0):115 if(res.count() > 0):
98 entries = make_list(res)
99 return render_template('show_posts.html', entries=entries, str=str)
116 #entries = make_list(res)
117 return render_template('show_posts.html', entries=res, str=str)
100 else:118 else:
101 abort(404)119 abort(404)
102 except InvalidId:120 except InvalidId:
137def make_list(res):137def make_list(res):
138 entries = []138 entries = []
139 for row in res:139 for row in res:
140 d = dict()
140 d = row
141 d['id'] = str(row['_id'])141 d['id'] = str(row['_id'])
142 d['text'] = row['text']
143 d["title"] = row["title"]
144 d["user"] = row["user"]
142 # d['text'] = row['text']
143 # d["title"] = row["title"]
144 # d["user"] = row["user"]
145 entries.append(d)145 entries.append(d)
146 return entries146 return entries
147147
148if __name__ == '__main__':148if __name__ == '__main__':
149 app.run()
149 app.run(debug=True, port=5001)

templates/show_posts.html

12<body>12<body>
13<ul class=entries>13<ul class=entries>
14 {% for entry in entries %}14 {% for entry in entries %}
15 <li><h2>{{ entry.title }}</h2>@<b>{{entry.user}}</b> <a href=""><i class="icon-trash" onclick=deletePost({{ entry.id|string|tojson|safe }});></i></a>
16 <p> {{ entry.text|safe }} </p>
17 {% endfor %}
15 <li><h2>{{ entry.title }}</h2>@<b>{{entry.author}}</b>
16 {% if entry.type == 1 %}
17 <p> re-narrate "{{ entry.about|safe }}{{ entry.xpath|safe }}" with "{{ entry.data|safe }}" at "{{ entry.location|safe }}" in "{{ entry.lang|safe }}"</p>
18 <a href=""><i class="icon-trash" onclick=deletePost({{ entry.id|string|tojson|safe }});></i></a>
19 {% endif %}
20 <!-- <p> {{ entry.text|safe }} </p> -->
21 {% endfor %}
18</ul>22</ul>
19</body>23</body>