forked from Inndy/git-web-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguestbook.py
More file actions
40 lines (32 loc) · 801 Bytes
/
guestbook.py
File metadata and controls
40 lines (32 loc) · 801 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import json
from bottle import run, route, template, static_file, view, request, redirect
@route('/')
@view('static/index.html')
def index():
try:
with open('data.json', 'r') as fin:
posts = json.load(fin)
except FileNotFoundError as e:
posts = []
return { 'posts': posts }
@route('/static/<filename:path>')
def static(filename):
return static_file(filename, root='static')
@route('/post', method='POST')
def create_post():
author = request.forms.get('author')
message = request.forms.get('message')
post = {
'author': author,
'message': message
}
try:
with open('data.json', 'r') as fin:
posts = json.load(fin)
except FileNotFoundError as e:
posts = []
posts.append(post)
with open('data.json', 'w') as fout:
json.dump(posts, fout)
return redirect('/')
run()