From aba62ead5b8fb07ad327afa95706984a8014ec98 Mon Sep 17 00:00:00 2001 From: Billy Brawner Date: Mon, 7 Mar 2016 23:39:55 -0600 Subject: [PATCH] Renamed install.py to setup.py to sound more Python-y and finished up the blog upload/views for posts. --- admin.py | 13 ++++++++----- flask_site.py | 14 ++++++++++++-- schema.sql | 1 + install.py => setup.py | 0 static/css/style.css | 24 +++++++++++++++++++++++- templates/admin/new-post.html | 19 ++++++++++++++++--- templates/admin/posts.html | 33 +++++++++++++++++++++++++++++++++ templates/blog-post.html | 10 ++++++++++ templates/blog.html | 15 ++------------- templates/home.html | 4 ++-- 10 files changed, 107 insertions(+), 26 deletions(-) rename install.py => setup.py (100%) create mode 100644 templates/admin/posts.html create mode 100644 templates/blog-post.html diff --git a/admin.py b/admin.py index 71980f8..f3ccbdd 100644 --- a/admin.py +++ b/admin.py @@ -1,9 +1,11 @@ from flask import Flask, request, session, g, redirect, url_for, \ abort, render_template, flash, Blueprint from flask.ext.mysqldb import MySQL +import datetime admin = Blueprint('admin', __name__, template_folder='templates') +import flask_site @admin.route('/') def home(): @@ -17,7 +19,9 @@ def posts(): if not session.get('logged_in'): return redirect(url_for('login')) else: - return render_template('admin/posts.html') + g.db.execute('SELECT * FROM blog_posts ORDER BY updated_on DESC') + entries = [dict(title=row[1], category=row[3], tags=row[4], created=row[6].strftime("%d-%m-%Y"), updated=row[7].strftime("%d-%m-%Y")) for row in g.db.fetchall()] + return render_template('admin/posts.html', entries=entries) @admin.route('/new-post', methods=['GET', 'POST']) def new_post(): @@ -25,9 +29,8 @@ def new_post(): return redirect(url_for('login')) else: if request.method == 'POST': - g.db.execute('insert into blog_posts (title, text, category, tags, created_on, updated_on) values (?, ?, ?, ?, str(datetime.datetime.now()), str(datetime.datetime.now()))', - [request.form['title'], request.form['text'], request.form['category'], request.form['tags']]) - g.db.commit() + g.db.execute("insert into blog_posts (title, text, category, tags, url, created_on, updated_on) values ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}')" .format(request.form['title'], request.form['text'], request.form['category'], request.form['tags'], request.form['title'].lower().replace(' ', '-'), str(datetime.datetime.now()), str(datetime.datetime.now()))) + flask_site.mysql.connection.commit() flash('New post added successfully') return redirect(url_for('blog')) else: @@ -41,7 +44,7 @@ def edit_post(): if request.method == 'POST': g.db.execute('UPDATE blog_posts SET (title, text, category, tags, updated_on) values (?, ?, ?, ?, str(datetime.datetime.now()))', [request.form['title'], request.form['text'], request.form['category'], request.form['tags']]) - g.db.commit() + flask_site.mysql.connection.commit() flash('New post added successfully') return redirect(url_for('blog')) else: diff --git a/flask_site.py b/flask_site.py index a3bee6b..35f9f30 100644 --- a/flask_site.py +++ b/flask_site.py @@ -2,12 +2,15 @@ from flask import Flask, request, session, g, redirect, url_for, \ abort, render_template, flash from flask.ext.mysqldb import MySQL import hashlib +from flask_debugtoolbar import DebugToolbarExtension from admin import admin + app = Flask(__name__) app.config.from_pyfile('config.py') app.secret_key = app.config['SECRET_KEY'] mysql = MySQL(app) +toolbar = DebugToolbarExtension(app) app.register_blueprint(admin, url_prefix='/admin') @@ -27,7 +30,7 @@ def teardown_request(exception): @app.route('/') def home(): g.db.execute('SELECT * FROM blog_posts ORDER BY updated_on DESC') - entries = [dict(title=row[1], text=row[2], created=row[5].strftime("%B %d, %Y"), updated=row[6].strftime("%B %d, %Y")) for row in g.db.fetchall()] + 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()] return render_template('home.html', entries=entries) @app.route('/bio') @@ -37,9 +40,16 @@ def bio(): @app.route('/blog') def blog(): g.db.execute('SELECT * FROM blog_posts ORDER BY id DESC') - entries = [dict(title=row[1], text=row[2], created=row[5].strftime("%B %d, %Y"), updated=row[6].strftime("%B %d, %Y")) for row in g.db.fetchall()] + 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()] return render_template('blog.html', entries=entries) +@app.route('/blog/') +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) + @app.route('/projects') def projects(): return render_template('projects.html') diff --git a/schema.sql b/schema.sql index 4f4717a..cb8e1c7 100644 --- a/schema.sql +++ b/schema.sql @@ -4,6 +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, created_on DATETIME NOT NULL , updated_on TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP ); \ No newline at end of file diff --git a/install.py b/setup.py similarity index 100% rename from install.py rename to setup.py diff --git a/static/css/style.css b/static/css/style.css index 6483245..6bb0b2e 100644 --- a/static/css/style.css +++ b/static/css/style.css @@ -33,7 +33,7 @@ td ul { margin: 5px; } -input { +input, textarea { background-color: #FFF; color: #002900; border: 1px solid #002900; @@ -464,6 +464,28 @@ a.social-link:hover, a.social-link:active { color: #fff; } +/* Admin Styles */ +.admin-table { + width: 100%; +} + +.admin-table th, .admin-table td { + padding: 10px 5px; +} + +.admin-table tr:first-child th, { + padding-bottom: 5px; + +} +.admin-table tr:first-child td { + padding-top: 5px; +} + + +.admin-table th:first-child, .admin-table td:first-child { + width: 50%; +} + @media screen and (max-width:1024px) { .main-nav, #nav { position: absolute; diff --git a/templates/admin/new-post.html b/templates/admin/new-post.html index 1befc45..e1a827e 100644 --- a/templates/admin/new-post.html +++ b/templates/admin/new-post.html @@ -1,6 +1,19 @@ {% extends 'admin/master.html' %} {% block body %} -

- congrats, it worked -

+
+

new post

+
+
+
Title:
+
+
Text:
+
+
Category:
+
+
Tags:
+
+
+
+
+
{% endblock %} diff --git a/templates/admin/posts.html b/templates/admin/posts.html new file mode 100644 index 0000000..bbf7a37 --- /dev/null +++ b/templates/admin/posts.html @@ -0,0 +1,33 @@ +{% extends "admin/master.html" %} +{% block body %} +
+

posts

+ Add new post + + + + + + + + + + + + + {% for entry in entries %} + + + + + + + + + {% else %} + Get to writing! + {% endfor %} + +
titlecategorytagscommentspublishedupdated
{{ entry.title }}{{ entry.category }}{{ entry.tags }}{{ entry.comments }}{{ entry.created }}{{ entry.updated }}
+
+{% endblock %} diff --git a/templates/blog-post.html b/templates/blog-post.html new file mode 100644 index 0000000..2f734e8 --- /dev/null +++ b/templates/blog-post.html @@ -0,0 +1,10 @@ +{% extends 'master.html' %} +{% block body %} +{% for data in post %} +
+

{{ data.title }}

+ {{ data.updated }} + {{ data.text | safe }} +
+{% endfor %} +{% endblock %} \ No newline at end of file diff --git a/templates/blog.html b/templates/blog.html index 8427aa2..fab7eaa 100644 --- a/templates/blog.html +++ b/templates/blog.html @@ -1,23 +1,12 @@ {% extends "master.html" %} {% block body %} - {% if session.logged_in %} -
-
-
Title: -
-
Text: -
-
-
-
- {% endif %}

blog

{% for entry in entries %}
-

{{ entry.title }}

+

{{ entry.title }}

{{ entry.updated }}
-

{{ entry.text|safe }}

+ {{ entry.text|safe }}
{% else %} Sorry, I still haven't gotten around to moving my blog posts over! diff --git a/templates/home.html b/templates/home.html index 9542b4a..35170a6 100644 --- a/templates/home.html +++ b/templates/home.html @@ -8,9 +8,9 @@

from my blog

{% for entry in entries %}
-

{{ entry.title }}

+

{{ entry.title }}

{{ entry.updated }}
-

{{ entry.text|safe }}

+ {{ entry.text|safe }}
{% else %} Sorry, I still haven't gotten around to moving my blog posts over!