diff --git a/app/controllers/api.py b/app/controllers/api.py index 50004b6..9069eff 100644 --- a/app/controllers/api.py +++ b/app/controllers/api.py @@ -11,17 +11,13 @@ from app.helpers.hashing import md5 logger = logging.getLogger(__name__) -@app.route("/comments", methods=['GET', 'POST']) +@app.route("/comments", methods=['GET']) def query_comments(): comments = [] try: - if request.method == 'POST': - token = request.json['token'] - url = request.json['url'] - else: - token = request.args.get('token', '') - url = request.args.get('url', '') + token = request.args.get('token', '') + url = request.args.get('url', '') logger.info('retrieve comments for token %s, url %s' % (token, url)) for comment in Comment.select(Comment).join(Site).where( @@ -44,3 +40,19 @@ def query_comments(): r = jsonify({'data': []}) r.status_code = 400 return r + + +@app.route("/comments/count", methods=['GET']) +def get_comments_count(): + try: + token = request.args.get('token', '') + url = request.args.get('url', '') + count = Comment.select(Comment).join(Site).where( + (Comment.url == url) & + (Site.token == token)).count() + r = jsonify({'count': count}) + r.status_code = 200 + except: + r = jsonify({'count': 0}) + r.status_code = 200 + return r diff --git a/demo/public/index.html b/demo/public/index.html index 81165bc..9eee8dc 100644 --- a/demo/public/index.html +++ b/demo/public/index.html @@ -35,11 +35,11 @@ + - - + @@ -92,7 +92,7 @@ instance d'ici peu.

- Commenter + Commenter
+ + + + +

+ + -
Chargement...
+ + + -
+ @@ -203,6 +194,23 @@ function Editor(input, preview) { + + + + + + + + diff --git a/demo/public/js/page.js b/demo/public/js/page.js new file mode 100644 index 0000000..f3acbab --- /dev/null +++ b/demo/public/js/page.js @@ -0,0 +1,33 @@ +function show_hide(panel_id, button_id){ + if (document.getElementById(panel_id).style.display == 'none'){ + document.getElementById(panel_id).style.display = ''; + document.getElementById(button_id).style.display = 'none'; + } else { + document.getElementById(panel_id).style.display = 'none'; + } +} + +function show_comments() { + stacosys_load(); + show_hide('stacosys-comments', 'show-comments-button'); +} + +function preview_markdown() { + if (document.getElementById('preview-container').style.display == 'none'){ + document.getElementById('preview-container').style.display = ''; + } + var $ = function (id) { return document.getElementById(id); }; + new Editor($("message"), $("preview")); +} + +function Editor(input, preview) { + this.update = function () { + preview.innerHTML = markdown.toHTML(input.value); + }; + input.editor = this; + this.update(); +} + +function get_action() { + return '/post_a_new_comment'; +} diff --git a/demo/public/js/stacosys.js b/demo/public/js/stacosys.js index 43068b0..24ea418 100644 --- a/demo/public/js/stacosys.js +++ b/demo/public/js/stacosys.js @@ -1,11 +1,6 @@ // Released under Apache license // Copyright (c) 2015 Yannic ARNOUX -STACOSYS_URL = 'http://127.0.0.1:8000'; -STACOSYS_TOKEN = '9fb3fc042c572cb831005fd16186126765140fa2bd9bb2d4a28e47a9457dc26c'; -//STACOSYS_PAGE = 'blogduyax.madyanne.fr/mes-applications-pour-blackberry.html' -STACOSYS_PAGE = 'blogduyax.madyanne.fr/migration-du-blog-sous-pelican.html' - // Create the XHR object. function stacosys_get_cors_request(method, url) { var xhr = new XMLHttpRequest(); @@ -23,12 +18,38 @@ function stacosys_get_cors_request(method, url) { return xhr; } -function stacosys_get_url() { - return STACOSYS_URL + '/comments?token=' + STACOSYS_TOKEN + '&url=' + STACOSYS_PAGE; +function stacosys_count() { + var url = STACOSYS_URL + '/comments/count?token=' + STACOSYS_TOKEN + '&url=' + STACOSYS_PAGE; + var xhr = stacosys_get_cors_request('GET', url); + if (!xhr) { + console.log('CORS not supported'); + return 0; + } + + // Response handlers. + xhr.onload = function() { + var jsonResponse = JSON.parse(xhr.responseText); + var count = jsonResponse.count; + if (count > 0) { + if (count > 1) { + document.getElementById('show-comment-label').innerHTML = 'Voir les ' + count + ' commentaires'; + } + document.getElementById('show-comments-button').style.display = ''; + } + return jsonResponse.count; + }; + + xhr.onerror = function() { + console.log('Woops, there was an error making the request.'); + return 0; + }; + + xhr.send(); } function stacosys_load() { - var url = stacosys_get_url(); + var url = STACOSYS_URL + '/comments?token=' + STACOSYS_TOKEN + '&url=' + STACOSYS_PAGE; + var xhr = stacosys_get_cors_request('GET', url); if (!xhr) { alert('CORS not supported'); @@ -52,4 +73,3 @@ function stacosys_load() { xhr.send(); } -window.onload = stacosys_load;