| 1 |
from flask import flash |
| 2 |
from flask import Flask |
| 3 |
from flask import redirect |
| 4 |
from flask import render_template |
| 5 |
from flask import session |
| 6 |
from flask import url_for |
| 7 |
from wtforms import TextAreaField |
| 8 |
from wtforms.validators import DataRequired |
| 9 |
|
| 10 |
from flask_wtf import FlaskForm |
| 11 |
from flask_wtf.recaptcha import RecaptchaField |
| 12 |
|
| 13 |
DEBUG = True |
| 14 |
SECRET_KEY = "secret" |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
RECAPTCHA_PUBLIC_KEY = "6LeYIbsSAAAAACRPIllxA7wvXjIE411PfdB2gt2J" |
| 19 |
RECAPTCHA_PRIVATE_KEY = "6LeYIbsSAAAAAJezaIq3Ft_hSTo0YtyeFG-JgRtu" |
| 20 |
|
| 21 |
app = Flask(__name__) |
| 22 |
app.config.from_object(__name__) |
| 23 |
|
| 24 |
|
| 25 |
class CommentForm(FlaskForm): |
| 26 |
comment = TextAreaField("Comment", validators=[DataRequired()]) |
| 27 |
recaptcha = RecaptchaField() |
| 28 |
|
| 29 |
|
| 30 |
@app.route("/") |
| 31 |
def index(form=None): |
| 32 |
if form is None: |
| 33 |
form = CommentForm() |
| 34 |
comments = session.get("comments", []) |
| 35 |
return render_template("index.html", comments=comments, form=form) |
| 36 |
|
| 37 |
|
| 38 |
@app.route("/add/", methods=("POST",)) |
| 39 |
def add_comment(): |
| 40 |
form = CommentForm() |
| 41 |
if form.validate_on_submit(): |
| 42 |
comments = session.pop("comments", []) |
| 43 |
comments.append(form.comment.data) |
| 44 |
session["comments"] = comments |
| 45 |
flash("You have added a new comment") |
| 46 |
return redirect(url_for("index")) |
| 47 |
return index(form) |
| 48 |
|
| 49 |
|
| 50 |
if __name__ == "__main__": |
| 51 |
app.run() |