first pass on guestbook
parent
1a3ce5c683
commit
c6451b4834
|
@ -0,0 +1,64 @@
|
|||
import json
|
||||
import os
|
||||
import time
|
||||
from sys import argv
|
||||
from tempfile import mkdtemp, mkstemp
|
||||
from flask import Flask, render_template, request, redirect
|
||||
|
||||
SITE_NAME = 'tilde.town'
|
||||
|
||||
app = Flask('~cgi')
|
||||
|
||||
def slurp(file_path):
|
||||
contents = None
|
||||
with open(file_path, 'r') as f:
|
||||
contents = f.read()
|
||||
return contents
|
||||
|
||||
def save_post(name, message):
|
||||
timestamp = time.time()
|
||||
data = {
|
||||
'name': name,
|
||||
'message': message,
|
||||
'timestamp': timestamp,
|
||||
}
|
||||
_, file_path = mkstemp(dir=app.config['DATA_DIR'])
|
||||
with open(file_path, 'w') as f:
|
||||
f.write(json.dumps(data))
|
||||
|
||||
@app.route('/random', methods=['GET'])
|
||||
def get_random():
|
||||
return "RANDOM"
|
||||
|
||||
@app.route('/guestbook', methods=['GET'])
|
||||
def get_guestbook():
|
||||
data_dir = app.config['DATA_DIR']
|
||||
# TODO sort by timestamp
|
||||
posts = map(lambda p: json.loads(slurp(os.path.join(data_dir, p))), os.listdir(data_dir))
|
||||
|
||||
context= {
|
||||
"site_name": SITE_NAME,
|
||||
"posts": posts,
|
||||
}
|
||||
return render_template('guestbook.html', **context)
|
||||
|
||||
@app.route('/guestbook', methods=['POST'])
|
||||
def post_guestbook():
|
||||
save_post(request.form['name'], request.form['message'])
|
||||
return redirect("/guestbook")
|
||||
|
||||
@app.route('/helpdesk', methods=['GET'])
|
||||
def helpdesk():
|
||||
return "HELPDESK"
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(argv) == 2:
|
||||
data_dir = argv[1]
|
||||
|
||||
else:
|
||||
data_dir = mkdtemp()
|
||||
|
||||
app.config['DEBUG'] = True
|
||||
app.config['DATA_DIR'] = data_dir
|
||||
|
||||
app.run()
|
|
@ -0,0 +1,32 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>{{site_name}} guestbook</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>{{site_name}} guestbook</h1>
|
||||
<marquee behavior="alternate"><em style="font-size:150%">~*~*~*~*~*~*~*~*~*~*~*say hello*~*~*~*~*~*~*~*~*~*~*~</em></marquee>
|
||||
<form action="/guestbook" method="post">
|
||||
<table border="1px">
|
||||
<tr>
|
||||
<th>name!</th>
|
||||
<th>message!</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<input type="text" name="name" />
|
||||
</td>
|
||||
<td>
|
||||
<textarea style="border:0px"name="message" cols="100" rows="10"></textarea>
|
||||
</td>
|
||||
</table>
|
||||
<input style="font-size:200%" type="submit" value="sign"/>
|
||||
</form>
|
||||
|
||||
{% for post in posts %}
|
||||
<p>
|
||||
{{post.name}} says: {{post.message}}
|
||||
</p>
|
||||
{% endfor %}
|
||||
</body>
|
||||
</html>
|
Reference in New Issue