Skip to content

Commit ad5391f

Browse files
committed
add 0023
1 parent c8d9d4c commit ad5391f

7 files changed

Lines changed: 165 additions & 0 deletions

File tree

Jaccorot/0023/guestbook.db

3 KB
Binary file not shown.

Jaccorot/0023/guestbook.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import sqlite3
2+
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
3+
from contextlib import closing
4+
import time
5+
6+
DATABASE = 'guestbook.db'
7+
DEBUG = True
8+
SECRET_KEY = 'development key'
9+
10+
app = Flask(__name__)
11+
app.config.from_object(__name__)
12+
13+
def connect_db():
14+
return sqlite3.connect(app.config['DATABASE'])
15+
16+
def init_db():
17+
with closing(connect_db()) as db:
18+
with app.open_resource('schema.sql', mode='r') as f:
19+
db.cursor().executescript(f.read())
20+
db.commit()
21+
22+
@app.before_request
23+
def before_request():
24+
g.db = connect_db()
25+
26+
@app.teardown_request
27+
def teardown_request(exception):
28+
db = getattr(g, 'db', None)
29+
if db is not None:
30+
db.close()
31+
g.db.close()
32+
33+
34+
@app.route('/')
35+
def show_entires():
36+
cur = g.db.execute('select name,text,time from entries order by id desc')
37+
entries = [dict(name=row[0], text=row[1], time=row[2]) for row in cur.fetchall()]
38+
for i in entries:
39+
print i
40+
return render_template('show_entries.html', entries=entries)
41+
42+
@app.route('/add', methods=['POST'])
43+
def add_entry():
44+
if not session.get('logged_in'):
45+
abort(401)
46+
current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
47+
g.db.execute('insert into entries (name, text, time) values (?, ?, ?)',
48+
[request.form['name'], request.form['text'], current_time])
49+
g.db.commit()
50+
flash('New entry was successfully posted')
51+
return redirect(url_for('show_entires'))
52+
53+
@app.route('/login', methods=['GET', 'POST'])
54+
def login():
55+
error = None
56+
if request.method == 'POST':
57+
if request.form['username'] is None:
58+
error = "Invalid username"
59+
else:
60+
session['logged_in'] = True
61+
session['name'] = request.form['username']
62+
flash('You were logged in')
63+
return redirect(url_for('show_entires'))
64+
return render_template('login.html', error=error)
65+
66+
@app.route('/logout')
67+
def logout():
68+
session.pop('logged_in', None)
69+
flash('You were logged out')
70+
return redirect(url_for('show_entires'))
71+
72+
73+
74+
if __name__ == "__main__":
75+
app.run()

Jaccorot/0023/schema.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
DROP TABLE if EXISTS entries;
2+
CREATE TABLE entries(
3+
id INTEGER PRIMARY KEY autoincrement,
4+
name text NOT NULL ,
5+
text text NOT NULL,
6+
time datetime NOT NULL
7+
);

Jaccorot/0023/static/style.css

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
body { font-family: sans-serif; background: #eee; }
2+
a, h1, h2 { color: #377ba8; }
3+
h1, h2 { font-family: 'Georgia', serif; margin: 0; }
4+
h1 { border-bottom: 2px solid #eee; }
5+
h2 { font-size: 1.2em; }
6+
7+
.page { margin: 2em auto; width: 35em; border: 5px solid #ccc;
8+
padding: 0.8em; background: white; }
9+
.entries { list-style: none; margin: 0; padding: 0; }
10+
.entries li { margin: 0.8em 1.2em; }
11+
.entries li h2 { margin-left: -1em; }
12+
.add-entry { font-size: 0.9em; border-bottom: 1px solid #ccc; }
13+
.add-entry dl { font-weight: bold; }
14+
.metanav { text-align: right; font-size: 0.8em; padding: 0.3em;
15+
margin-bottom: 1em; background: #fafafa; }
16+
.flash { background: #cee5F5; padding: 0.5em;
17+
border: 1px solid #aacbe2; }
18+
.error { background: #f0d6d6; padding: 0.5em; }
19+
.time { text-align:right; }
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<title>Guestbook</title>
6+
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}"/>
7+
</head>
8+
<body>
9+
<div class="page">
10+
<h1>Guestbook</h1>
11+
<div class="metanav">
12+
{% if not session.logged_in %}
13+
<a href="{{ url_for('login') }}">log in</a>
14+
{% else %}
15+
<div>{{ session.name }}</div>
16+
<a href="{{ url_for('logout') }}">log out</a>
17+
{% endif %}
18+
</div>
19+
{% for message in get_flashed_messages() %}
20+
<div class="flash">{{ message }}</div>
21+
{% endfor %}
22+
{% block body %}{% endblock %}
23+
</div>
24+
</body>
25+
</html>

Jaccorot/0023/templates/login.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{% extends "layout.html" %}
2+
{% block body %}
3+
<h2>Login</h2>
4+
{% if error %}
5+
<p class="error"><strong>Error:</strong>{{ error }}</p>
6+
{% endif %}
7+
<form action="{{ url_for('login') }}" method="post">
8+
<dl>
9+
<dt>Username:</dt>
10+
<dd><input type="text" name="username"/></dd>
11+
<dd><input type="submit" value="Login"/></dd>
12+
</dl>
13+
</form>
14+
{% endblock %}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{% extends "layout.html" %}
2+
{% block body %}
3+
{% if session.logged_in %}
4+
<form action="{{ url_for('add_entry') }}" method=post class="add-entry">
5+
<dl>
6+
<dt>Text:</dt>
7+
<dd><textarea name="text" cols="40" rows="5"></textarea></dd>
8+
<dd><input type="submit" value="Share"/></dd>
9+
<input type="hidden" name="name" value="{{ session.name }}" />
10+
</dl>
11+
</form>
12+
{% endif %}
13+
<ul class="entries">
14+
{% for entry in entries %}
15+
<li>
16+
<h2>{{ entry.name }}</h2>
17+
<div class="time">{{ entry.time }}</div>
18+
{{ entry.text|safe }}
19+
20+
</li>
21+
{% else %}
22+
<li><em>Unbelieveable. No entries here so far</em></li>
23+
{% endfor %}
24+
</ul>
25+
{% endblock %}

0 commit comments

Comments
 (0)