2016-03-04 04:10:53 +00:00
|
|
|
from flask import Flask, request, session, g, redirect, url_for, \
|
|
|
|
abort, render_template, flash
|
2016-03-07 00:31:27 +00:00
|
|
|
from flask.ext.mysqldb import MySQL
|
2016-03-07 02:28:14 +00:00
|
|
|
import hashlib
|
2016-03-08 05:39:55 +00:00
|
|
|
from flask_debugtoolbar import DebugToolbarExtension
|
2016-03-07 00:31:27 +00:00
|
|
|
from admin import admin
|
2016-03-10 01:55:18 +00:00
|
|
|
from flask.ext.mail import Mail, Message
|
2016-03-10 03:42:37 +00:00
|
|
|
from wtforms import Form, TextField, TextAreaField, validators
|
2016-03-08 05:39:55 +00:00
|
|
|
|
2016-03-04 04:10:53 +00:00
|
|
|
app = Flask(__name__)
|
|
|
|
app.config.from_pyfile('config.py')
|
2016-03-07 00:31:27 +00:00
|
|
|
app.secret_key = app.config['SECRET_KEY']
|
|
|
|
mysql = MySQL(app)
|
2016-03-08 05:39:55 +00:00
|
|
|
toolbar = DebugToolbarExtension(app)
|
2016-03-10 00:49:33 +00:00
|
|
|
mail = Mail(app)
|
2016-03-07 00:31:27 +00:00
|
|
|
|
|
|
|
app.register_blueprint(admin, url_prefix='/admin')
|
2016-03-04 04:10:53 +00:00
|
|
|
|
|
|
|
def connect_db():
|
2016-03-07 00:31:27 +00:00
|
|
|
return mysql.connection.cursor()
|
|
|
|
|
2016-03-04 04:10:53 +00:00
|
|
|
@app.before_request
|
|
|
|
def before_request():
|
|
|
|
g.db = connect_db()
|
|
|
|
|
|
|
|
@app.teardown_request
|
|
|
|
def teardown_request(exception):
|
|
|
|
db = getattr(g, 'db', None)
|
|
|
|
if db is not None:
|
|
|
|
db.close()
|
|
|
|
|
2016-03-10 03:42:37 +00:00
|
|
|
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)])
|
|
|
|
|
2016-03-04 04:10:53 +00:00
|
|
|
@app.route('/')
|
2016-03-06 02:27:52 +00:00
|
|
|
def home():
|
2016-03-17 06:12:15 +00:00
|
|
|
g.db.execute('SELECT * FROM blog_posts ORDER BY updated_on DESC LIMIT 3')
|
2016-03-08 05:39:55 +00:00
|
|
|
entries = [dict(title=row[1], text=row[2], url=row[5], created=row[6].strftime("%B %d, %Y"), updated=row[7].strftime("%B %d, %Y")) for row in g.db.fetchall()]
|
2016-03-06 02:27:52 +00:00
|
|
|
return render_template('home.html', entries=entries)
|
|
|
|
|
|
|
|
@app.route('/bio')
|
|
|
|
def bio():
|
|
|
|
return render_template('bio.html')
|
|
|
|
|
|
|
|
@app.route('/blog')
|
|
|
|
def blog():
|
2016-03-07 00:31:27 +00:00
|
|
|
g.db.execute('SELECT * FROM blog_posts ORDER BY id DESC')
|
2016-03-08 05:39:55 +00:00
|
|
|
entries = [dict(title=row[1], text=row[2], url=row[5], created=row[6].strftime("%B %d, %Y"), updated=row[7].strftime("%B %d, %Y")) for row in g.db.fetchall()]
|
2016-03-06 02:27:52 +00:00
|
|
|
return render_template('blog.html', entries=entries)
|
|
|
|
|
2016-03-08 05:39:55 +00:00
|
|
|
@app.route('/blog/<url>')
|
|
|
|
def blog_post(url):
|
|
|
|
g.db.execute('SELECT * FROM blog_posts WHERE url="%s"' % url)
|
|
|
|
row = g.db.fetchone()
|
|
|
|
post = [dict(title=row[1], text=row[2], category=row[3], tags=row[4], created=row[6].strftime("%B %d, %Y"), updated=row[7].strftime("%B %d, %Y"))]
|
|
|
|
return render_template('blog-post.html', post=post)
|
|
|
|
|
2016-03-06 02:27:52 +00:00
|
|
|
@app.route('/projects')
|
|
|
|
def projects():
|
|
|
|
return render_template('projects.html')
|
|
|
|
|
2016-03-10 00:49:33 +00:00
|
|
|
@app.route('/contact', methods=['GET', 'POST'])
|
2016-03-06 02:27:52 +00:00
|
|
|
def contact():
|
2016-03-10 03:42:37 +00:00
|
|
|
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'))
|
2016-03-10 00:49:33 +00:00
|
|
|
else:
|
2016-03-10 03:42:37 +00:00
|
|
|
return render_template('contact.html', form=form)
|
2016-03-04 04:10:53 +00:00
|
|
|
|
|
|
|
@app.route('/login', methods=['GET', 'POST'])
|
|
|
|
def login():
|
|
|
|
error = None
|
|
|
|
if request.method == 'POST':
|
|
|
|
if request.form['username'] != app.config['USERNAME']:
|
|
|
|
error = 'Invalid username'
|
2016-03-07 02:28:14 +00:00
|
|
|
elif hashlib.sha512(request.form['password'].encode('utf-8')).hexdigest() != app.config['PASSWORD']:
|
2016-03-04 04:10:53 +00:00
|
|
|
error = 'Invalid password'
|
|
|
|
else:
|
|
|
|
session['logged_in'] = True
|
|
|
|
flash('You were logged in')
|
2016-03-07 00:31:27 +00:00
|
|
|
return redirect(url_for('admin.home'))
|
2016-03-04 04:10:53 +00:00
|
|
|
return render_template('login.html', error=error)
|
|
|
|
|
|
|
|
@app.route('/logout')
|
|
|
|
def logout():
|
|
|
|
session.pop('logged_in', None)
|
|
|
|
flash('You were logged out')
|
2016-03-07 00:31:27 +00:00
|
|
|
return redirect(url_for('blog'))
|
2016-03-04 04:10:53 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2016-03-09 02:38:53 +00:00
|
|
|
app.run(host= '0.0.0.0')
|