From 504e0347bf165e0f1d13c9751ac6669684e5844d Mon Sep 17 00:00:00 2001 From: Yax <1949284+kianby@users.noreply.github.com> Date: Sun, 10 May 2015 12:44:04 +0200 Subject: [PATCH] Draft new comment submitting --- app/controllers/api.py | 50 +++++++++++++++++++++++++++++++++++++++++- demo/public/index.html | 9 ++++---- demo/public/js/page.js | 47 ++++++++++++++++++++++++++++----------- 3 files changed, 88 insertions(+), 18 deletions(-) diff --git a/app/controllers/api.py b/app/controllers/api.py index 9069eff..ca2922d 100644 --- a/app/controllers/api.py +++ b/app/controllers/api.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- import logging -from flask import request, jsonify +from flask import request, jsonify, abort from app import app from app.models.site import Site from app.models.comment import Comment @@ -56,3 +56,51 @@ def get_comments_count(): r = jsonify({'count': 0}) r.status_code = 200 return r + + +@app.route("/comments", methods=['POST']) +def new_comment(): + + logger.info("new comment !!!!") + + try: + token = request.form['token'] + site = Site.select().where(Site.token == token).get() + + # FOR DEBUG + return "OK" + + 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 + captcha = "" + if "captcha" in request.form: + captcha = request.form['captcha'] + if captcha: + logger.warn("discard spam: captcha %s author %s email %s site %s article %s message %s" + % (captcha, author, email, site, article, message)) + else: + req = {'type': 'comment', 'author': author, 'email': email, 'site': site, 'article': article, + 'message': message, 'url': source_url, 'subscribe': subscribe} + processor.enqueue(req) + + except: + logger.exception("new comment failure") + abort(400) + + return "OK" diff --git a/demo/public/index.html b/demo/public/index.html index 52eb9c4..3abe8ae 100644 --- a/demo/public/index.html +++ b/demo/public/index.html @@ -90,13 +90,14 @@ en vaut la peine. Je l'utilise dans le cadre du projet Framabag mais je prévois d'installer ma propre instance d'ici peu.

-
+ +
- - + +
diff --git a/demo/public/js/page.js b/demo/public/js/page.js index adea0bf..a1b176e 100644 --- a/demo/public/js/page.js +++ b/demo/public/js/page.js @@ -1,3 +1,7 @@ +// -------------------------------------------------------------------------- +// Common functions +// -------------------------------------------------------------------------- + function show_hide(panel_id, button_id){ if (document.getElementById(panel_id).style.display == 'none'){ document.getElementById(panel_id).style.display = ''; @@ -7,6 +11,23 @@ function show_hide(panel_id, button_id){ } } +// -------------------------------------------------------------------------- +// Load and display page comments +// -------------------------------------------------------------------------- + +function initialize_comments() { + stacosys_count(comments_initialized); +} + +function comments_initialized(count) { + if (count > 0) { + if (count > 1) { + document.getElementById('show-comment-label').innerHTML = 'Voir les ' + count + ' commentaires'; + } + document.getElementById('show-comments-button').style.display = ''; + } +} + function show_comments() { stacosys_load(comments_loaded); } @@ -21,19 +42,21 @@ function comments_loaded(response) { document.getElementById('stacosys-comments').innerHTML = rendered; } -function initialize_comments() { - stacosys_count(comments_initialized); -} +// -------------------------------------------------------------------------- +// Submit a new comment +// -------------------------------------------------------------------------- -function comments_initialized(count) { - if (count > 0) { - if (count > 1) { - document.getElementById('show-comment-label').innerHTML = 'Voir les ' + count + ' commentaires'; - } - document.getElementById('show-comments-button').style.display = ''; - } +function new_comment() { + var author = document.getElementById('author').value; + // TODO make CORS POST request + // and asynchronously redirect depending on result + console.log('SUBMIT ' + author); } +// -------------------------------------------------------------------------- +// Markdown preview +// -------------------------------------------------------------------------- + function preview_markdown() { if (document.getElementById('preview-container').style.display == 'none'){ document.getElementById('preview-container').style.display = ''; @@ -50,6 +73,4 @@ function Editor(input, preview) { this.update(); } -function get_action() { - return '/post_a_new_comment'; -} +