Commit 1a7990c568f0000388ae067c8cefc2d9befd0d93

  • avatar
  • arvind
  • Fri Apr 26 20:29:43 IST 2013
Adding API
			 - /query to search for a SWeeT
swtr.py
(30 / 11)
  
3838 g.collection = db[app.config["COLLECTION_NAME"]]
3939
4040
41
4142@app.teardown_request
4243def close_db(exception):
4344 g.connection.disconnect()
6666 response.headers['Access-Control-Allow-Origin'] = '*'
6767 data = {}
6868 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)
7271 data['permalink'] = app.config['URL']+'/posts/'+str(ObjectId(id))
72 data['id'] = str(ObjectId(id))
7373 data_list.append(data)
7474 response.data = json.dumps(data_list)
7575 return response
9090 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
93110@app.route('/posts/<post_id>',methods=['GET'])
94111def show_specific_entry(post_id):
95112 try:
96 res = g.collection.find({'_id':ObjectId(post_id)});
113 res = g.collection.find({'_id':ObjectId(post_id)})
114 print res
97115 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)
100118 else:
101119 abort(404)
102120 except InvalidId:
137137def make_list(res):
138138 entries = []
139139 for row in res:
140 d = dict()
140 d = row
141141 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"]
145145 entries.append(d)
146146 return entries
147147
148148if __name__ == '__main__':
149 app.run()
149 app.run(debug=True, port=5001)
  
1212<body>
1313<ul class=entries>
1414 {% 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 %}
1822</ul>
1923</body>