-
Notifications
You must be signed in to change notification settings - Fork 0
/
newsapp.py
185 lines (169 loc) · 5.99 KB
/
newsapp.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
#!/usr/bin/python2.7
#coding:utf-8
from settings import route_app,render,pagesize
from settings import errorpage,auth_user,auth_admin
import store
import web
app = route_app()
@app.route("")
class routeto():
def GET(self):
raise web.seeother("/news/",absolute=True)
@app.route("/")
class index():
def GET(self):
web.header("Content-Type","text/html; charset=utf-8")
page = int(web.input().get("page",1))
offset = (page-1)*pagesize
stats = store.get_post_stats(False)
tops = store.Post.where(status=1).order_by("modified desc")[offset:offset+pagesize]
tags = store.get_post_tags(30)
return render("news.html",
tops = tops,
stats=stats,
tags=tags,
page=page)
@app.route("/feed")
class index():
def GET(self):
web.header("Content-Type","text/xml; charset=utf-8")
tops = store.Post.where(status=1).order_by("modified desc")[:50]
return render("newsfeed.html",tops = tops,lastBuildDate=store.currtime())
@app.route("/audit")
class index():
def GET(self):
web.header("Content-Type","text/html; charset=utf-8")
page = int(web.input().get("page",1))
offset = (page-1)*pagesize
tops = store.Post.where(status=0).order_by("modified desc")[offset:pagesize]
return render("news_audit.html",
tops = tops,
page=page)
@app.route("/tag/(.*)")
class index():
def GET(self,tag):
web.header("Content-Type","text/html; charset=utf-8")
page = int(web.input().get("page",1))
offset = (page-1)*pagesize
tags = store.get_post_tags(30)
stats = store.get_post_stats(tag)
tops = store.Post.where("status=1 and tags like %s",'%%%s%%'%tag).order_by("modified desc")[offset:pagesize]
return render("news.html",
ctag=tag,
tops = tops,
stats=stats,
tags=tags,
page=page)
@app.route("/post/add")
class add_post():
@auth_user
def GET(self):
web.header("Content-Type","text/html; charset=utf-8")
codeid = web.input().get("codeid")
return render("post_add.html",codeid=codeid)
@auth_user
def POST(self):
post = store.Post(id=store.nextid())
user = web.ctx.session.get("user")
form = web.input()
post.codeid = form.get("codeid")
post.title = form.get("title")
post.tags = form.get("tags","other")[:255]
post.content = form.get("content")
post.userid = user.id
post.created = post.modified = store.currtime()
post.status = 0
if not post.title or not post.content:
return errorpage(u"请输入完整数据")
try:
post.save()
raise web.seeother("/news/",absolute=True)
except Exception, e:
return errorpage("add post error %s"%e)
@app.route("/comment/add")
class add_comment():
def POST(self):
try:
comment = store.Comment()
comment.id = store.nextid()
user = web.ctx.session.get("user")
comment.userid = user and user.id or ''
form = web.input()
comment.author = user and user.username or form.get("author")
comment.content = form.get("content")
comment.postid = form.get("postid")
comment.email = user and user.email or form.get("email")
comment.url = user and user.url or form.get("url")
comment.ip = web.ctx.ip
comment.agent = web.ctx.env.get('HTTP_USER_AGENT')
comment.status = comment.userid and 1 or 0
comment.created = store.currtime()
if not comment.content:
raise Exception('content not empty')
if not comment.userid:
if not comment.author or not comment.email :
raise Exception('author email not empty')
comment.save()
store.Comment.commit()
raise web.seeother("/news/post/view/%s"%comment.postid,absolute=True)
except Exception, e:
return errorpage("add comment error %s"%e)
@app.route("/post/audit1/(.*)")
class update_post():
@auth_admin
def GET(self,uid):
try:
store.Post.get(id=uid).update(status=1)
raise web.seeother("/news/audit",absolute=True)
except Exception, e:
return errorpage("audit error %s"%e)
@app.route("/post/audit2/(.*)")
class update_post():
@auth_admin
def GET(self,uid):
try:
store.Post.get(id=uid).update(status=2)
raise web.seeother("/news/audit",absolute=True)
except Exception, e:
return errorpage("audit error %s"%e)
@app.route("/post/update")
class update_post():
@auth_user
def GET(self):
pass
@app.route("/post/view/(.*)")
class get_post():
def GET(self,uid):
web.header("Content-Type","text/html; charset=utf-8")
try:
page = int(web.input().get("page",1))
post = store.Post.get(uid)
post.hits += 1
post.save()
tags = store.get_post_tags(30)
codeid = post.codeid
code = None
if codeid:
code = store.Code.get(codeid)
comments = store.Comment.where(postid=uid).order_by("created desc")[(page-1)*pagesize:pagesize]
return render("post_view.html",
tags=tags,
post=post,
comments=comments,
page=page,
code=code,
pagename=post.title)
except Exception,e:
return errorpage("error %s"%e)
@app.route("/post/rec/(.*)")
class index():
def GET(self,uid):
web.header("Content-Type","text/html; charset=utf-8")
post = store.Post.get(uid)
try:
if post:
post.recs += 1
post.save()
except:
pass
raise web.seeother("/news/",absolute=True)