From 3a2f8347c150a3605ef627fba9c20c5e7a6d31e8 Mon Sep 17 00:00:00 2001 From: Anon Ray Date: Tue, 9 Jul 2013 15:01:01 +0530 Subject: [PATCH] Many changes - now the app sweets to sweet store - also displays related web annotations on the web view --- .gitignore | 3 +- conf.py.sample | 1 + server.py | 69 ++++++++++++++++++++++++++++++++------------ static/myVector.js | 2 +- static/text-annotation.css | 1 + static/text-annotation.js | 26 ++++++++++++++++- static/tree.js | 43 ++++++++------------------- sweetmaker.py | 36 +++++++++++++++++++++++ templates/blank.html | 4 +-- templates/web.html | 6 ++-- 10 files changed, 133 insertions(+), 58 deletions(-) create mode 100644 conf.py.sample create mode 100644 sweetmaker.py diff --git a/.gitignore b/.gitignore index c76e001..3d8e3ae 100755 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.png *.*~ -*.xml \ No newline at end of file +*.xml +conf.py diff --git a/conf.py.sample b/conf.py.sample new file mode 100644 index 0000000..f85ec4c --- /dev/null +++ b/conf.py.sample @@ -0,0 +1 @@ +SWEET_STORE_URL = 'your sweet store url here' diff --git a/server.py b/server.py index 2a1144b..502dcca 100644 --- a/server.py +++ b/server.py @@ -13,8 +13,13 @@ import StringIO import json import urllib +import conf +import sweetmaker + app = Flask(__name__) +SWEET_STORE_URL = conf.SWEET_STORE_URL + @app.route('/', methods=['GET']) def index(): @@ -60,15 +65,32 @@ def search(): ret = {} keywords_dict = json.loads(request.args['data']) #keywords = json.loads(keywords_dict)['data'] - for i in collection.find(): - for keyword in keywords_dict: + if 'nodes' in keywords_dict: + for i in collection.find(): try: - if keyword in i['nodes']: - del(i['_id']) - ret[y] = i - y = y + 1 + if 'how' in i: + i['nodes'] = i['how'] except: pass + for node in keywords_dict['nodes']: + try: + if node in i['nodes']: + del(i['_id']) + ret[y] = i + y = y + 1 + except: + pass + elif 'where' in keywords_dict: + for i in collection.find({'uri': keywords_dict['where']}): + del(i['_id']) + ret[y] = i + y = y + 1 + for i in collection.find({'where': {'$regex':\ + keywords_dict['where']}}): + del(i['_id']) + ret[y] = i + y = y + 1 + return render_template('blank.html', content = ret) @@ -81,16 +103,25 @@ def submit(): try: for i in requestData: coll.insert(i) + print 'inserted' + print requestData response = make_response() response.headers['Access-Control-Allow-Origin'] = '*' - response.status = '200 OK' response.status_code = 200 - return response + for i in requestData: + del(i['_id']) + i['how'] = '{concepts: ' + ', '.join(i['how']) + '}' + #i['how'] = attribs + print 'payload for sweet' + print requestData + sweetmaker.sweet(SWEET_STORE_URL, requestData) except: response = make_response() - response.status = "500" + response.status_code = 500 response.data = "Your post could not be saved. Try posting again." - return response + + return response + @app.route('/web/', methods=['GET']) def web(): @@ -116,35 +147,35 @@ def SWeeText(): # inject the JS toolbar to annotate text jq = root.makeelement('script') - jq.set('src', 'static/jquery-1.9.1.min.js') + jq.set('src', '/static/jquery-1.9.1.min.js') script = root.makeelement('script') - script.set('src', 'static/text-annotation.js') + script.set('src', '/static/text-annotation.js') tree = root.makeelement('script') - tree.set('src', 'static/tree.js') + tree.set('src', '/static/tree.js') bs_js = root.makeelement('script') - bs_js.set('src', 'static/bootstrap.js') + bs_js.set('src', '/static/bootstrap.js') jit = root.makeelement('script') - jit.set('src', 'static/jit.js') + jit.set('src', '/static/jit.js') us = root.makeelement('script') - us.set('src', 'static/underscore-min-1.4.4.js') + us.set('src', '/static/underscore-min-1.4.4.js') link = root.makeelement('link') - link.set('href', 'static/text-annotation.css') + link.set('href', '/static/text-annotation.css') link.set('type', 'text/css') link.set('rel', 'stylesheet') bs = root.makeelement('link') - bs.set('href', 'static/bootstrap.css') + bs.set('href', '/static/bootstrap.css') bs.set('type', 'text/css') bs.set('rel', 'stylesheet') tree_css = root.makeelement('link') - tree_css.set('href', 'static/tree.css') + tree_css.set('href', '/static/tree.css') tree_css.set('type', 'text/css') tree_css.set('rel', 'stylesheet') diff --git a/static/myVector.js b/static/myVector.js index a574819..5b7ac62 100644 --- a/static/myVector.js +++ b/static/myVector.js @@ -77,7 +77,7 @@ function onFeatureSelect(feature) "test", feature.geometry.getBounds().getCenterLonLat(), new OpenLayers.Size(640,480), - '",null,true); feature.popup = z; z.panMapIfOutOfView = true; map.addPopup(z); diff --git a/static/text-annotation.css b/static/text-annotation.css index 600e4f8..5509780 100644 --- a/static/text-annotation.css +++ b/static/text-annotation.css @@ -14,3 +14,4 @@ width: 700px; box-shadow: 3px 3px 3px #468847; } + diff --git a/static/text-annotation.js b/static/text-annotation.js index 5345a88..ba0addb 100644 --- a/static/text-annotation.js +++ b/static/text-annotation.js @@ -35,6 +35,17 @@ var attributes = {}; //$(document).mouseover(onHover); //$(document).mouseout(onHoverOut); }; + window.listAnno = function() { // list annotations of current page + console.log('listing annotations'); + $.get(config.indexer + '/search?data=' + + JSON.stringify({'where': window.location.search.split('url=')[1]}), + function(data) { + //console.log(data); + $('#list-anno .modal-body').html(data); + $('#list-anno').modal(); + }); + }; + var removeSelect = function() { var nodes = document.getElementsByTagName('*'); var bar = document.getElementById('annotate-bar'); @@ -56,9 +67,22 @@ var attributes = {}; '
  • '+ '
  • ' + '
  • '+ + '
  • ' + + '
  • '+ '
  • ' + '' + - '
    '; + '
    '+ + ''; }; var modal_template = function() { diff --git a/static/tree.js b/static/tree.js index 366a621..9a99e7b 100644 --- a/static/tree.js +++ b/static/tree.js @@ -162,23 +162,24 @@ var sweet = { window.location.href; resource = decodeURIComponent(resource).replace('"', '', 'gi'); var data = { - user: user, - type: this.type, - uri: resource, - nodes: this.nodes, + who: user, + what: this.type, + where: resource, + how: this.nodes, name: attribs.name }; if(attribs.hasOwnProperty('top') && attribs.hasOwnProperty('bottom') && attribs.hasOwnProperty('right') && attribs.hasOwnProperty('left')) { - data.top = attribs.top; - data.bottom = attribs.bottom; - data.left = attribs.left; - data.right = attribs.right; + + data.where += '#[' + attribs.top + + ',' + attribs.bottom + + ',' + attribs.left + + ',' + attribs.right + ']'; } if(attribs.hasOwnProperty('xpath')) { - data.xpath = attribs.xpath; + data.where += '#' + attribs.xpath; } this.swts.push(data); this.nodes = []; @@ -193,32 +194,12 @@ var sweet = { url: config.indexer + '/submit', data: {'data': JSON.stringify(this.swts)}, success: function() { - /*$.ajax({ - type: 'POST', - url: config.postTweetUrl, - data: {'data': JSON.stringify(this.swts)}, - success: function() { - $('#posted').show(); - this.swts = []; - }, - error: function() { - $('#fail-posting').show(); - } - });*/ - //$('#posted').html(posted_template()); - //$('#posted').show(); var swts = ''; for(var i in sweet.swts) { var data = sweet.swts[i]; console.log(data); - var swt = '@'+data.user+' '+data.type+' '+data.uri; - if(data.hasOwnProperty('xpath')) { - swt += ' xpath: '+data.xpath; - } - if(data.hasOwnProperty('top')) { - swt += ' #['+data.top+','+data.right+','+data.bottom+','+data.left+']'; - } - swt += ' ' + data.nodes.join(); + var swt = '@'+data.who+' #'+data.what+' / '+data.where; + swt += ' {' + data.how.join() + ' }'; swts += swt + '\n'; } console.log(swts); diff --git a/sweetmaker.py b/sweetmaker.py new file mode 100644 index 0000000..262661b --- /dev/null +++ b/sweetmaker.py @@ -0,0 +1,36 @@ +# SwtMaker +# ------- +# Server-side component to make sweets and post them to specified +# sweet store +# +# License: BSD, see LICENSE for more details. +# Servelots 2013 +# Authors: +# Arvind Khadri +# Anon Ray + +import requests +import json +from datetime import datetime + +TIMESTAMP_FORMAT = '%d-%m-%Y %H:%M:%S' + +def sweet(sweet_url, sweet_list): + sweets = makeSweet(sweet_list) + if not sweets: + return False + else: + request = requests.api.post(sweet_url, {'data': json.dumps(sweets)}) + if request.status_code == 200: + return True + else: + return False + +def makeSweet(sweet_list): + for sweet in sweet_list: + if len(sweet['who']) and len(sweet['what']) and len(sweet['where'])\ + and len(sweet['how']): + sweet['created'] = datetime.utcnow().strftime(TIMESTAMP_FORMAT) + else: + return False + return sweet_list diff --git a/templates/blank.html b/templates/blank.html index 2732963..ac48999 100644 --- a/templates/blank.html +++ b/templates/blank.html @@ -1,7 +1,7 @@ {% for obj in content %} -
    +
    {% for key in content[obj].keys() %} -

    {{ key }}: {{ content[obj][key] }}

    +

    {{ key }}: {{ content[obj][key] }}

    {% endfor %}
    {% endfor %} diff --git a/templates/web.html b/templates/web.html index d8663ea..232c704 100644 --- a/templates/web.html +++ b/templates/web.html @@ -4,9 +4,9 @@ Web Annotation - - - + + +