Commit b724e6010e4acbbf5e4d212acfc67db2be861cff

  • avatar
  • arvind
  • Sat Apr 05 18:13:13 IST 2014
Fix malformed how of re-narration type. `how` has blogUrl key now
which contains the permalink of the post.
Fix PEP8 warnings and errors and on the alipi.py code.
  • alipi/alipi.py 224 -----------------------------------------------------------------------------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  • Diff rendering mode:
  • inline
  • side by side

alipi/alipi.py

8import urllib28import urllib2
9import StringIO9import StringIO
10from flask import g10from flask import g
11from flask import redirect
12from urllib import quote_plus11from urllib import quote_plus
13from urllib import unquote_plus12from urllib import unquote_plus
14import conf13import conf
16import requests16import requests
17from flask import jsonify17from flask import jsonify
18import json18import json
19from flask import url_for
2019
20
21app = Flask(__name__)21app = Flask(__name__)
22
23
22@app.before_request24@app.before_request
23def first():25def first():
24 g.connection = pymongo.MongoClient('localhost',27017) #Create the object once and use it.
26 g.connection = pymongo.MongoClient('localhost', 27017) # Create the
27 # object once and use it.
25 g.db = g.connection[conf.MONGODB[0]]28 g.db = g.connection[conf.MONGODB[0]]
2629
27# @app.after_request
28# def set_secret(response):
29# response.set_cookie("key", conf.SWEET_SECRET_KEY[0])
3030
31
32@app.teardown_request31@app.teardown_request
33def close(exception):32def close(exception):
34 g.connection.disconnect()33 g.connection.disconnect()
3534
3635
37@app.route('/')36@app.route('/')
38def start_page() :
37def start_page():
39 d = {}38 d = {}
40 d['foruri'] = request.args['foruri']39 d['foruri'] = request.args['foruri']
41 myhandler1 = urllib2.Request(d['foruri'],headers={'User-Agent':"Mozilla/5.0 (X11; Linux x86_64; rv:25.0) Gecko/20100101 Firefox/25.0)"}) #A fix to send user-agents, so that sites render properly.
40 myhandler1 = urllib2.Request(d['foruri'],
41 headers={'User-Agent':
42 "Mozilla/5.0 (X11; " +
43 "Linux x86_64; rv:25.0)" +
44 "Gecko/20100101 Firefox/25.0)"})
45 # A fix to send user-agents, so that sites render properly.
42 try:46 try:
43 a = urllib2.urlopen(myhandler1)47 a = urllib2.urlopen(myhandler1)
44 if a.geturl() != d['foruri']:48 if a.geturl() != d['foruri']:
45 return "There was a server redirect, please click on the <a href='http://y.a11y.in/web?foruri={0}'>link</a> to continue.".format(quote_plus(a.geturl()))
49 return ("There was a server redirect, please click on the" +
50 " <a href='http://y.a11y.in/web?foruri={0}'>link</a> to" +
51 " continue.".format(quote_plus(a.geturl())))
46 else:52 else:
47 page = a.read()53 page = a.read()
48 a.close()54 a.close()
49 except ValueError:55 except ValueError:
50 return "The link is malformed, click <a href='http://y.a11y.in/web?foruri={0}&lang={1}&interactive=1'>here</a> to be redirected.".format(quote_plus(unquote_plus(d['foruri'].encode('utf-8'))),request.args['lang'])
56 return ("The link is malformed, click " +
57 "<a href='http://y.a11y.in/web?foruri={0}&lang={1}" +
58 "&interactive=1'>" +
59 "here</a> to be redirected.".format(
60 quote_plus(unquote_plus(d['foruri'].encode('utf-8'))),
61 request.args['lang']))
51 except urllib2.URLError:62 except urllib2.URLError:
52 return render_template('error.html')63 return render_template('error.html')
53 try:64 try:
54 page = unicode(page,'utf-8') #Hack to fix improperly displayed chars on wikipedia.
65 page = unicode(page, 'utf-8') # Hack to fix improperly displayed chars on wikipedia.
55 except UnicodeDecodeError:66 except UnicodeDecodeError:
56 pass #Some pages may not need be utf-8'ed
67 pass # Some pages may not need be utf-8'ed
57 try:68 try:
58 g.root = lxml.html.parse(StringIO.StringIO(page)).getroot()69 g.root = lxml.html.parse(StringIO.StringIO(page)).getroot()
59 except ValueError:70 except ValueError:
60 g.root = lxml.html.parse(d['foruri']).getroot() #Sometimes creators of the page lie about the encoding, thus leading to this execption. http://lxml.de/parsing.html#python-unicode-strings
71 g.root = lxml.html.parse(d['foruri']).getroot() # Sometimes creators of the page lie about the encoding, thus leading to this execption. http://lxml.de/parsing.html#python-unicode-strings
61 if request.args.has_key('lang') == False and request.args.has_key('blog') == False:72 if request.args.has_key('lang') == False and request.args.has_key('blog') == False:
62 g.root.make_links_absolute(d['foruri'], resolve_base_href = True)
73 g.root.make_links_absolute(d['foruri'],
74 resolve_base_href=True)
63 for i in g.root.iterlinks():75 for i in g.root.iterlinks():
64 if i[1] == 'href' and i[0].tag != 'link':76 if i[1] == 'href' and i[0].tag != 'link':
65 try:77 try:
66 i[0].attrib['href'] = 'http://{0}?foruri={1}'.format(conf.DEPLOYURL[0],quote_plus(i[0].attrib['href']))
78 i[0].attrib['href'] = 'http://{0}?foruri={1}'.format(
79 conf.DEPLOYURL[0], quote_plus(i[0].attrib['href']))
67 except KeyError:80 except KeyError:
68 i[0].attrib['href'] = '{0}?foruri={1}'.format(conf.DEPLOYURL[0],quote_plus(i[0].attrib['href'].encode('utf-8')))
81 i[0].attrib['href'] = '{0}?foruri={1}'.format(
82 conf.DEPLOYURL[0], quote_plus(
83 i[0].attrib['href'].encode('utf-8')))
69 setScripts()84 setScripts()
70 g.root.body.set("onload","a11ypi.loadOverlay();")
85 g.root.body.set("onload", "a11ypi.loadOverlay();")
71 response = make_response()86 response = make_response()
72 response.data = lxml.html.tostring(g.root)87 response.data = lxml.html.tostring(g.root)
73 return response88 return response
90 elif request.args.has_key('lang') == True and request.args.has_key('interactive') == True and request.args.has_key('blog') == False:90 elif request.args.has_key('lang') == True and request.args.has_key('interactive') == True and request.args.has_key('blog') == False:
91 setScripts()91 setScripts()
92 setSocialScript()92 setSocialScript()
93 g.root.body.set("onload","a11ypi.ren();a11ypi.tweet(); a11ypi.facebook(); a11ypi.loadOverlay();")
94 g.root.make_links_absolute(d['foruri'], resolve_base_href = True)
93 g.root.body.set("onload", "a11ypi.ren();a11ypi.tweet(); " +
94 "a11ypi.facebook(); a11ypi.loadOverlay();")
95 g.root.make_links_absolute(d['foruri'], resolve_base_href=True)
95 response = make_response()96 response = make_response()
96 response.data = lxml.html.tostring(g.root)97 response.data = lxml.html.tostring(g.root)
97 return response98 return response
9899
99
100 elif request.args.has_key('lang') == True and request.args.has_key('blog') == False:100 elif request.args.has_key('lang') == True and request.args.has_key('blog') == False:
101 script_jq_mini = g.root.makeelement('script')101 script_jq_mini = g.root.makeelement('script')
102 g.root.body.append(script_jq_mini)102 g.root.body.append(script_jq_mini)
107 g.root.body.append(script_test)107 g.root.body.append(script_test)
108 script_test.set("src", conf.APPURL[0] + "/alipi/ui.js")108 script_test.set("src", conf.APPURL[0] + "/alipi/ui.js")
109 script_test.set("type", "text/javascript")109 script_test.set("type", "text/javascript")
110 g.root.body.set("onload","a11ypi.ren()");
110 g.root.body.set("onload", "a11ypi.ren()")
111 response = make_response()111 response = make_response()
112 response.data = lxml.html.tostring(g.root)112 response.data = lxml.html.tostring(g.root)
113 return response113 return response
114114
115
116 elif request.args.has_key('interactive') == True and request.args.has_key('blog') == True and request.args.has_key('lang') == True:115 elif request.args.has_key('interactive') == True and request.args.has_key('blog') == True and request.args.has_key('lang') == True:
117 setScripts()116 setScripts()
118 setSocialScript()117 setSocialScript()
119 g.root.body.set("onload","a11ypi.filter(); a11ypi.tweet(); a11ypi.facebook(); a11ypi.loadOverlay();");
120 g.root.make_links_absolute(d['foruri'], resolve_base_href = True)
118 g.root.body.set("onload", "a11ypi.filter(); a11ypi.tweet();" +
119 "a11ypi.facebook(); a11ypi.loadOverlay();")
120 g.root.make_links_absolute(d['foruri'], resolve_base_href=True)
121 response = make_response()121 response = make_response()
122 response.data = lxml.html.tostring(g.root)122 response.data = lxml.html.tostring(g.root)
123 return response123 return response
124124
125 elif request.args.has_key('interactive') == False and request.args.has_key('blog') == True:125 elif request.args.has_key('interactive') == False and request.args.has_key('blog') == True:
126 setScripts()126 setScripts()
127 g.root.make_links_absolute(d['foruri'], resolve_base_href = True)
127 g.root.make_links_absolute(d['foruri'], resolve_base_href=True)
128 g.root.body.set('onload', 'a11ypi.loadOverlay();')128 g.root.body.set('onload', 'a11ypi.loadOverlay();')
129 response = make_response()129 response = make_response()
130 response.data = lxml.html.tostring(g.root)130 response.data = lxml.html.tostring(g.root)
143143
144 style = g.root.makeelement('link')144 style = g.root.makeelement('link')
145 g.root.body.append(style)145 g.root.body.append(style)
146 style.set("rel","stylesheet")
146 style.set("rel", "stylesheet")
147 style.set("type", "text/css")147 style.set("type", "text/css")
148 style.set("href", conf.APPURL[0] + "/alipi/pack.min.css")148 style.set("href", conf.APPURL[0] + "/alipi/pack.min.css")
149149
154 info_button.set("id", "info")154 info_button.set("id", "info")
155 info_button.set("class", "alipi")155 info_button.set("class", "alipi")
156 info_button.set("onClick", "a11ypi.showInfo(a11ypi.responseJSON);")156 info_button.set("onClick", "a11ypi.showInfo(a11ypi.responseJSON);")
157 info_button.text = "Info"
158 info_button.set("title", "Have a look at the information of each renarrated element")
157 info_button.text = "Info"
158 info_button.set("title", "Have a look at the information of each" +
159 " renarrated element")
159160
160 share_button = g.root.makeelement('button')161 share_button = g.root.makeelement('button')
161 g.root.body.append(share_button)162 g.root.body.append(share_button)
162 share_button.set("id", "share")163 share_button.set("id", "share")
163 share_button.set("class", "alipi")164 share_button.set("class", "alipi")
164 share_button.set("onClick", "a11ypi.share();")165 share_button.set("onClick", "a11ypi.share();")
165 share_button.text = "Share"
166 share_button.text = "Share"
166 share_button.set("title", "Share your contribution in your social network")167 share_button.set("title", "Share your contribution in your social network")
167168
168 see_orig = g.root.makeelement('button')169 see_orig = g.root.makeelement('button')
172 see_orig.set("class", "alipi")172 see_orig.set("class", "alipi")
173 see_orig.set("onClick", "a11ypi.showOriginal();")173 see_orig.set("onClick", "a11ypi.showOriginal();")
174 see_orig.text = "Original Page"174 see_orig.text = "Original Page"
175 see_orig.set("title", "Go to Original link, the original page of this renarrated")
175 see_orig.set("title", "Go to Original link, the original page of this" +
176 " renarrated page")
176177
177 tweetroot = g.root.makeelement("div")178 tweetroot = g.root.makeelement("div")
178 tweetroot.set("id", "tweet-root")179 tweetroot.set("id", "tweet-root")
187 tweet.set("class", "alipi twitter-share-button")187 tweet.set("class", "alipi twitter-share-button")
188 tweet.set("data-via", "a11ypi")188 tweet.set("data-via", "a11ypi")
189 tweet.set("data-lang", "en")189 tweet.set("data-lang", "en")
190 tweet.set("data-url", "http://y.a11y.in/web?foruri={0}&lang={1}&interactive=1".format(quote_plus(request.args['foruri']),(request.args['lang']).encode('unicode-escape')))
190 tweet.set("data-url", "http://y.a11y.in/web?foruri={0}&lang={1}&" +
191 "interactive=1".format(quote_plus(request.args['foruri']),
192 (request.args['lang']).encode(
193 'unicode-escape')))
191 tweet.textContent = "Tweet"194 tweet.textContent = "Tweet"
192 tweetroot.append(tweet)195 tweetroot.append(tweet)
193196
198 fblike.set("id", "fb-like")198 fblike.set("id", "fb-like")
199 fblike.set("class", "alipi fb-like")199 fblike.set("class", "alipi fb-like")
200 fblike.set("style", "display:none;padding:10px;")200 fblike.set("style", "display:none;padding:10px;")
201 fblike.set("data-href", "http://y.a11y.in/web?foruri={0}&lang={1}&interactive=1".format(quote_plus(request.args['foruri']),(request.args['lang']).encode('unicode-escape')))
201 fblike.set("data-href", "http://y.a11y.in/web?foruri={0}&lang={1}&" +
202 "interactive=1".format(quote_plus(request.args['foruri']),
203 (request.args['lang']).encode(
204 'unicode-escape')))
202 fblike.set("data-send", "true")205 fblike.set("data-send", "true")
203 fblike.set("data-layout", "button_count")206 fblike.set("data-layout", "button_count")
204 fblike.set("data-width", "50")207 fblike.set("data-width", "50")
211211
212 style = g.root.makeelement('link')212 style = g.root.makeelement('link')
213 g.root.body.append(style)213 g.root.body.append(style)
214 style.set("rel","stylesheet")
214 style.set("rel", "stylesheet")
215 style.set("type", "text/css")215 style.set("type", "text/css")
216 style.set("href", "http://y.a11y.in/alipi/stylesheet.css")216 style.set("href", "http://y.a11y.in/alipi/stylesheet.css")
217217
220def show_directory():220def show_directory():
221 collection = g.db['post']221 collection = g.db['post']
222 query = collection.group(222 query = collection.group(
223 key = Code('function(doc){return {"about" : doc.about,"lang":doc.lang}}'),
224 condition={"about":{'$regex':'^[/\S/]'}},
223 key=Code('function(doc){' +
224 'return {"about" : doc.about,"lang":doc.lang}}'),
225 condition={"about": {'$regex': '^[/\S/]'}},
225 initial={'na': []},226 initial={'na': []},
226 reduce=Code('function(doc,out){out.na.push(doc.blog)}')227 reduce=Code('function(doc,out){out.na.push(doc.blog)}')
227 )228 )
228 query.reverse()229 query.reverse()
229 return render_template('directory.html', name=query, mymodule = quote_plus, myset=set, mylist= list)
230 return render_template('directory.html', name=query, mymodule=quote_plus,
231 myset=set, mylist=list)
230232
233
231@app.route('/getLoc', methods=['GET'])234@app.route('/getLoc', methods=['GET'])
232def get_loc():235def get_loc():
233236
234 term = request.args['term']237 term = request.args['term']
235 connection = oursql.Connection(conf.DBHOST[0],conf.DBUSRNAME[0],conf.DBPASSWD[0],db=conf.DBNAME[0])
238 connection = oursql.Connection(conf.DBHOST[0], conf.DBUSRNAME[0],
239 conf.DBPASSWD[0], db=conf.DBNAME[0])
236 cursor = connection.cursor(oursql.DictCursor)240 cursor = connection.cursor(oursql.DictCursor)
237 cursor.execute('select l.name, c.country_name from `location` as l, `codes` as c where l.name like ? and l.code=c.code limit ?', (term+'%', 5))
241 cursor.execute('select l.name, c.country_name from `location` as l, ' +
242 ' `codes` as c where l.name like ? and l.code=c.code' +
243 ' limit ?', (term+'%', 5))
238 r = cursor.fetchall()244 r = cursor.fetchall()
239 connection.close()245 connection.close()
240 d = {}246 d = {}
248 response = jsonify(d)248 response = jsonify(d)
249 response.headers['Access-Control-Allow-Origin'] = '*'249 response.headers['Access-Control-Allow-Origin'] = '*'
250 return response250 return response
251
252
251@app.route('/getLang', methods=['GET'])253@app.route('/getLang', methods=['GET'])
252def get_lang():254def get_lang():
253 term = request.args['term']255 term = request.args['term']
254 connection = oursql.Connection(conf.DBHOST[0],conf.DBUSRNAME[0],conf.DBPASSWD[0],db=conf.DBNAME[0])
256 connection = oursql.Connection(conf.DBHOST[0], conf.DBUSRNAME[0],
257 conf.DBPASSWD[0], db=conf.DBNAME[0])
255 cursor = connection.cursor(oursql.DictCursor)258 cursor = connection.cursor(oursql.DictCursor)
256 cursor.execute('select * from `languages` as l where l.name like ? limit ?', (term+'%',5))
259 cursor.execute('select * from `languages` as l where l.name like' +
260 ' ? limit ?', (term+'%', 5))
257 r = cursor.fetchall()261 r = cursor.fetchall()
258 connection.close()262 connection.close()
259 d = {}263 d = {}
266 response.headers['Access-Control-Allow-Origin'] = '*'266 response.headers['Access-Control-Allow-Origin'] = '*'
267 return response267 return response
268268
269
269@app.route('/blank', methods=['GET'])270@app.route('/blank', methods=['GET'])
270def serve_blank():271def serve_blank():
271 return render_template('blank.html')272 return render_template('blank.html')
272273
274
273@app.route('/info', methods=['GET'])275@app.route('/info', methods=['GET'])
274def serve_info():276def serve_info():
275 coll = g.db['post']277 coll = g.db['post']
276 d = {}278 d = {}
277 cntr = 0279 cntr = 0
278 for i in coll.find({"about":unquote_plus(request.args['about']),"lang":request.args['lang']}):
280 for i in coll.find({"about": unquote_plus(request.args['about']),
281 "lang": request.args['lang']}):
279 i['_id'] = str(i['_id'])282 i['_id'] = str(i['_id'])
280 d[cntr] = i283 d[cntr] = i
281 cntr+=1
284 cntr += 1
282 response = jsonify(d)285 response = jsonify(d)
283 response.headers['Access-Control-Allow-Origin'] = '*'286 response.headers['Access-Control-Allow-Origin'] = '*'
284 return response287 return response
293 lang = request.args['lang']293 lang = request.args['lang']
294 url = request.args['url']294 url = request.args['url']
295 query = collection.group(295 query = collection.group(
296 key = Code('function(doc){return {"xpath" : doc.xpath, "about": doc.url}}'),
297 condition={"about" : url, "lang" : lang,"elementtype":"text"},
296 key=Code('function(doc){' +
297 'return {"xpath": doc.xpath, "about": doc.url}}'),
298 condition={"about": url, "lang": lang, "elementtype": "text"},
298 initial={'narration': []},299 initial={'narration': []},
299 reduce=Code('function(doc,out){out.narration.push(doc);}')300 reduce=Code('function(doc,out){out.narration.push(doc);}')
300 )301 )
301302
302 print query303 print query
303304
304 audio_query =collection.group(
305 key = Code('function(doc){return {"xpath" : doc.xpath, "about": doc.url}}'),
306 condition={"about" : url, "lang" : lang, 'elementtype':"audio/ogg"},
305 audio_query = collection.group(
306 key=Code('function(doc){' +
307 'return {"xpath": doc.xpath, "about": doc.url}}'),
308 condition={"about": url, "lang": lang, 'elementtype': "audio/ogg"},
307 initial={'narration': []},309 initial={'narration': []},
308 reduce=Code('function(doc,out){out.narration.push(doc);}')310 reduce=Code('function(doc,out){out.narration.push(doc);}')
309 )311 )
310312
311 image_query =collection.group(
312 key = Code('function(doc){return {"xpath" : doc.xpath, "about": doc.url}}'),
313 condition={"about" : url, "lang" : lang, 'elementtype':"image"},
313 image_query = collection.group(
314 key=Code('function(doc){' +
315 'return {"xpath": doc.xpath, "about": doc.url}}'),
316 condition={"about": url, "lang": lang, 'elementtype': "image"},
314 initial={'narration': []},317 initial={'narration': []},
315 reduce=Code('function(doc,out){out.narration.push(doc);}')318 reduce=Code('function(doc,out){out.narration.push(doc);}')
316 )319 )
337 response.headers['Access-Control-Allow-Origin'] = '*'337 response.headers['Access-Control-Allow-Origin'] = '*'
338 return response338 return response
339339
340
340@app.route('/feeds', methods=['GET'])341@app.route('/feeds', methods=['GET'])
341def serve_feed_temp():342def serve_feed_temp():
342 return render_template("feeds.html")343 return render_template("feeds.html")
343344
345
344@app.route('/feed', methods=['GET'])346@app.route('/feed', methods=['GET'])
345def serve_feed():347def serve_feed():
346 coll = g.db['post']348 coll = g.db['post']
347 d = {}349 d = {}
348 cntr = 0350 cntr = 0
349 for i in coll.find().sort('_id',direction=-1):
351 for i in coll.find().sort('_id', direction=-1):
350 if i['data'] != '<br/>':352 if i['data'] != '<br/>':
351 i['_id'] = str(i['_id'])353 i['_id'] = str(i['_id'])
352 d[cntr] = i354 d[cntr] = i
353 cntr+=1
355 cntr += 1
354 response = jsonify(d)356 response = jsonify(d)
355 response.headers['Access-Control-Allow-Origin'] = '*'357 response.headers['Access-Control-Allow-Origin'] = '*'
356 return response358 return response
357359
360
358@app.route('/about', methods=['GET'])361@app.route('/about', methods=['GET'])
359def serve_authors():362def serve_authors():
360 coll = g.db['post']363 coll = g.db['post']
361 d = {}364 d = {}
362 cntr = 0365 cntr = 0
363 for i in coll.find({"about":unquote_plus(request.args['about'])}):
366 for i in coll.find({"about": unquote_plus(request.args['about'])}):
364 i['_id'] = str(i['_id'])367 i['_id'] = str(i['_id'])
365 d[cntr] = i368 d[cntr] = i
366 cntr+=1
369 cntr += 1
367 response = jsonify(d)370 response = jsonify(d)
368 response.headers['Access-Control-Allow-Origin'] = '*'371 response.headers['Access-Control-Allow-Origin'] = '*'
369 return response372 return response
373
374
370#Retrieve all information about a specific $about and a given $author.375#Retrieve all information about a specific $about and a given $author.
371@app.route('/author', methods=['GET'])376@app.route('/author', methods=['GET'])
372def serve_author():377def serve_author():
373 coll = g.db['post']378 coll = g.db['post']
374 d = {}379 d = {}
375 cntr = 0380 cntr = 0
376 for i in coll.find({"about":unquote_plus(request.args['about']),"author":unquote_plus(request.args['author'])}):
381 for i in coll.find({"about": unquote_plus(request.args['about']),
382 "author": unquote_plus(request.args['author'])}):
377 i['_id'] = str(i['_id'])383 i['_id'] = str(i['_id'])
378 d[cntr] = i384 d[cntr] = i
379 cntr += 1385 cntr += 1
387 response.headers['Access-Control-Allow-Origin'] = '*'387 response.headers['Access-Control-Allow-Origin'] = '*'
388 return response388 return response
389389
390
390@app.route('/getAllLang', methods=['GET'])391@app.route('/getAllLang', methods=['GET'])
391def get_all_lang():392def get_all_lang():
392 term = request.args['term']393 term = request.args['term']
393 connection = oursql.Connection(conf.DBHOST[0],conf.DBUSRNAME[0],conf.DBPASSWD[0],db=conf.DBNAME[0])
394 connection = oursql.Connection(conf.DBHOST[0], conf.DBUSRNAME[0],
395 conf.DBPASSWD[0], db=conf.DBNAME[0])
394 cursor = connection.cursor(oursql.DictCursor)396 cursor = connection.cursor(oursql.DictCursor)
395 cursor.execute('select * from `languages` as l where l.name like ?', (term+'%',))
397 cursor.execute('select * from `languages` as l where l.name like ?',
398 (term+'%',))
396 r = cursor.fetchall()399 r = cursor.fetchall()
397 connection.close()400 connection.close()
398 d = {}401 d = {}
410 data = json.loads(request.form['data'])410 data = json.loads(request.form['data'])
411 collection = g.db['post']411 collection = g.db['post']
412 page = {}412 page = {}
413 if type(data) is unicode: #A hack to fix malformed data. FIXME.
413 if type(data) is unicode: # A hack to fix malformed data. FIXME.
414 data = json.loads(data)414 data = json.loads(data)
415 content = []415 content = []
416 for i in data: #Create content objects here for posting to blog. DELETEME.
416 for i in data:
417 # Create content objects here for posting to blog. DELETEME.
417 if 'comments' in i:418 if 'comments' in i:
418 page['comments'] = i['comments']419 page['comments'] = i['comments']
419 else:420 else:
420 contentobj = {}421 contentobj = {}
421 contentobj['type'] = i['elementtype']422 contentobj['type'] = i['elementtype']
422 contentobj['attr'] = {"language":i['lang'], "location":i['location'], "about":i['about'], "xpath":i['xpath']}
423 contentobj['attr'] = {"language": i['lang'],
424 "location": i['location'],
425 "about": i['about'],
426 "xpath": i['xpath']}
423 contentobj['data'] = i['data']427 contentobj['data'] = i['data']
424 content.append(contentobj)428 content.append(contentobj)
425 i['bxpath'] = ''429 i['bxpath'] = ''
433 page['name'] = "About " + content[0]['attr']['about']433 page['name'] = "About " + content[0]['attr']['about']
434 page['content'] = content434 page['content'] = content
435435
436 g.response_from_blogger = requests.api.post(conf.CUSTOM_BLOG_POST_URL[0], json.dumps(page), headers={"content-type":"application/json"})
436 g.response_from_blogger = requests.api.post(conf.CUSTOM_BLOG_POST_URL[0],
437 json.dumps(page),
438 headers={"content-type":
439 "application/json"})
437 print "response from blogger " + repr(g.response_from_blogger)440 print "response from blogger " + repr(g.response_from_blogger)
438 sweet(data)441 sweet(data)
439 reply = make_response()442 reply = make_response()
444444
445445
446def sweet(data):446def sweet(data):
447 """ A function to sweet the data that is inserted. Accepts a <list of dicts>. """
447 """ A function to sweet the data that is inserted.
448 Accepts a <list of dicts>. """
448 for i in data:449 for i in data:
449 if 'type' in i:450 if 'type' in i:
450 del(i['_id'])451 del(i['_id'])
451 sweetmaker.sweet(conf.SWEET_STORE_ADD[0], [{"what":i['type'], "who":i['author'], "where":i['about']+i['xpath'], "how":conf.CUSTOM_BLOG_URL[0]+"/#"+g.response_from_blogger.json()['name']+' {lang: '+i["lang"]+',loc: '+i["location"]+'}'}])
452 sweetmaker.sweet(conf.SWEET_STORE_ADD[0],
453 [{"what": i['type'],
454 "who": i['author'],
455 "where":i['about']+i['xpath'],
456 "how":{'blogUrl': '{0}/#{1}'.format(
457 conf.CUSTOM_BLOG_URL[0],
458 g.response_from_blogger.json()['name']),
459 'language': i['lang'],
460 'location': i['location']}}])
452 return True461 return True
453 # data = json.dumps(data)462 # data = json.dumps(data)
454 # req = requests.api.post(conf.SWEETURL[0]+"/add",{'data':data})463 # req = requests.api.post(conf.SWEETURL[0]+"/add",{'data':data})
479 reply = make_response()479 reply = make_response()
480 return reply480 return reply
481481
482@app.route("/menu",methods=['GET'])
482
483@app.route("/menu", methods=['GET'])
483def menuForDialog():484def menuForDialog():
484 if request.args.has_key('option') == False:485 if request.args.has_key('option') == False:
485 collection = g.db['post']486 collection = g.db['post']
486 c = {}487 c = {}
487 cntr = 0488 cntr = 0
488 print request.args['url']489 print request.args['url']
489 for i in collection.find({"about":request.args['url']}).distinct('lang'):
490 for j in collection.find({"about":request.args['url'],'lang':i}).distinct('type'):
490 for i in collection.find({"about":
491 request.args['url']}).distinct('lang'):
492 for j in collection.find({"about": request.args['url'],
493 'lang': i}).distinct('type'):
491 d = {}494 d = {}
492 d['lang'] = i495 d['lang'] = i
493 d['type'] = j496 d['type'] = j
502 collection = g.db['post']502 collection = g.db['post']
503 #get the ren languages for the received url503 #get the ren languages for the received url
504 langForUrl = collection.group(504 langForUrl = collection.group(
505 key = Code('function(doc){return {"about" : doc.about}}'),
506 condition={"about" : d['url'],"blog":{'$regex':'/'+d['option']+'.*/'}},
505 key=Code('function(doc){return {"about" : doc.about}}'),
506 condition={"about": d['url'], "blog": {'$regex':
507 '/'+d['option']+'.*/'}},
507 initial={'lang': []},508 initial={'lang': []},
508 reduce=Code('function(doc, out){if (out.lang.indexOf(doc.lang) == -1) out.lang.push(doc.lang)}') #here xpath for test
509 reduce=Code('function(doc, out){' +
510 'if (out.lang.indexOf(doc.lang) == -1)' +
511 'out.lang.push(doc.lang)}') # here xpath for test
509 )512 )
510513
511 #send the response514 #send the response
526 url = request.args['url']526 url = request.args['url']
527 #all re-narrations of the same xpath are grouped527 #all re-narrations of the same xpath are grouped
528 query = collection.group(528 query = collection.group(
529 key = None,
530 condition={"about" :{'$regex':url+'*'}},
529 key=None,
530 condition={"about": {'$regex': url+'*'}},
531 initial={'narration': []},531 initial={'narration': []},
532 reduce=Code('function(doc,out){out.narration.push(doc["about"]);}')532 reduce=Code('function(doc,out){out.narration.push(doc["about"]);}')
533 )533 )
534534
535 string=''
536 if len(query)==0:
537 return jsonify({'0':'empty'})
535 string = ''
536 if len(query) == 0:
537 return jsonify({'0': 'empty'})
538 else:538 else:
539 otherlist = {}539 otherlist = {}
540 cntr = -1540 cntr = -1
548 return jsonify(otherlist)548 return jsonify(otherlist)
549549
550550
551import logging,os
551import logging
552import os
552from logging import FileHandler553from logging import FileHandler
553554
554fil = FileHandler(os.path.join(os.path.dirname(__file__),'logme'),mode='a')
555fil = FileHandler(os.path.join(os.path.dirname(__file__), 'logme'), mode='a')
555fil.setLevel(logging.ERROR)556fil.setLevel(logging.ERROR)
556app.logger.addHandler(fil)557app.logger.addHandler(fil)
557558