From 1e155009f6f688099ab28fdb1bff607899a5b1d4 Mon Sep 17 00:00:00 2001 From: Billy Brawner Date: Wed, 9 Mar 2016 21:42:37 -0600 Subject: [PATCH] Added requirements for the project, added WTForms for contact form validation, and finished setting up the contact form. Now ready to go into production for testing there. --- config-example.py | 5 +++-- flask_site.py | 20 +++++++++++++++----- requirements.txt | 11 +++++++++++ schema.sql | 2 +- templates/_formhelper.html | 12 ++++++++++++ templates/contact.html | 13 ++++++++----- 6 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 requirements.txt create mode 100644 templates/_formhelper.html diff --git a/config-example.py b/config-example.py index 90f8dc8..d82b1bd 100644 --- a/config-example.py +++ b/config-example.py @@ -1,8 +1,9 @@ MYSQL_USER = 'user' MYSQL_PASSWORD = 'password' -MYSQL_HOST = 'localhost' +MYSQL_HOST = '%' MYSQL_DB = 'database' DEBUG = True -SECRET_KEY = 'development key' +SECRET_KEY = '\xd9\n\xe6[Hy\xf6\xac\x00\xd3b\xc2\xb3\x98Ii\x8e\x1b\xcb\xcc\x89'K\x1f' USERNAME = 'username' PASSWORD = 'password' +MAIL_DEFAULT_SENDER = "admin@example.com" \ No newline at end of file diff --git a/flask_site.py b/flask_site.py index 2dfa62c..85f48b2 100644 --- a/flask_site.py +++ b/flask_site.py @@ -5,6 +5,7 @@ import hashlib from flask_debugtoolbar import DebugToolbarExtension from admin import admin from flask.ext.mail import Mail, Message +from wtforms import Form, TextField, TextAreaField, validators app = Flask(__name__) app.config.from_pyfile('config.py') @@ -28,6 +29,11 @@ def teardown_request(exception): if db is not None: db.close() +class ContactForm(Form): + name = TextField('Name', [validators.Length(min=4, max=25)]) + email = TextField('Email Address', [validators.Length(min=6, max=35)]) + message = TextAreaField('Message', [validators.Length(min=6, max=5000)]) + @app.route('/') def home(): g.db.execute('SELECT * FROM blog_posts ORDER BY updated_on DESC') @@ -57,12 +63,16 @@ def projects(): @app.route('/contact', methods=['GET', 'POST']) def contact(): - if request.method == 'POST': - request.form['name'] - request.form['email'] - request.form['message'] + form = ContactForm(request.form) + if request.method == 'POST' and form.validate(): + msg = Message(subject='New Message From wbrawner.com', + body="Name: {0}\nEmail: {1}\nMessage: {2}".format(request.form['name'], request.form['email'], request.form['message']), + recipients=["billybrawner@gmail.com"]) + mail.send(msg) + flash('Thanks, your message was sent.') + return redirect(url_for('contact')) else: - return render_template('contact.html') + return render_template('contact.html', form=form) @app.route('/login', methods=['GET', 'POST']) def login(): diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c9ad933 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,11 @@ +blinker==1.4 +Flask==0.10.1 +Flask-DebugToolbar==0.10.0 +Flask-Mail==0.9.1 +Flask-MySQLdb==0.2.0 +itsdangerous==0.24 +Jinja2==2.8 +MarkupSafe==0.23 +mysqlclient==1.3.7 +Werkzeug==0.11.4 +WTForms==2.1 diff --git a/schema.sql b/schema.sql index cb8e1c7..291bba3 100644 --- a/schema.sql +++ b/schema.sql @@ -4,7 +4,7 @@ CREATE TABLE blog_posts ( text VARCHAR( 10000 ) DEFAULT NULL , category VARCHAR( 50 ) DEFAULT NULL , tags VARCHAR( 100 ) DEFAULT NULL , - url VARCHAR(500) DEFAULT NULL UNIQUE, + url VARCHAR(100) DEFAULT NULL UNIQUE, created_on DATETIME NOT NULL , updated_on TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP ); \ No newline at end of file diff --git a/templates/_formhelper.html b/templates/_formhelper.html new file mode 100644 index 0000000..5790894 --- /dev/null +++ b/templates/_formhelper.html @@ -0,0 +1,12 @@ +{% macro render_field(field) %} +
{{ field.label }} +
{{ field(**kwargs)|safe }} + {% if field.errors %} + + {% endif %} +
+{% endmacro %} \ No newline at end of file diff --git a/templates/contact.html b/templates/contact.html index 9da917e..041b377 100644 --- a/templates/contact.html +++ b/templates/contact.html @@ -2,11 +2,14 @@ {% block body %}

contact

-
- - - - + {% from "_formhelper.html" import render_field %} + +
+ {{ render_field(form.name) }} + {{ render_field(form.email) }} + {{ render_field(form.message) }} +
+