/
index.py
53 lines (46 loc) · 1.24 KB
/
index.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*
import os
import web
import model
import json
from config import env
from datetime import date, datetime
import jinja2
from jinja2 import Environment
urls = (
'/','Index',
'/add','AddMood',
'/view', 'ViewMood'
)
app = web.application(urls, globals())
class Index:
def GET(self):
posts = model.get_posts()
template = env.get_template('index.html')
return template.render(moods=posts)
class AddMood:
def GET(self):
mood_data = web.input()
template = env.get_template('add.html')
return template.render(mood_data)
def POST(self):
mood_data = web.input()
model.new_post(mood_data.mood_content)
raise web.seeother('/')
class ViewMood:
def GET(self):
web.header('Content-type','application/json')
id = web.input(_method='get').postId
post = model.get_post(int(id))
return json.dumps(post,cls=CJsonEncoder)
# 扩展JSONEncoder用来格式化时间
class CJsonEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime):
return obj.strftime('%Y-%m-%d %H:%M:%S')
elif isinstance(obj, date):
return obj.strftime('%Y-%m-%d')
else:
return json.JSONEncoder.default(self, obj)
if __name__ == '__main__':
app.run()