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) %} +