-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
210 lines (170 loc) · 5.11 KB
/
server.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# coding=utf-8
import datetime
import web
from web.contrib.template import render_jinja
from conf import settings
from models.blog import Blogs
from models.category import Category
from utils.mongodbutils import MongodbUtil
from utils.templateutils import register
from utils.utils import get_session, render_markdown, md5, login_decorator
__author__ = 'sunshine'
urls = (
'/', 'Index',
'/login', 'Login',
'/manage', 'ManageBlog',
'/manage/blog', 'ManageBlog',
'/manage/blog/edit', 'ManageBlogEdit',
'/manage/blog/delete', 'ManageBlogDelete',
'/manage/category', 'ManageCategory',
'/manage/category/edit', 'ManageCategoryEdit',
'/blog/(.+)', 'Blog'
)
app = web.application(urls, locals())
# session
session = get_session(app)
render = render_jinja('templates', encoding='utf-8')
# 注册jinja2模板的自定义过滤器
register(render)
mu = MongodbUtil()
class Index:
"""
首页
"""
def GET(self):
blogs = mu.get_blog()
items = {}
for blog in blogs:
name = blog['category']
if name in items:
items[name].append(blog)
else:
items[name] = [blog]
blogs = items
return render.index(locals())
class Login:
"""
登录页
"""
def GET(self):
return render.login(locals())
def POST(self):
args = web.input()
username = args.get('username', None)
password = args.get('password', None)
if username and password:
password = md5(password)
user = mu.get_user(username, password)
if user:
# 登录成功保存客户端的session_id,如果关闭浏览器或者更换浏览器则session失效
# 登录的功能没做好,可以在登录成功后生成一个token返回,客户端每次请求都要带上这个token,这样就比较好的控制
session.login = web.cookies().webpy_session_id
raise web.seeother('/manage')
raise web.seeother('/login')
class ManageCategory:
"""
类别管理页
"""
@login_decorator
def GET(self):
categories = mu.get_category()
return render.manage_category(locals())
@login_decorator
def POST(self):
category_name = web.input().get('category_name', None)
if category_name:
category_name = category_name.strip()
category_id = web.input().get('category_id', None)
category = Category()
if category_id:
category.id = category_id
if category_name:
category.name = category_name
category.create_time = datetime.datetime.now()
mu.update_category(category)
raise web.seeother('/manage/category')
class ManageCategoryEdit:
"""
编辑类别(删除)
"""
@login_decorator
def GET(self):
category_id = web.input().get('id', None)
if category_id:
mu.del_category(category_id)
raise web.seeother('/manage/category')
class ManageBlogDelete:
"""
删除文章
"""
@login_decorator
def GET(self):
blog_id = web.input().get('id', None)
if blog_id:
mu.del_blog(blog_id)
raise web.seeother('/manage')
class ManageBlog:
"""
博客管理页
"""
@login_decorator
def GET(self):
blogs = mu.get_blog()
return render.manage_blog(locals())
class ManageBlogEdit:
"""
编辑文章
"""
@login_decorator
def GET(self):
blog_id = web.input().get("id", None)
categories = mu.get_category()
if blog_id:
blog = mu.get_blog(blog_id)
else:
blog = None
return render.manage_blog_edit(locals())
@login_decorator
def POST(self):
args = web.input(content={})
blog_id = args.get("id", None)
category_id = args.get("category_id", None)
title = args.get("title", None)
summary = args.get("summary", None)
blog = Blogs()
if title:
blog.title = title
if summary:
blog.summary = summary
if category_id:
category = mu.get_category(category_id)
blog.category = category['name']
blog.category_id = category_id
if 'content' in args:
source_content = args.content.file.read()
blog.source_content = source_content
content = render_markdown(source_content)
html_content = unicode(content, 'utf-8')
blog.html_content = html_content
blog.create_time = datetime.datetime.now()
if blog_id:
blog.id = blog_id
mu.update_blog(blog)
else:
mu.insert_blog(blog)
raise web.seeother('/manage')
class Blog:
"""
文章
"""
def GET(self, blog_id):
blog = mu.get_blog(blog_id)
if 'html_content' in blog:
content = blog['html_content']
else:
content = ''
return render.blog(locals())
if __name__ == '__main__':
web.config.debug = settings.debug
app.run()
# app.wsgifunc()