From b10e58d40c886768aff2cd5f1f8027d662ec653e Mon Sep 17 00:00:00 2001 From: Billy Brawner Date: Thu, 3 Mar 2016 22:10:53 -0600 Subject: [PATCH] Created a basic Flask microblog app from a tutorial --- .gitignore | 2 ++ flask_site.py | 57 +++++++++++++++++++++++++++++++++++++ install.py | 11 +++++++ schema.sql | 7 +++++ static/css/style.css | 18 ++++++++++++ templates/login.html | 14 +++++++++ templates/master.html | 17 +++++++++++ templates/show_entries.html | 21 ++++++++++++++ 8 files changed, 147 insertions(+) create mode 100644 .gitignore create mode 100644 flask_site.py create mode 100644 install.py create mode 100644 schema.sql create mode 100644 static/css/style.css create mode 100644 templates/login.html create mode 100644 templates/master.html create mode 100644 templates/show_entries.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3570422 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +config.py +__pycache__ diff --git a/flask_site.py b/flask_site.py new file mode 100644 index 0000000..f5f41fe --- /dev/null +++ b/flask_site.py @@ -0,0 +1,57 @@ +import sqlite3 +from flask import Flask, request, session, g, redirect, url_for, \ + abort, render_template, flash + +app = Flask(__name__) +app.config.from_pyfile('config.py') + +def connect_db(): + return sqlite3.connect(app.config['DATABASE']) +@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() + +@app.route('/') +def show_entries(): + cur = g.db.execute('select title, text from entries order by id desc') + entries = [dict(title=row[0], text=row[1]) for row in cur.fetchall()] + return render_template('show_entries.html', entries=entries) + +@app.route('/add', methods=['POST']) +def add_entry(): + if not session.get('logged_in'): + abort(401) + g.db.execute('insert into entries (title, text) values (?, ?)', + [request.form['title'], request.form['text']]) + g.db.commit() + flash('New entry was successfully posted') + return redirect(url_for('show_entries')) + +@app.route('/login', methods=['GET', 'POST']) +def login(): + error = None + if request.method == 'POST': + if request.form['username'] != app.config['USERNAME']: + error = 'Invalid username' + elif request.form['password'] != app.config['PASSWORD']: + error = 'Invalid password' + else: + session['logged_in'] = True + flash('You were logged in') + return redirect(url_for('show_entries')) + return render_template('login.html', error=error) + +@app.route('/logout') +def logout(): + session.pop('logged_in', None) + flash('You were logged out') + return redirect(url_for('show_entries')) + +if __name__ == '__main__': + app.run() diff --git a/install.py b/install.py new file mode 100644 index 0000000..f6f73de --- /dev/null +++ b/install.py @@ -0,0 +1,11 @@ +from flask_site import * +from contextlib import closing + +def init_db(): + with closing(connect_db()) as db: + with app.open_resource('schema.sql', mode='r') as f: + db.cursor().executescript(f.read()) + db.commit() + +if __name__ == "__main__": + init_db() diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..6ace639 --- /dev/null +++ b/schema.sql @@ -0,0 +1,7 @@ +drop table if exists entries; +create table entries ( + id integer primary key autoincrement, + title text not null, + text text not null + +); diff --git a/static/css/style.css b/static/css/style.css new file mode 100644 index 0000000..cbe4564 --- /dev/null +++ b/static/css/style.css @@ -0,0 +1,18 @@ +body { font-family: sans-serif; background: #eee; } +a, h1, h2 { color: #377ba8; } +h1, h2 { font-family: Georgia, serif; margin: 0; } +h1 { border-bottom: 2px solid #eee; } +h2 { font-size: 1.2em; } + +.page { margin: 2em auto; width: 35em; border: 5px solid #ccc; + padding: 0.8em; background: white; } +.entries { list-style: none; margin: 0; padding: 0; } +.entries li { margin: 0.8em 1.2em; } +.entries li h2 { margin-left: -1em; } +.add-entry { font-size: 0.9em; border-bottom: 1px solid #ccc; } +.add-entry dl { font-weight: bold; } +.metanav { text-align: right; font-size: 0.8em; padding: 0.3em; + margin-bottom: 1em; background: #fafafa; } +.flash { background: #cee5F5; padding: 0.5em; + border: 1px solid #aacbe2; } +.error { background: #f0d6d6; padding: 0.5em; } diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..bcae185 --- /dev/null +++ b/templates/login.html @@ -0,0 +1,14 @@ +{% extends "master.html" %} +{% block body %} +

Login

+ {% if error %}

Error: {{ error }}{% endif %} +

+
+
Username: +
+
Password: +
+
+
+
+{% endblock %} diff --git a/templates/master.html b/templates/master.html new file mode 100644 index 0000000..169b578 --- /dev/null +++ b/templates/master.html @@ -0,0 +1,17 @@ + +Flask Site + +
+

Flask Site

+
+ {% if not session.logged_in %} + log in + {% else %} + log out + {% endif %} +
+ {% for message in get_flashed_messages() %} +
{{ message }}
+ {% endfor %} + {% block body %}{% endblock %} +
diff --git a/templates/show_entries.html b/templates/show_entries.html new file mode 100644 index 0000000..25042d8 --- /dev/null +++ b/templates/show_entries.html @@ -0,0 +1,21 @@ +{% extends "master.html" %} +{% block body %} + {% if session.logged_in %} +
+
+
Title: +
+
Text: +
+
+
+
+ {% endif %} + +{% endblock %}