Add 'get count of comment' method
Draft JavaScript side
This commit is contained in:
		
							parent
							
								
									8893cf83ed
								
							
						
					
					
						commit
						71383eff9a
					
				
					 4 changed files with 124 additions and 51 deletions
				
			
		| 
						 | 
					@ -11,15 +11,11 @@ from app.helpers.hashing import md5
 | 
				
			||||||
logger = logging.getLogger(__name__)
 | 
					logger = logging.getLogger(__name__)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@app.route("/comments", methods=['GET', 'POST'])
 | 
					@app.route("/comments", methods=['GET'])
 | 
				
			||||||
def query_comments():
 | 
					def query_comments():
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    comments = []
 | 
					    comments = []
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        if request.method == 'POST':
 | 
					 | 
				
			||||||
            token = request.json['token']
 | 
					 | 
				
			||||||
            url = request.json['url']
 | 
					 | 
				
			||||||
        else:
 | 
					 | 
				
			||||||
        token = request.args.get('token', '')
 | 
					        token = request.args.get('token', '')
 | 
				
			||||||
        url = request.args.get('url', '')
 | 
					        url = request.args.get('url', '')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -44,3 +40,19 @@ def query_comments():
 | 
				
			||||||
        r = jsonify({'data': []})
 | 
					        r = jsonify({'data': []})
 | 
				
			||||||
        r.status_code = 400
 | 
					        r.status_code = 400
 | 
				
			||||||
    return r
 | 
					    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-TileColor" content="#2d89ef">
 | 
				
			||||||
    <meta name="msapplication-TileImage" content="/mstile-144x144.png">
 | 
					    <meta name="msapplication-TileImage" content="/mstile-144x144.png">
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <!-- === javascript dependencies === -->
 | 
				
			||||||
    <script src="js/markdown.js"></script>
 | 
					    <script src="js/markdown.js"></script>
 | 
				
			||||||
 | 
					 | 
				
			||||||
    <!-- cosysnode dependencies -->
 | 
					 | 
				
			||||||
    <script src="js/mustache.js"></script>
 | 
					    <script src="js/mustache.js"></script>
 | 
				
			||||||
    <script src="js/stacosys.js"></script>
 | 
					    <script src="js/stacosys.js"></script>
 | 
				
			||||||
 | 
					    <script src="js/page.js"></script>
 | 
				
			||||||
</head>
 | 
					</head>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<body>
 | 
					<body>
 | 
				
			||||||
| 
						 | 
					@ -92,7 +92,7 @@ instance d'ici peu.</p>
 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
	<div class="comment">
 | 
						<div class="comment">
 | 
				
			||||||
<div id="submit-button">
 | 
					<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>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<div id="comment-form" style="display:none">
 | 
					<div id="comment-form" style="display:none">
 | 
				
			||||||
| 
						 | 
					@ -124,13 +124,31 @@ instance d'ici peu.</p>
 | 
				
			||||||
    </form>
 | 
					    </form>
 | 
				
			||||||
</div>
 | 
					</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 -->
 | 
					    <!-- 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">
 | 
					    <script id="stacosys-template" type="x-tmpl-mustache">
 | 
				
			||||||
{{#data}}
 | 
					{{#data}}
 | 
				
			||||||
<hr>
 | 
					<hr>
 | 
				
			||||||
<div class="inline">
 | 
					<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">
 | 
					    <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 class="title">{{author}}</span>
 | 
				
			||||||
    <span> - {{date}}</span>
 | 
					    <span> - {{date}}</span>
 | 
				
			||||||
</div>
 | 
					</div>
 | 
				
			||||||
| 
						 | 
					@ -138,38 +156,11 @@ instance d'ici peu.</p>
 | 
				
			||||||
{{/data}}
 | 
					{{/data}}
 | 
				
			||||||
    </script>
 | 
					    </script>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<script type="text/javascript"><!--
 | 
					    <!-- ====================================================== -->
 | 
				
			||||||
 | 
					    <!--  End section : display comments                        -->
 | 
				
			||||||
 | 
					    <!-- ====================================================== -->
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function showHide(panel_id, button_id){
 | 
					   </div>
 | 
				
			||||||
    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>
 | 
					 | 
				
			||||||
   </article>
 | 
					   </article>
 | 
				
			||||||
</section>
 | 
					</section>
 | 
				
			||||||
            </div>
 | 
					            </div>
 | 
				
			||||||
| 
						 | 
					@ -203,6 +194,23 @@ function Editor(input, preview) {
 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
    </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>
 | 
					</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
 | 
					// Released under Apache license
 | 
				
			||||||
// Copyright (c) 2015 Yannic ARNOUX
 | 
					// 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.
 | 
					// Create the XHR object.
 | 
				
			||||||
function stacosys_get_cors_request(method, url) {
 | 
					function stacosys_get_cors_request(method, url) {
 | 
				
			||||||
  var xhr = new XMLHttpRequest();
 | 
					  var xhr = new XMLHttpRequest();
 | 
				
			||||||
| 
						 | 
					@ -23,12 +18,38 @@ function stacosys_get_cors_request(method, url) {
 | 
				
			||||||
  return xhr;
 | 
					  return xhr;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function stacosys_get_url() {
 | 
					function stacosys_count() {
 | 
				
			||||||
  return STACOSYS_URL + '/comments?token=' + STACOSYS_TOKEN + '&url=' + STACOSYS_PAGE;
 | 
					  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() {
 | 
					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);
 | 
					  var xhr = stacosys_get_cors_request('GET', url);
 | 
				
			||||||
  if (!xhr) {
 | 
					  if (!xhr) {
 | 
				
			||||||
    alert('CORS not supported');
 | 
					    alert('CORS not supported');
 | 
				
			||||||
| 
						 | 
					@ -52,4 +73,3 @@ function stacosys_load() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  xhr.send();
 | 
					  xhr.send();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
window.onload = stacosys_load;
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue