Renamed install.py to setup.py to sound more Python-y and finished up the blog upload/views for posts.

This commit is contained in:
William Brawner 2016-03-07 23:39:55 -06:00
parent 843b208a67
commit aba62ead5b
10 changed files with 107 additions and 26 deletions

View file

@ -1,9 +1,11 @@
from flask import Flask, request, session, g, redirect, url_for, \ from flask import Flask, request, session, g, redirect, url_for, \
abort, render_template, flash, Blueprint abort, render_template, flash, Blueprint
from flask.ext.mysqldb import MySQL from flask.ext.mysqldb import MySQL
import datetime
admin = Blueprint('admin', __name__, admin = Blueprint('admin', __name__,
template_folder='templates') template_folder='templates')
import flask_site
@admin.route('/') @admin.route('/')
def home(): def home():
@ -17,7 +19,9 @@ def posts():
if not session.get('logged_in'): if not session.get('logged_in'):
return redirect(url_for('login')) return redirect(url_for('login'))
else: 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']) @admin.route('/new-post', methods=['GET', 'POST'])
def new_post(): def new_post():
@ -25,9 +29,8 @@ def new_post():
return redirect(url_for('login')) return redirect(url_for('login'))
else: else:
if request.method == 'POST': 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()))', 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())))
[request.form['title'], request.form['text'], request.form['category'], request.form['tags']]) flask_site.mysql.connection.commit()
g.db.commit()
flash('New post added successfully') flash('New post added successfully')
return redirect(url_for('blog')) return redirect(url_for('blog'))
else: else:
@ -41,7 +44,7 @@ def edit_post():
if request.method == 'POST': if request.method == 'POST':
g.db.execute('UPDATE blog_posts SET (title, text, category, tags, updated_on) values (?, ?, ?, ?, str(datetime.datetime.now()))', 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']]) [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') flash('New post added successfully')
return redirect(url_for('blog')) return redirect(url_for('blog'))
else: else:

View file

@ -2,12 +2,15 @@ from flask import Flask, request, session, g, redirect, url_for, \
abort, render_template, flash abort, render_template, flash
from flask.ext.mysqldb import MySQL from flask.ext.mysqldb import MySQL
import hashlib import hashlib
from flask_debugtoolbar import DebugToolbarExtension
from admin import admin from admin import admin
app = Flask(__name__) app = Flask(__name__)
app.config.from_pyfile('config.py') app.config.from_pyfile('config.py')
app.secret_key = app.config['SECRET_KEY'] app.secret_key = app.config['SECRET_KEY']
mysql = MySQL(app) mysql = MySQL(app)
toolbar = DebugToolbarExtension(app)
app.register_blueprint(admin, url_prefix='/admin') app.register_blueprint(admin, url_prefix='/admin')
@ -27,7 +30,7 @@ def teardown_request(exception):
@app.route('/') @app.route('/')
def home(): def home():
g.db.execute('SELECT * FROM blog_posts ORDER BY updated_on DESC') 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) return render_template('home.html', entries=entries)
@app.route('/bio') @app.route('/bio')
@ -37,9 +40,16 @@ def bio():
@app.route('/blog') @app.route('/blog')
def blog(): def blog():
g.db.execute('SELECT * FROM blog_posts ORDER BY id DESC') 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) return render_template('blog.html', entries=entries)
@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)
@app.route('/projects') @app.route('/projects')
def projects(): def projects():
return render_template('projects.html') return render_template('projects.html')

View file

@ -4,6 +4,7 @@ CREATE TABLE blog_posts (
text VARCHAR( 10000 ) DEFAULT NULL , text VARCHAR( 10000 ) DEFAULT NULL ,
category VARCHAR( 50 ) DEFAULT NULL , category VARCHAR( 50 ) DEFAULT NULL ,
tags VARCHAR( 100 ) DEFAULT NULL , tags VARCHAR( 100 ) DEFAULT NULL ,
url VARCHAR(500) DEFAULT NULL UNIQUE,
created_on DATETIME NOT NULL , created_on DATETIME NOT NULL ,
updated_on TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP updated_on TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP
); );

View file

@ -33,7 +33,7 @@ td ul {
margin: 5px; margin: 5px;
} }
input { input, textarea {
background-color: #FFF; background-color: #FFF;
color: #002900; color: #002900;
border: 1px solid #002900; border: 1px solid #002900;
@ -464,6 +464,28 @@ a.social-link:hover, a.social-link:active {
color: #fff; 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) { @media screen and (max-width:1024px) {
.main-nav, #nav { .main-nav, #nav {
position: absolute; position: absolute;

View file

@ -1,6 +1,19 @@
{% extends 'admin/master.html' %} {% extends 'admin/master.html' %}
{% block body %} {% block body %}
<h1 style="border-width: 0px; text-align: center; min-height: 120px; position: relative; overflow: hidden;"> <div class="section">
<span>congrats, it worked</span> <h2>new post</h2>
</h1> <form action="{{ url_for('admin.new_post') }}" method="post">
<dl>
<dt>Title:</dt>
<dd><input type="text" size=30 name="title"></dd>
<dt>Text:</dt>
<dd><textarea name="text" rows=5 cols=40></textarea></dd>
<dt>Category:</dt>
<dd><input type="text" size=30 name="category"></dd>
<dt>Tags:</dt>
<dd><input type="text" size=30 name="tags"></dd>
<dd><input type="submit" value="Publish"></dd>
</dl>
</form>
</div>
{% endblock %} {% endblock %}

View file

@ -0,0 +1,33 @@
{% extends "admin/master.html" %}
{% block body %}
<div class="section">
<h2>posts</h2>
<a href="{{ url_for('admin.new_post') }}">Add new post</a>
<table class="admin-table">
<thead>
<tr>
<th>title</th>
<th>category</th>
<th>tags</th>
<th>comments</th>
<th>published</th>
<th>updated</th>
</tr>
</thead>
<tbody>
{% for entry in entries %}
<tr>
<td>{{ entry.title }}</td>
<td>{{ entry.category }}</td>
<td>{{ entry.tags }}</td>
<td>{{ entry.comments }}</td>
<td>{{ entry.created }}</td>
<td>{{ entry.updated }}</td>
</tr>
{% else %}
<em>Get to writing!</em>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}

10
templates/blog-post.html Normal file
View file

@ -0,0 +1,10 @@
{% extends 'master.html' %}
{% block body %}
{% for data in post %}
<div class="section">
<h2>{{ data.title }}</h2>
<em>{{ data.updated }}</em>
{{ data.text | safe }}
</div>
{% endfor %}
{% endblock %}

View file

@ -1,23 +1,12 @@
{% extends "master.html" %} {% extends "master.html" %}
{% block body %} {% block body %}
{% if session.logged_in %}
<form action="{{ url_for('add_entry') }}" method=post class=add-entry>
<dl>
<dt>Title:
<dd><input type=text size=30 name=title>
<dt>Text:
<dd><textarea name=text rows=5 cols=40></textarea>
<dd><input type=submit value=Share>
</dl>
</form>
{% endif %}
<div class="section"> <div class="section">
<h2>blog</h2> <h2>blog</h2>
{% for entry in entries %} {% for entry in entries %}
<div class="blog-entry"> <div class="blog-entry">
<h3>{{ entry.title }}</h3> <h3><a href="{{ url_for('blog') }}/{{ entry.url }}">{{ entry.title }}</a></h3>
<h5>{{ entry.updated }}</h5> <h5>{{ entry.updated }}</h5>
<p>{{ entry.text|safe }}</p> {{ entry.text|safe }}
</div> </div>
{% else %} {% else %}
<em>Sorry, I still haven't gotten around to moving my blog posts over!</em> <em>Sorry, I still haven't gotten around to moving my blog posts over!</em>

View file

@ -8,9 +8,9 @@
<h2 style="margin-top: 0;">from my blog</h2> <h2 style="margin-top: 0;">from my blog</h2>
{% for entry in entries %} {% for entry in entries %}
<div class="blog-entry"> <div class="blog-entry">
<h3>{{ entry.title }}</h3> <h3><a href="{{ url_for('blog') }}/{{ entry.url }}">{{ entry.title }}</a></h3>
<h5>{{ entry.updated }}</h5> <h5>{{ entry.updated }}</h5>
<p>{{ entry.text|safe }}</p> {{ entry.text|safe }}
</div> </div>
{% else %} {% else %}
<em>Sorry, I still haven't gotten around to moving my blog posts over!</em> <em>Sorry, I still haven't gotten around to moving my blog posts over!</em>