Split cleanly stacosys API usage and page rendering
Progress on new comment post method
This commit is contained in:
parent
a200fdfa88
commit
300727cdab
4 changed files with 78 additions and 126 deletions
|
@ -61,43 +61,35 @@ def get_comments_count():
|
||||||
@app.route("/comments", methods=['POST'])
|
@app.route("/comments", methods=['POST'])
|
||||||
def new_comment():
|
def new_comment():
|
||||||
|
|
||||||
logger.info("new comment !!!!")
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
token = request.form['token']
|
data = request.get_json()
|
||||||
|
logger.info(data)
|
||||||
|
|
||||||
|
# validate token: retrieve site entity
|
||||||
|
token = data.get('token', '')
|
||||||
site = Site.select().where(Site.token == token).get()
|
site = Site.select().where(Site.token == token).get()
|
||||||
|
if site is None:
|
||||||
|
logger.warn('Unknown site %s' % token)
|
||||||
|
abort(400)
|
||||||
|
|
||||||
# FOR DEBUG
|
# get values
|
||||||
return "OK"
|
url = data.get('url', '')
|
||||||
|
author_name = data.get('author', '')
|
||||||
|
author_email = data.get('email', '')
|
||||||
|
author_site = data.get('site', '')
|
||||||
|
message = data.get('message', '')
|
||||||
|
subscribe = data.get('subscribe', '')
|
||||||
|
|
||||||
source_url = request.headers.get('referer', '')
|
|
||||||
url = app.config["pecosys"]["post"]["redirect_url"]
|
|
||||||
|
|
||||||
if app.config["pecosys"]["post"]["redirect_referer"]:
|
|
||||||
url = app.config["pecosys"]["post"]["redirect_url"] + '?referer=' + request.headers.get('referer', '')
|
|
||||||
else:
|
|
||||||
url = request.headers.get('referer', app.config["pecosys"]["post"]["redirect_url"])
|
|
||||||
|
|
||||||
# get form values and create comment file
|
|
||||||
author = request.form['author']
|
|
||||||
email = request.form['email']
|
|
||||||
site = request.form['site']
|
|
||||||
article = request.form['article']
|
|
||||||
message = request.form['message']
|
|
||||||
subscribe = False
|
|
||||||
if "subscribe" in request.form and request.form['subscribe'] == "on":
|
|
||||||
subscribe = True
|
|
||||||
# honeypot for spammers
|
# honeypot for spammers
|
||||||
captcha = ""
|
captcha = data.get('captcha', '')
|
||||||
if "captcha" in request.form:
|
|
||||||
captcha = request.form['captcha']
|
|
||||||
if captcha:
|
if captcha:
|
||||||
logger.warn("discard spam: captcha %s author %s email %s site %s article %s message %s"
|
logger.warn('discard spam: captcha %s author %s email %s site %s url %s message %s'
|
||||||
% (captcha, author, email, site, article, message))
|
% (captcha, author_name, author_email, author_site, url, message))
|
||||||
else:
|
else:
|
||||||
req = {'type': 'comment', 'author': author, 'email': email, 'site': site, 'article': article,
|
# TODO push new comment to backend service
|
||||||
'message': message, 'url': source_url, 'subscribe': subscribe}
|
logger.info('process: captcha %s author %s email %s site %s url %s message %s subscribe %s'
|
||||||
processor.enqueue(req)
|
% (captcha, author_name, author_email, author_site,
|
||||||
|
url, message, subscribe))
|
||||||
|
|
||||||
except:
|
except:
|
||||||
logger.exception("new comment failure")
|
logger.exception("new comment failure")
|
||||||
|
|
|
@ -200,10 +200,10 @@ instance d'ici peu.</p>
|
||||||
|
|
||||||
<script type="text/javascript"><!--
|
<script type="text/javascript"><!--
|
||||||
|
|
||||||
STACOSYS_URL = 'http://127.0.0.1:8000';
|
var STACOSYS_URL = 'http://127.0.0.1:8000';
|
||||||
STACOSYS_TOKEN = '9fb3fc042c572cb831005fd16186126765140fa2bd9bb2d4a28e47a9457dc26c';
|
var STACOSYS_TOKEN = '9fb3fc042c572cb831005fd16186126765140fa2bd9bb2d4a28e47a9457dc26c';
|
||||||
//STACOSYS_PAGE = 'blogduyax.madyanne.fr/mes-applications-pour-blackberry.html'
|
//STACOSYS_PAGE = 'blogduyax.madyanne.fr/mes-applications-pour-blackberry.html'
|
||||||
STACOSYS_PAGE = 'blogduyax.madyanne.fr/migration-du-blog-sous-pelican.html'
|
var STACOSYS_PAGE = 'blogduyax.madyanne.fr/migration-du-blog-sous-pelican.html'
|
||||||
|
|
||||||
window.onload = initialize_comments();
|
window.onload = initialize_comments();
|
||||||
|
|
||||||
|
|
|
@ -16,23 +16,32 @@ function show_hide(panel_id, button_id){
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
|
|
||||||
function initialize_comments() {
|
function initialize_comments() {
|
||||||
stacosys_count(comments_initialized);
|
stacosys_get_count(init_success, init_failure);
|
||||||
}
|
}
|
||||||
|
|
||||||
function comments_initialized(count) {
|
function init_success(data) {
|
||||||
|
var response = JSON.parse(data);
|
||||||
|
var count = response.count;
|
||||||
if (count > 0) {
|
if (count > 0) {
|
||||||
if (count > 1) {
|
if (count > 1) {
|
||||||
document.getElementById('show-comment-label').innerHTML = 'Voir les ' + count + ' commentaires';
|
document.getElementById('show-comment-label').innerHTML = 'Voir les ' + count + ' commentaires';
|
||||||
}
|
}
|
||||||
document.getElementById('show-comments-button').style.display = '';
|
document.getElementById('show-comments-button').style.display = '';
|
||||||
}
|
}
|
||||||
|
console.log('initialization success');
|
||||||
|
}
|
||||||
|
|
||||||
|
function init_failure(error) {
|
||||||
|
// NOP
|
||||||
|
console.log('initialization failure');
|
||||||
}
|
}
|
||||||
|
|
||||||
function show_comments() {
|
function show_comments() {
|
||||||
stacosys_load(comments_loaded);
|
stacosys_load_comments(loading_success, loading_failure);
|
||||||
}
|
}
|
||||||
|
|
||||||
function comments_loaded(response) {
|
function loading_success(data) {
|
||||||
|
var response = JSON.parse(data);
|
||||||
for (var i = 0, numTokens = response.data.length; i < numTokens; ++i) {
|
for (var i = 0, numTokens = response.data.length; i < numTokens; ++i) {
|
||||||
response.data[i].mdcontent = markdown.toHTML(response.data[i].content);
|
response.data[i].mdcontent = markdown.toHTML(response.data[i].content);
|
||||||
}
|
}
|
||||||
|
@ -42,6 +51,11 @@ function comments_loaded(response) {
|
||||||
document.getElementById('stacosys-comments').innerHTML = rendered;
|
document.getElementById('stacosys-comments').innerHTML = rendered;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function loading_failure(error) {
|
||||||
|
// NOP
|
||||||
|
console.log('loading failure');
|
||||||
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
// Submit a new comment
|
// Submit a new comment
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
|
@ -53,11 +67,17 @@ function new_comment() {
|
||||||
var captcha = document.getElementById('captcha').value;
|
var captcha = document.getElementById('captcha').value;
|
||||||
//var subscribe = document.getElementById('subscribe').value;
|
//var subscribe = document.getElementById('subscribe').value;
|
||||||
|
|
||||||
stacosys_new(author, email, site, captcha, comment_submitted);
|
stacosys_new_comment(author, email, site, captcha, submit_success, submit_failure);
|
||||||
}
|
}
|
||||||
|
|
||||||
function comment_submitted(success) {
|
function submit_success(data) {
|
||||||
console.log('SUBMITTED : ' + success);
|
console.log('submit ' + data);
|
||||||
|
// TODO redirect to redirect page with page as argument
|
||||||
|
}
|
||||||
|
|
||||||
|
function submit_failure(error) {
|
||||||
|
console.log('submit failure');
|
||||||
|
// TODO redirect to error page
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
|
|
|
@ -1,17 +1,16 @@
|
||||||
// Copyright (c) 2015 Yannic ARNOUX
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make a X-Domain request to url and callback.
|
* Make a X-Domain request to url and callback.
|
||||||
*
|
*
|
||||||
* @param url {String}
|
* @param url {String}
|
||||||
* @param method {String} HTTP verb ('GET', 'POST', 'DELETE', etc.)
|
* @param method {String} HTTP verb ('GET', 'POST', 'DELETE', etc.)
|
||||||
* @param data {String} request body
|
* @param data {String} request body
|
||||||
|
* @param header {Dict} header options
|
||||||
* @param callback {Function} to callback on completion
|
* @param callback {Function} to callback on completion
|
||||||
* @param errback {Function} to callback on error
|
* @param errback {Function} to callback on error
|
||||||
*/
|
*/
|
||||||
function xdr(url, method, data, callback, errback) {
|
function xdr(url, method, data, header, callback, errback) {
|
||||||
var req;
|
var req;
|
||||||
|
|
||||||
if(XMLHttpRequest) {
|
if(XMLHttpRequest) {
|
||||||
req = new XMLHttpRequest();
|
req = new XMLHttpRequest();
|
||||||
|
|
||||||
|
@ -27,6 +26,9 @@ function xdr(url, method, data, callback, errback) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
for ( var h in header ) {
|
||||||
|
req.setRequestHeader(h, header[h]);
|
||||||
|
}
|
||||||
req.send(data);
|
req.send(data);
|
||||||
}
|
}
|
||||||
} else if(XDomainRequest) {
|
} else if(XDomainRequest) {
|
||||||
|
@ -36,100 +38,38 @@ function xdr(url, method, data, callback, errback) {
|
||||||
req.onload = function() {
|
req.onload = function() {
|
||||||
callback(req.responseText);
|
callback(req.responseText);
|
||||||
};
|
};
|
||||||
|
for ( var h in header ) {
|
||||||
|
req.setRequestHeader(h, header[h]);
|
||||||
|
}
|
||||||
req.send(data);
|
req.send(data);
|
||||||
} else {
|
} else {
|
||||||
errback(new Error('CORS not supported'));
|
errback(new Error('CORS not supported'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function stacosys_get_count(callback, errback) {
|
||||||
// Create the XHR object.
|
|
||||||
function stacosys_get_cors_request(method, url) {
|
|
||||||
var xhr = new XMLHttpRequest();
|
|
||||||
if ("withCredentials" in xhr) {
|
|
||||||
// XHR for Chrome/Firefox/Opera/Safari.
|
|
||||||
xhr.open(method, url, true);
|
|
||||||
} else if (typeof XDomainRequest != "undefined") {
|
|
||||||
// XDomainRequest for IE.
|
|
||||||
xhr = new XDomainRequest();
|
|
||||||
xhr.open(method, url);
|
|
||||||
} else {
|
|
||||||
// CORS not supported.
|
|
||||||
xhr = null;
|
|
||||||
}
|
|
||||||
return xhr;
|
|
||||||
}
|
|
||||||
|
|
||||||
function stacosys_count(callback) {
|
|
||||||
|
|
||||||
var url = STACOSYS_URL + '/comments/count?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);
|
xdr(url, 'GET', null, {}, callback, errback);
|
||||||
if (!xhr) {
|
|
||||||
console.log('CORS not supported');
|
|
||||||
callback(0);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Response handlers.
|
|
||||||
xhr.onload = function() {
|
|
||||||
var jsonResponse = JSON.parse(xhr.responseText);
|
|
||||||
var count = jsonResponse.count;
|
|
||||||
callback(count);
|
|
||||||
};
|
|
||||||
|
|
||||||
xhr.onerror = function() {
|
|
||||||
console.log('Woops, there was an error making the request.');
|
|
||||||
callback(0);
|
|
||||||
};
|
|
||||||
|
|
||||||
xhr.send();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function stacosys_load(callback) {
|
function stacosys_load_comments(callback, errback) {
|
||||||
|
|
||||||
var url = STACOSYS_URL + '/comments?token=' + STACOSYS_TOKEN + '&url=' + STACOSYS_PAGE;
|
var url = STACOSYS_URL + '/comments?token=' + STACOSYS_TOKEN + '&url=' + STACOSYS_PAGE;
|
||||||
var xhr = stacosys_get_cors_request('GET', url);
|
xdr(url, 'GET', null, {}, callback, errback);
|
||||||
if (!xhr) {
|
|
||||||
console.log('CORS not supported');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Response handlers.
|
|
||||||
xhr.onload = function() {
|
|
||||||
var jsonResponse = JSON.parse(xhr.responseText);
|
|
||||||
callback(jsonResponse);
|
|
||||||
};
|
|
||||||
|
|
||||||
xhr.onerror = function() {
|
|
||||||
console.log('Woops, there was an error making the request.');
|
|
||||||
};
|
|
||||||
|
|
||||||
xhr.send();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function stacosys_new(author, email, site, captcha, callback) {
|
function stacosys_new_comment(author, email, site, captcha, callback, errback) {
|
||||||
|
var url = STACOSYS_URL + '/comments';
|
||||||
var url = STACOSYS_URL + '/comments?token=' + STACOSYS_TOKEN
|
var data = {
|
||||||
+ '&url=' + STACOSYS_PAGE + '&author=' + author
|
'token': STACOSYS_TOKEN,
|
||||||
+ '&email=' + email + '&site=' + site
|
'url': STACOSYS_PAGE,
|
||||||
+ '&captcha=' + captcha;
|
'author': author,
|
||||||
var xhr = stacosys_get_cors_request('POST', url);
|
'email': email,
|
||||||
if (!xhr) {
|
'site': site,
|
||||||
console.log('CORS not supported');
|
'captcha': captcha
|
||||||
callback(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Response handlers.
|
|
||||||
xhr.onload = function() {
|
|
||||||
var jsonResponse = JSON.parse(xhr.responseText);
|
|
||||||
callback(jsonResponse);
|
|
||||||
};
|
};
|
||||||
|
var header = {
|
||||||
xhr.onerror = function() {
|
'Content-type': 'application/json'
|
||||||
console.log('Woops, there was an error making the request.');
|
|
||||||
callback(false);
|
|
||||||
};
|
};
|
||||||
|
var j = JSON.stringify(data);
|
||||||
xhr.send();
|
xdr(url, 'POST', JSON.stringify(data), header, callback, errback);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue