-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.py
108 lines (92 loc) · 3.26 KB
/
main.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Google Code-In unofficial leaderboard
# Copyright (C) 2013-2015 Ignacio Rodríguez <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import os
import json
import operator
from utils import *
from flask import Flask
from flask import render_template
from flask import redirect
from flask import request
from flask import make_response
app = Flask(__name__)
GCI = GCIUtils()
@app.route('/gci<year>/', defaults={'year': str(CURRENT_CONTEST)})
@app.route('/gci<year>')
@app.route('/gci<year>/')
@app.route('/')
def start_index(year=CURRENT_CONTEST):
return redirect('/gci' + str(year) + '/org/all')
@app.errorhandler(404)
def page_not_found(e):
return render_template('errors/404.html')
@app.route('/org/<orgname>', defaults={'year': str(CURRENT_CONTEST)})
@app.route('/org/<orgname>/', defaults={'year': str(CURRENT_CONTEST)})
@app.route('/gci<year>/org/<orgname>')
@app.route('/gci<year>/org/<orgname>/')
def leaderboard_org(year, orgname):
org_title = GCI.get_org_name(int(year), orgname)
tags = GCI.get_tasks_count(int(year), orgname)
tasks = GCI.get_tasks(int(year), orgname)
return render_template(
'org.html',
orgname=org_title,
tags=tags,
totalTasks=tasks["totalTasks"],
userTasks=tasks["userTasks"],
totalStudents=len(
CONTEST_LEADERBOARD[int(year)][orgname]),
org_id=orgname,
orgs=tasks["pageOrgs"],
year=year)
@app.route(
'/student/<studentName>',
defaults={
'year': str(CURRENT_CONTEST),
'org': u'all'})
@app.route('/gci<year>/student/<studentName>/<org>')
def student(year, studentName, org):
studentTasks = GCI.get_student_tasks(studentName, int(year), org)
tags = studentTasks['total_tags']
tasks = GCI.get_tasks(int(year), org)
studentPos = 1
for x in tasks["userTasks"]:
if x[1]['name'] == studentName:
break
studentPos += 1
studentTasks['tasks'] = sorted(studentTasks['tasks'].iteritems(),
key=lambda x: x[1]['title'],
reverse=False)
if not studentTasks['tasks']:
return render_template('errors/404.html')
try:
orgname = GCI.get_org_name(int(year), org)
except KeyError:
return render_template('errors/404.html')
return render_template(
'student.html',
tags=tags,
studentName=studentName,
orgname=orgname,
tasks=studentTasks['tasks'],
year=year,
orgs=tasks["pageOrgs"],
studentPos=studentPos)
if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0", port=int(sys.argv[1]))