Add 'get count of comment' method
Draft JavaScript side
This commit is contained in:
parent
ac00f6ad33
commit
20b01caf86
4 changed files with 124 additions and 51 deletions
|
@ -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
|
||||
|
|
|
@ -35,11 +35,11 @@
|
|||
<meta name="msapplication-TileColor" content="#2d89ef">
|
||||
<meta name="msapplication-TileImage" content="/mstile-144x144.png">
|
||||
|
||||
<!-- === javascript dependencies === -->
|
||||
<script src="js/markdown.js"></script>
|
||||
|
||||
<!-- cosysnode dependencies -->
|
||||
<script src="js/mustache.js"></script>
|
||||
<script src="js/stacosys.js"></script>
|
||||
<script src="js/page.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
@ -92,7 +92,7 @@ instance d'ici peu.</p>
|
|||
</div>
|
||||
<div class="comment">
|
||||
<div id="submit-button">
|
||||
<a class="button-success pure-button" href="javascript:showHide('comment-form',%20'submit-button');">Commenter</a>
|
||||
<a class="button-success pure-button" href="javascript:show_hide('comment-form','submit-button');">Commenter</a>
|
||||
</div>
|
||||
|
||||
<div id="comment-form" style="display:none">
|
||||
|
@ -124,13 +124,31 @@ instance d'ici peu.</p>
|
|||
</form>
|
||||
</div>
|
||||
|
||||
<!-- ====================================================== -->
|
||||
<!-- Begin section : display comments -->
|
||||
<!-- ====================================================== -->
|
||||
|
||||
<p></p>
|
||||
<div id="show-comments-button" style="display:none">
|
||||
<a id="show-comment-label" class="button-success pure-button"
|
||||
href="javascript:show_comments();">Voir le commentaire</a>
|
||||
</div>
|
||||
|
||||
<!-- stacosys comments container -->
|
||||
<div id="stacosys-comments">Chargement...</div>
|
||||
<div id="stacosys-comments" style="display:none">Chargement...</div>
|
||||
|
||||
<!-- stacosys comments template -->
|
||||
<script id="stacosys-template" type="x-tmpl-mustache">
|
||||
{{#data}}
|
||||
<hr>
|
||||
<div class="inline">
|
||||
{{#site}}
|
||||
<a href="{{site}}">
|
||||
{{/site}}
|
||||
<img src="http://www.gravatar.com/avatar/{{avatar}}.jpg" style="float:left; margin-right:10px" height="32" width="32">
|
||||
{{#site}}
|
||||
</a>
|
||||
{{/site}}
|
||||
<span class="title">{{author}}</span>
|
||||
<span> - {{date}}</span>
|
||||
</div>
|
||||
|
@ -138,38 +156,11 @@ instance d'ici peu.</p>
|
|||
{{/data}}
|
||||
</script>
|
||||
|
||||
<script type="text/javascript"><!--
|
||||
<!-- ====================================================== -->
|
||||
<!-- End section : display comments -->
|
||||
<!-- ====================================================== -->
|
||||
|
||||
function showHide(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 get_action() {
|
||||
return '/post_a_new_comment';
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
--></script> </div>
|
||||
</div>
|
||||
</article>
|
||||
</section>
|
||||
</div>
|
||||
|
@ -203,6 +194,23 @@ function Editor(input, preview) {
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<!-- ====================================================== -->
|
||||
<!-- Begin section : page initialization -->
|
||||
<!-- ====================================================== -->
|
||||
|
||||
<script type="text/javascript"><!--
|
||||
|
||||
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'
|
||||
|
||||
window.onload = stacosys_count();
|
||||
|
||||
--></script>
|
||||
|
||||
<!-- ====================================================== -->
|
||||
<!-- End section : page initialization -->
|
||||
<!-- ====================================================== -->
|
||||
|
||||
</body></html>
|
||||
|
|
33
demo/public/js/page.js
Normal file
33
demo/public/js/page.js
Normal file
|
@ -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';
|
||||
}
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue