-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.py
271 lines (216 loc) · 5.86 KB
/
routes.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
"""
Routes and views for the bottle application.
"""
from bottle import route, view,redirect,request,template,static_onefile
from models import *
from config import baseinfo,getfilepath
from pony.orm import db_session
import html
from os.path import abspath,dirname,join,splitext
from urllib.parse import unquote,urlencode
@route('/home')
@view('home')
def home():
"""Renders the home page."""
bf=baseinfo()
dd= dict(
title='Home',
info=bf
)
return dd
@route('/member')
@db_session
def contact():
"""Renders the contact page."""
names=select(u.name for u in User )
bf=baseinfo()
dd= dict(title='member',info=bf,names=names)
return template('member',dd)
@route('/logout')
@view('home')
def logout():
s=request.environ.get('beaker.session')
s.delete()
bf=baseinfo()
dd= dict(
title='Home',
info=bf
)
return dd
@route('/login')
@view('login')
def login():
bf=baseinfo()
dd= dict(
title='login',
info=bf
)
return dd
@route('/login',method='POST')
@view('login')
def login():
username = request.forms.username
password = request.forms.password
if username.strip() and password.strip():
u = check_login(username, password)
if u:
s = request.environ.get('beaker.session')
s['user'] = u.name
s['id'] = u.id
s['auth']=True
s.save()
return redirect('/')
bf=baseinfo()
dd= dict(
title='Home',
info=bf
)
return dd
faviconico='/static/images/book.gif'
@route('/favicon.ico')
def favicon():
return faviconico
@route('/problem')
@db_session
def blog():
page=request.query.page or '1'
plimit=5
problems=select(b for b in Problem )
problemscount=problems.count()
pnum= int( problemscount/plimit)
if problemscount>pnum*plimit:
pnum=pnum+1
problems=problems.page(int(page),plimit)
bf = baseinfo()
dd = dict( title='Problem',info=bf, problems=problems,pagecount=pnum,cpage=int(page))
return template('problem',dd)
@route('/problem/:id')
@db_session
def detail(id):
b=Problem[id]
bf=baseinfo()
dd= dict(title='Detail',info=bf,problem=b)
return template('detail',dd)
@route('/add',method='POST')
@db_session
def add():
bf = baseinfo()
if bf.auth:
title = request.forms.title
describe = request.forms.content
if title.strip() and describe.strip():
describe=html.unescape(describe)
b = Problem(title=title, describe=describe,ratio='',level=0, update=today, author=bf.id)
commit()
# must commit,otherwise not add anything
#必须要提交,否则添加不了内容
return redirect("/problem/%d" % b.id)
else:
return redirect('/problem')
@route('/add')
@view('add')
def add():
bf = baseinfo()
if bf.auth:
dd = dict(
title='Add news',
info=bf
)
return dd
else:
return redirect('/problem')
@route('/edit/:id')
@db_session
def edit(id):
bf=baseinfo()
if bf.auth:
b=Problem[id]
dd= dict(title='Edit',info=bf,blog=b)
return template('edit',dd)
else:
return redirect("/problem/%s" % id) # id is string , must be %s ,not be %d
@route('/edit/:id',method='POST')
@db_session
def add(id):
title = request.forms.title
describe = request.forms.content
if title.strip() and describe.strip():
describe=html.unescape(describe)
b = Problem[id]
b.title=title
b.describe=describe
b.update=today
commit()
# We must put the commit() command here, but it is very necessary
# because PonyPlugin will take no care of this!!!
return redirect("/problem/%d" % b.id)
@route('/register')
@view('register')
def register():
bf=baseinfo()
dd= dict(
title='register',
info=bf
)
return dd
@route('/register',method='POST')
@view('register')
@db_session
def register():
username = request.forms.username
password = request.forms.password
signature = request.forms.signature
if username.strip() and password.strip():
u = User( name=username, signature=signature, password=password, regdate=today,avatar='')
commit()
s = request.environ.get('beaker.session')
s['name'] = u.name
s['id'] = u.id
s['auth'] = True
s.save()
return redirect('/')
else:
return "need username and password! "
@route('/python')
@db_session
def program():
page = request.query.page or '1'
plimit = 5
problems = select(p for p in Problem)
problemscount = problems.count()
pnum = int(problemscount / plimit)
if problemscount > pnum * plimit:
pnum = pnum + 1
problems = problems.page(int(page), plimit)
bf = baseinfo()
dd = dict(title='Problems', info=bf, problems=problems, pagecount=pnum, cpage=int(page))
return template('source', dd)
@route('/')
@route('/python/:id')
@db_session
def code(id=1):
p=Problem[id]
problems=select(m for m in Problem)
bf=baseinfo()
dd= dict(title='Code Detail',info=bf,problems=problems,program=p)
return template('source',dd)
@route('/write',method='POST')
@db_session
def write():
bf = baseinfo()
if bf.auth:
title = request.forms.title
pcode = request.forms.code
if title.strip() and pcode.strip():
pcode=html.unescape(pcode)
p=Problem(title=title,code=pcode,update=today,level='0')
commit()
# must commit,otherwise not add anything
#必须要提交,否则添加不了内容
return redirect("/program/%d" % p.id)
else:
return redirect('/program')
@route('/judge',method='POST')
def judge():
progradcode=request.forms.code
return progradcode