forked from gmunkhbaatarmn/codeforces-mn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
398 lines (304 loc) · 11.3 KB
/
app.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import re
import json
import time
import parse
from hashlib import md5
from markdown2 import markdown
from natrix import app, route, data, info, warning, taskqueue
from parse import cf_get_active_users, tc_get_active_users, date_format
from models import Problem, Contest, Suggestion
app.config["session-key"] = "Tiy3ahhiefux2hailaiph4echidaelee3daighahdahruPhoh"
app.config["context"] = lambda x: {
"date_format": date_format,
"top": data.fetch("Rating:contribution", []),
"codeforces": data.fetch("Rating:codeforces", []),
"topcoder": data.fetch("Rating:topcoder", []),
"markdown": lambda x: markdown(x, extras=["code-friendly"]),
"suggestion_count": Suggestion.all().count(),
"count_all": data.fetch("count_all"),
"count_done": data.fetch("count_done"),
}
# Home
@route(":error-404")
def not_found(x):
x.render("error-404.html")
@route(":error-500")
def internal_error(x):
x.response("Sorry, Something technically wrong.")
@route("/")
def home(x):
x.render("home.html")
# Contest
@route("/contests")
def contest_list(x, page="1"):
# todo: translated problem count in contest list
offset = 100 * (int(page) - 1)
contests = Contest.all().order("-id").fetch(100, offset=offset)
count = Contest.all().count(1000)
x.render("contest-list.html", locals())
@route("/contests/page/(\d+)")
def contest_list_paged(x, page):
contest_list(x, page)
@route("/contest/(\d+)")
def contest_dashboard(x, id):
contest = Contest.find(id=int(id))
x.render("contest-dashboard.html", locals())
@route("/contest/(\d+)/problem/(\w+)")
def contest_problem(x, contest_id, letter):
contest = Contest.find(id=int(contest_id))
code = dict(contest.problems)[letter]
problem = Problem.find(code=code)
if problem.credits:
x.render("contest-problem.html", locals())
else:
x.render("contest-problem-en.html", locals())
# Problemset
@route("/problemset")
def problemset_index(x, page="1"):
# todo: link of non translated problem
offset = 100 * (int(page) - 1)
problems = Problem.all().order("-code").fetch(100, offset=offset)
x.render("problemset-index.html", locals())
@route("/problemset/page/(\d+)")
def problemset_paged(x, page):
problemset_index(x, page)
@route("/problemset/problem/(\d+)/(\w+)")
def problemset_problem(x, contest_id, index):
problem = Problem.find(code="%3s-%s" % (contest_id, index))
if problem.credits:
x.render("problemset-problem.html", locals())
else:
x.render("problemset-problem-en.html", locals())
@route("/problemset/problem/(\d+)/(\w+)/edit")
def problemset_translate(x, contest_id, index):
problem = Problem.find(code="%3s-%s" % (contest_id, index))
x.render("problemset-translate.html", locals())
# Rating
@route("/ratings")
def ratings(x):
x.render("ratings.html")
@route("/ratings/update")
def ratings_update(x):
taskqueue.add(url="/ratings/update")
@route("/ratings/update#post")
def ratings_update_task(x):
start = time.time()
data.write("Rating:codeforces", cf_get_active_users())
data.write("Rating:topcoder", tc_get_active_users())
info("Executed seconds: %.1f" % (time.time() - start))
x.response("OK")
# Suggestion
@route("/suggestion")
def suggestion_index(x):
if x.request.query == "logout":
x.session.pop("moderator", None)
suggestions = Suggestion.all().order("-added")
x.render("suggestion-index.html", **locals())
@route("/suggestion#login")
def suggestion_login(x):
if x.request["password"] in data.fetch("moderators", {}):
x.session["moderator"] = 1
x.redirect("/suggestion")
suggestions = Suggestion.all().order("-added")
login_failed = 1
x.render("suggestion-index.html", **locals())
@route("/suggestion#insert")
def suggestion_insert(x):
# if not x.session.get("moderator"):
# x.redirect("/suggestion")
code = x.request["code"]
source = x.request["source"].strip().decode("utf-8")
source = source.replace("\r\n", "\n")
title = source.split("\n", 1)[0][2:]
source = source.split("\n", 1)[1].strip()
credits = source.rsplit("\n", 1)[1][3:]
source = source.rsplit("\n", 1)[0].strip()
note = ""
# '## Temdeglel'
heading = u"\n## \u0422\u044d\u043c\u0434\u044d\u0433\u043b\u044d\u043b\n"
if heading in source:
note = source.split(heading, 1)[1]
source = source.split(heading, 1)[0]
s = Suggestion(code=code)
s.title = title
s.content = source
s.note = note
s.credits = credits
s.save()
x.redirect("/suggestion", delay=1)
@route("/suggestion#publish")
def suggestion_publish(x):
if not x.session.get("moderator"):
x.redirect("/suggestion")
id = x.request["id"]
suggestion = Suggestion.get_by_id(int(id))
problem = Problem.find(code=suggestion.code)
source = x.request["source"].strip().decode("utf-8")
source = source.replace("\r\n", "\n")
title = source.split("\n", 1)[0][2:]
source = source.split("\n", 1)[1].strip()
credits = source.rsplit("\n", 1)[1][3:]
source = source.rsplit("\n", 1)[0].strip()
note = ""
# '## Temdeglel'
heading = u"\n## \u0422\u044d\u043c\u0434\u044d\u0433\u043b\u044d\u043b\n"
if heading in source:
note = source.split(heading, 1)[1]
source = source.split(heading, 1)[0]
problem.title = title
problem.content = source
problem.note = note
problem.credits = credits
problem.save()
# cache count query
count_all = Problem.all().count(3000)
count_done = Problem.all().filter("credits >", "").count(3000)
data.write("count_all", count_all)
data.write("count_done", count_done)
# update contribution
contribution = {}
for p in Problem.all().filter("credits !=", ""):
translators = p.credits.split(", ")
for t in translators:
point = (p.meta.get("credit_point") or 1.0) / len(translators)
contribution[t] = contribution.get(t, 0.0) + point
contribution = sorted(contribution.items(), key=lambda t: -t[1])
data.write("Rating:contribution", contribution)
suggestion.delete()
x.redirect(str(problem.link), delay=1)
@route("/suggestion#delete")
def suggestion_delete(x):
if not x.session.get("moderator"):
x.redirect("/suggestion")
id = x.request["id"]
suggestion = Suggestion.get_by_id(int(id))
suggestion.delete()
x.redirect("/suggestion")
@route("/suggestion/(\d+)")
def suggestion_review(x, id):
if not x.session.get("moderator"):
x.redirect("/suggestion")
suggestion = Suggestion.get_by_id(int(id))
problem = Problem.find(code=suggestion.code)
x.render("suggestion-review.html", **locals())
# Others
@route("/extension")
def extension(x):
# 1. translated problems
arr = [p.code for p in Problem.all().filter("credits >", "")]
x.response.write("|".join([p.strip() for p in sorted(arr)]) + "\n")
# 2. todo: contests "translated/all"
x.response.write("001:3/3\n")
# 3. contribution
contribution = data.fetch("Rating:contribution")
x.response.write("|".join(["%s:%s" % (k, v) for k, v in contribution]))
x.response.write("\n")
# 4. all problems count
x.response.write("%s\n" % Problem.all().count(10000))
'''
extension old codes
all_problem = dict(Data.fetch("All:problem"))
all_similar = Data.fetch("All:similar")
all_contest = Data.fetch("All:contest")
contribution = Data.fetch("Rating:contribution")
for k, v in all_similar.items():
all_problem[v] = all_problem[k]
def nozero(x):
while x.startswith("0"):
x = x[1:]
return x
all_problem = sorted(filter(lambda x: x[1][1], all_problem.items()),
key=lambda x: x[0])
self.response.write("|".join([nozero(i[0]) for i in all_problem]) + "\n")
self.response.write("|".join(["%s:%s/%s" % (i[0], i[1][1], i[1][2]) for i
in all_contest]) + "\n")
self.response.write("|".join(["%s:%s" % (k, v) for k, v in contribution]) +
"\n")
self.response.write("%s\n" % Data.fetch("Contribution:full"))
'''
@route("/update")
def update(x):
" new contests, new problems "
# - Check problemset first page
new_problems = 0
for code, title in parse.problemset(1):
if not re.search("^\d+[A-Z]$", code):
warning("SKIPPED: %s" % code)
continue
code = "%3s-%s" % (code[:-1], code[-1])
p = Problem.find(code=code) or Problem(code=code)
p.title = p.title or title
if p.content:
continue
# new problem found
new_problems += 1
meta = parse.problem(p.code)
p.content = meta.pop("content")
p.note = meta.pop("note")
p.meta_json = json.dumps(meta)
p.identifier = md5(json.dumps(meta["tests"])).hexdigest()
p.save()
# - Check contests first page
for id, name, start in parse.contest_history(1):
c = Contest.find(id=int(id)) or Contest(id=int(id))
if c.name:
continue
# new contest found
c.name = name
c.start = start
problems = {}
for letter, _ in parse.contest(c.id)["problems"]:
code = "%3s-%s" % (c.id, letter)
i = md5(json.dumps(parse.problem(code)["tests"])).hexdigest()
p = Problem.find(identifier=i)
if not p:
warning("not found: %s" % code)
continue
problems[letter] = p.code
c.problems_json = json.dumps(problems)
c.save()
# - Update problems count
if new_problems > 0:
count_all = data.fetch("count_all")
data.write("count_all", count_all + new_problems)
@route("/setup")
def setup(x):
if "localhost" not in x.request.host:
x.response("Deny: Only for development")
start_time = time.time()
# - Ratings
data.write("Rating:codeforces", cf_get_active_users())
data.write("Rating:topcoder", tc_get_active_users())
# - Contests
for page in range(5, 0, -1):
info("Contests page: %s" % page)
for id, name, start in parse.contest_history(page):
c = Contest.find(id=id) or Contest(id=id)
c.name = name
c.start = start
c.save()
# - Problemset
for page in range(20, 0, -1):
info("Problemset page: %s" % page)
datas = parse.problemset(page)
for code, title in datas:
if not re.search("^\d+[A-Z]$", code):
warning("SKIPPED: %s" % code)
continue
code = "%3s-%s" % (code[:-1], code[-1])
p = Problem.find(code=code) or Problem(code=code)
p.title = title
# fill with fake data
p.content, p.note, p.credits, p.meta_json = parse.mock_problem()
p.save()
# - Contribution point from datastore
contribution = {}
for p in Problem.all().filter("credits !=", ""):
translators = p.credits.split(", ")
for t in translators:
point = (p.meta.get("credit_point") or 1.0) / len(translators)
contribution[t] = contribution.get(t, 0.0) + point
contribution = sorted(contribution.items(), key=lambda t: -t[1])
data.write("Rating:contribution", contribution)
info("Executed seconds: %.1f" % (time.time() - start_time))
x.response("Executed seconds: %.1f" % (time.time() - start_time))