Skip to content

Commit 1b63362

Browse files
committed
Merge pull request Show-Me-the-Code#57 from protream/master
Finished 0023
2 parents 3c6f029 + fcc56df commit 1b63362

6 files changed

Lines changed: 168 additions & 0 deletions

File tree

protream/0023/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
venv
2+
*.pyc
3+
*.swp
4+
db.sqlite3

protream/0023/README

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Message Board
2+
This my first verion of message board, based on flask and sqlite.

protream/0023/run.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"""
2+
Message Board
3+
~~~~~~~~~~~~~
4+
5+
A simple message board application written with Flask and sqlite3.
6+
Since it's a small application, I have not use SQLAlchemy and wtf.
7+
8+
:copyright: (c) 2015 by proteam@gmail.com.
9+
:license: BSD.
10+
"""
11+
import os
12+
import sqlite3
13+
from datetime import datetime
14+
from flask import Flask, g, request, render_template, flash
15+
16+
app = Flask(__name__)
17+
18+
# configuration
19+
app.config.update(dict(
20+
DATABASE = os.path.join(app.root_path, 'db.sqlite3'),
21+
SECRET_KEY = 'secret key'
22+
))
23+
24+
app.config.from_object(__name__)
25+
26+
27+
def connect_db():
28+
"""Connectts to the specific database."""
29+
return sqlite3.connect(app.config['DATABASE'])
30+
31+
32+
def get_db():
33+
db = getattr(g, '_database', None)
34+
if db is None:
35+
db = g._database = connect_db()
36+
return db
37+
38+
39+
def init_db():
40+
"""Initalizes the database.
41+
42+
>>> from run import init_db
43+
>>> init_db()
44+
"""
45+
with app.app_context():
46+
db = get_db()
47+
with app.open_resource('schema.sql', mode='r') as f:
48+
db.cursor().executescript(f.read())
49+
db.commit()
50+
51+
52+
@app.before_request
53+
def before_request():
54+
g.db = connect_db()
55+
56+
57+
@app.teardown_request
58+
def teardown_request(exception):
59+
db = getattr(g, 'db', None)
60+
if db is not None:
61+
db.close()
62+
g.db.close()
63+
64+
65+
@app.route('/', methods=['GET', 'POST'])
66+
def index():
67+
if request.method == 'POST':
68+
username = request.form['username']
69+
message = request.form['message']
70+
time = datetime.now()
71+
if username and message:
72+
g.db.execute(
73+
'insert into message_board (username, message, time) values (?, ?, ?)',
74+
[username, message, time]
75+
)
76+
g.db.commit()
77+
flash('Your meassage was successfully posted.')
78+
else:
79+
flash('Username or message can not be blank.')
80+
cur = g.db.execute('select username, message, time \
81+
from message_board order by time desc')
82+
posts = [dict(username=row[0], message=row[1], time=row[2]) \
83+
for row in cur.fetchall()]
84+
return render_template('index.html', posts=posts)
85+
86+
87+
if __name__ == '__main__':
88+
import doctest
89+
doctest.testmod()
90+
app.run(debug=True)

protream/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 message_board;
2+
create table message_board (
3+
id integer primary key autoincrement,
4+
username varchar(64) not null,
5+
message text not null,
6+
time datetime not null
7+
);

protream/0023/static/style.css

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
* {
2+
margin: 0px;
3+
padding: 0px;
4+
}
5+
6+
body {
7+
font-family: Verdana,"BitStream vera Sans",Helvetica,Sans-serif;
8+
font-size: 12px;
9+
color: #333;
10+
margin: 20px auto;
11+
}
12+
13+
#container {
14+
width: 600px;
15+
margin: 0px auto;
16+
}
17+
18+
#container h1 {
19+
text-align: center;
20+
color: #AAA;
21+
}
22+
23+
.message {
24+
margin: 5px 0px;
25+
padding: 5px;
26+
border-bottom: 1px dotted #AAA;
27+
}
28+
29+
.timestamp {
30+
float: right;
31+
}

protream/0023/templates/index.html

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Message Board</title>
6+
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
7+
</head>
8+
<body>
9+
<div id="container">
10+
<h1>留言板</h1>
11+
{% for message in get_flashed_messages() %}
12+
<p>{{ message }}</p>
13+
{% endfor %}
14+
<form action="{{ url_for('index') }}" method=post>
15+
<dl>
16+
<dt>你的名字:</dt>
17+
<dd><input type="text" size=24 name=username></dd>
18+
<dt>你的留言:</dt>
19+
<dd><textarea rows=5 cols=64 name=message></textarea></dd>
20+
<dd><input type="submit" value="发布"></dd>
21+
<br />
22+
{% for post in posts %}
23+
<span><strong>{{ post.username }}</strong></span>
24+
<span class="timestamp">{{ post.time }}</span>
25+
<div class="message">
26+
{{ post.message }}
27+
</div>
28+
{% endfor %}
29+
30+
</dl>
31+
</form>
32+
</div>
33+
</body>
34+
</html>

0 commit comments

Comments
 (0)