Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions protream/0023/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
venv
*.pyc
*.swp
db.sqlite3
2 changes: 2 additions & 0 deletions protream/0023/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Message Board
This my first verion of message board, based on flask and sqlite.
90 changes: 90 additions & 0 deletions protream/0023/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"""
Message Board
~~~~~~~~~~~~~

A simple message board application written with Flask and sqlite3.
Since it's a small application, I have not use SQLAlchemy and wtf.

:copyright: (c) 2015 by proteam@gmail.com.
:license: BSD.
"""
import os
import sqlite3
from datetime import datetime
from flask import Flask, g, request, render_template, flash

app = Flask(__name__)

# configuration
app.config.update(dict(
DATABASE = os.path.join(app.root_path, 'db.sqlite3'),
SECRET_KEY = 'secret key'
))

app.config.from_object(__name__)


def connect_db():
"""Connectts to the specific database."""
return sqlite3.connect(app.config['DATABASE'])


def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = connect_db()
return db


def init_db():
"""Initalizes the database.

>>> from run import init_db
>>> init_db()
"""
with app.app_context():
db = get_db()
with app.open_resource('schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
db.commit()


@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()
g.db.close()


@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
username = request.form['username']
message = request.form['message']
time = datetime.now()
if username and message:
g.db.execute(
'insert into message_board (username, message, time) values (?, ?, ?)',
[username, message, time]
)
g.db.commit()
flash('Your meassage was successfully posted.')
else:
flash('Username or message can not be blank.')
cur = g.db.execute('select username, message, time \
from message_board order by time desc')
posts = [dict(username=row[0], message=row[1], time=row[2]) \
for row in cur.fetchall()]
return render_template('index.html', posts=posts)


if __name__ == '__main__':
import doctest
doctest.testmod()
app.run(debug=True)
7 changes: 7 additions & 0 deletions protream/0023/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
drop table if exists message_board;
create table message_board (
id integer primary key autoincrement,
username varchar(64) not null,
message text not null,
time datetime not null
);
31 changes: 31 additions & 0 deletions protream/0023/static/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
* {
margin: 0px;
padding: 0px;
}

body {
font-family: Verdana,"BitStream vera Sans",Helvetica,Sans-serif;
font-size: 12px;
color: #333;
margin: 20px auto;
}

#container {
width: 600px;
margin: 0px auto;
}

#container h1 {
text-align: center;
color: #AAA;
}

.message {
margin: 5px 0px;
padding: 5px;
border-bottom: 1px dotted #AAA;
}

.timestamp {
float: right;
}
34 changes: 34 additions & 0 deletions protream/0023/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Message Board</title>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div id="container">
<h1>留言板</h1>
{% for message in get_flashed_messages() %}
<p>{{ message }}</p>
{% endfor %}
<form action="{{ url_for('index') }}" method=post>
<dl>
<dt>你的名字:</dt>
<dd><input type="text" size=24 name=username></dd>
<dt>你的留言:</dt>
<dd><textarea rows=5 cols=64 name=message></textarea></dd>
<dd><input type="submit" value="发布"></dd>
<br />
{% for post in posts %}
<span><strong>{{ post.username }}</strong></span>
<span class="timestamp">{{ post.time }}</span>
<div class="message">
{{ post.message }}
</div>
{% endfor %}

</dl>
</form>
</div>
</body>
</html>