-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
362 lines (330 loc) · 13.6 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
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
from bottle import route, run, get, post, request, response
from bottle import static_file
import psycopg2
database = psycopg2.connect(database='201201217', user='201201217', password='201201217',host='10.100.71.21', port='5432')
cur = database.cursor()
print "Database initialized "
#HTML!
html_users = '''
<html>
<head>
<title></title>
<meta content="">
<style></style>
</head>
<body>
<h2> <a href="/"> Users </a> </h2> <br> <hr>
<ul>
%s
</ul>
</body>
</html>
'''
html_register = '''
<html>
<head>
<title></title>
<meta content="">
<style></style>
</head>
<body>
<form method = 'post'>
<br>
<label> <h2> Register. </h2>
<hr>
<br> Enter your details below
</label>
<br>
<label> Name </label> <input type="text" name="username"> </label> <br>
<label> Password </label> <input type="password" name="password"> </label> <br>
<label> Retype Password </label> <input type="password" name="password_r"> </label> <br>
<input type="submit">
</form>
</body>
</html>'''
html_home = '''
<html>
<head>
<title>Competition Organizer</title>
<meta content="">
<style></style>
</head>
<body>
<h1> Home </h1> <hr> <br>
<p>Please find yourself at home on this website.
We have tried our best to provide you with a reliable system that puts your time and efforts to good use, sharpening your programming skills which may help you build your part towards a better tomorrow
<br>
Welcome aboard!</p>
<a href="/login"> Login </a> | <a href="/register"> Register </a> <br>
<div> <a href="/users"> View all users </a> </div>
<div> <a href="/contests"> Or jump right in! </a> </div>
</body>
</html>
'''
html_login = '''
<html>
<head>
<title></title>
<meta content="">
<style></style>
</head>
<body>
<h2> <a href="/"> Login </a> </h2> <br> <hr>
<form method = "post">
<br>
<label> Name </label> <input type="text" name="username"> </label> <br>
<label> Password </label> <input type="password" name="password"> </label> <br>
<input type="submit">
</form>
</body>
</html>
'''
html_contests = '''
<html>
<head>
<title></title>
<meta content="">
<style></style>
</head>
<body>
<h2> <a href="/"> Contests </a> </h2> <br> <hr>
<p> Contests are an integral part of this website.
We believe that a healthy competition acts as a booster for anyone who inspires to push his skill set to the limits <br>
In the age of information, we are all but walking repositories of knowledge waiting to be tested. Gradually we shed what we seem not to require<br>
Ask yourself, when did you put yourself under some test of that knowledge of the algorithms and the frameworks you learnt overtime from countless books and websites, only to never work upon them ever?<br>
<br>
We understand and appreciate the kind of attitude people have towards programming. For the definition of this word grew from teaching large chunky machines where to punch holes appropriately to running mighty websites from a credit card sized computer<br>
To respect every taste towards this branch, we provide two basic divisions in the kind of activities you could involve yourself with</p><br>
<a href = '/pcontests'><div style="width:500px; height:100px; margin:10px; background-color:#FF4940"> Programming contests</div> </a>
<a href = '/dcontests'><div style="width:500px; height:100px; margin:10px; background-color:#FFB561"> Developing contests</div> </a>
</body>
</html>
'''
html_dcontests = '''
<html>
<head>
<title></title>
<meta content="">
<style></style>
</head>
<body>
<h2> <a href="/"> Developing Contests </a> </h2> <br> <hr>
<p> Developing contests are designed to put to test if you are capable of developing a complete software/website/extension. <br>This kind of contest is radically different than programming contests for this does not stress on your algorithmic skills as much as it tests your knowledge and practice of handling frameworks and APIs as well as immense amount of creativity<br>
You may contact the mentors if you ever feel the need to. </p> <br>
<h4> Open Contests </h4>
<p> These contests have been declared open, but the activities haven't yet begun. Now would be a nice time to start </p>
<ul> %(open)s </ul>
<h4> Active Contests </h4>
<p> Although they have begun, they have not yet ended. Don't let doubt take the best of you. Start now!</p>
<ul> %(active)s </ul>
</body>
</html>'''
html_pcontests = '''
<html>
<head>
<title></title>
<meta content="">
<style></style>
</head>
<body>
<h2> <a href="/"> Programming Contests </a></h2> <br> <hr>
<p> Programming contests are designed to challenge your algorithm skills. Please choose any contest from the list below and get going.
You may contact the mentors if you ever feel the need to. </p> <br>
<h4> Open Contests </h4>
<p> These contests have been declared open, but the activities haven't yet begun. Now would be a nice time to start </p>
<ul> %(open)s </ul>
<h4> Active Contests </h4>
<p> Although they have begun, they have not yet ended. Don't let doubt take the best of you. Start now!</p>
<ul> %(active)s </ul>
</body>
</html>
'''
@route("/")
def home():
return html_home
@route('/register')
def register():
return html_register
@route('/register', method='POST')
def register_post():
username = request.forms.get('username')
password = request.forms.get('password')
password_r = request.forms.get('password_r')
if len(username)>0 and len(password)>0 and len(password_r)>0:
if password == password_r:
cur.execute("INSERT INTO lab5.user_acc VALUES ('%(u)s', '%(p)s');" % {'u':username, 'p':password})
database.commit()
return "Successfully registered %s. <br> <a href='/'> << Go back </a>" % username
else:
return register()
else:
return register()
@route('/users')
def print_users():
cur.execute("SELECT * FROM lab5.user_acc;")
l_users = cur.fetchall()
usernames = ''
username= request.get_cookie("username")
print "I am %s" % username
for user in l_users:
if username == user[0]:
usernames += "<li> <a href='/user/%s'>" % username + user[0] + '</a>'
continue
usernames += '<li>' + user[0]
print user[0]
result = html_users % usernames
return result
@route('/user/<user>')
def print_user(user):
print user
cur.execute("SELECT * FROM lab5.user_acc WHERE username = '%s';" % user)
user_detail = cur.fetchall()
result_user = ''
if user_detail:
result_user += "<h1> <b> <a href='/'> %s </a></b> </h1><hr>" % user
cur.execute("SELECT DISTINCT teamname, T.teamid FROM lab5.members as M, lab5.team as T WHERE username = '%s' and M.teamid = T.teamid ;" % user)
teams = cur.fetchall()
result_user += '<h4> Member of teams - </h4> <ul>'
result_team = ''
for team in teams:
result_team += '<li> <a href="/team/%s">' % team[1] + team[0] + '</a>'
if len(result_team) < 1:
result_team = '<li> None'
result_user += result_team + '</ul>'
result_user += '<h4> Top Scoring submissions </h4> <ul>'
cur.execute("SELECT contestname, score, S.contestid FROM lab5.SUBMISSIONS as S, lab5.submits_user as Su, lab5.contests as C WHERE S.submissionid = Su.submissionid AND username = '%s' AND C.contestid = S.contestid ORDER BY score DESC LIMIT 3;" % user)
result_sub = ''
subs = cur.fetchall()
for sub in subs:
result_sub += '<li> <a href="/contests/%(link)s"> %(cname)s</a> - %(score)d ' % {'cname':sub[0], 'score':sub[1], 'link':sub[2]}
if len(result_sub) < 1:
result_sub = '<li> None'
result_user += result_sub + '</ul>'
result_user += '<b> #submissions - </b> '
cur.execute("SELECT COUNT(submissionid) FROM lab5.SUBMITS_USER WHERE username = '%s';" % user)
num_subs = cur.fetchall()[0]
print num_subs
result_user += str(num_subs)[1:-3]
#If logged in with same user, give option to delete the user as well.
if request.get_cookie("username") == user:
result_user += "<br><br><br><a href='/delete/%s'> Click to delete user!. Beware that you can't undo this action </a>" % user
else:
return print_users()
return result_user
@route('/team/<team>')
def print_team(team):
print team
cur.execute("SELECT * FROM lab5.team WHERE teamid = '%s';" % team)
team_detail = cur.fetchall()
if len(team_detail) > 1:
team_detail = team_detail[0]
else:
return home()
result_team = ''
print team_detail
if team_detail:
result_team += '<h1> <a href="/"> %s </a> </h1><hr>' % team_detail[0]
cur.execute("SELECT username FROM lab5.TEAM as T, lab5.members as M where M.teamid = T.teamid AND teamname = '%s'" % team_detail[0])
usernames = cur.fetchall()
result_team += "<h4> Members - </h4> <ul>"
for user in usernames:
result_team += "<li> <a href='/user/%(userid)s'> %(usernm)s </a>" % {'userid':user[0], 'usernm':user[0]}
else:
return home()
return result_team
@route('/login')
def login():
return html_login
@route('/login', method='POST')
def login_post():
username = request.forms.get('username')
password = request.forms.get('password')
cur.execute("SELECT * FROM lab5.user_acc WHERE username = '%s';" % username)
user_credential = cur.fetchall()
print user_credential
if not user_credential:
return login()
if user_credential[0][1] == password:
print "logging in %s" % username
response.set_cookie("username", user_credential[0][0])
return "Now successfully logged in as %s <br> <a href='/'> << Go Back </a>" % user_credential[0][0]
else:
print "Incorrect credentials"
return login()
@route('/contests')
def contests():
return html_contests
@route('/pcontests')
def programming_contests():
cur.execute("SELECT * FROM lab5.programmingContest as PC, lab5.contests as C WHERE PC.contestid = C.contestid AND C.state = 'open';")
opens = cur.fetchall()
cur.execute("SELECT * FROM lab5.programmingContest as PC, lab5.contests as C WHERE PC.contestid = C.contestid AND C.state = 'active';")
active = cur.fetchall()
html_open = ''
html_active = ''
for contest in opens:
html_open += '<li> ' + contest[2]
for contest in active:
html_active += '<li> ' + contest[2]
result = html_pcontests % {'open':html_open,'active':html_active}
return result
@route('/dcontests')
def developing_cntests():
cur.execute("SELECT * FROM lab5.developingContest as PC, lab5.contests as C WHERE PC.contestid = C.contestid AND C.state = 'open';")
opens = cur.fetchall()
cur.execute("SELECT * FROM lab5.developingContest as PC, lab5.contests as C WHERE PC.contestid = C.contestid AND C.state = 'active';")
active = cur.fetchall()
html_open = ''
html_active = ''
for contest in opens:
html_open += '<li> '+ contest[3]
for contest in active:
html_active += '<li> '+ contest[3]
result = html_pcontests % {'open':html_open,'active':html_active}
return result
@route('/hello')
def hello_again():
if request.get_cookie("visited"):
return "Welcome back! Nice to see you again"
else:
response.set_cookie("visited", "yes")
return "Hello there! Nice to meet you"
@route('/logout')
def logout():
if request.get_cookie("username"):
response.delete_cookie("username")
return 'Logged you out :) <br> Be back soon'
else:
return "You are not logged in, in the first place <br> <a href='/'> << Go Back </a>"
@route('/check_login')
def is_loggedin():
if request.get_cookie("username"):
return "You are logged in as %s. <br> <a href='/'> << Go Back </a>" % request.get_cookie("username")
else:
return "You are not logged in, in the first place <br> <a href='/'> << Go Back </a>"
@route('/delete/<user>')
def delete(user):
username = request.get_cookie("username")
if user:
if user== username:
print "HERE"
cur.execute("DELETE FROM lab5.members WHERE username = '%s';" % user)
cur.execute("SELECT * FROM lab5.submits_user WHERE username = '%s';" % user)
submissionids = cur.fetchall()
for s in submissionids:
cur.execute("DELETE FROM lab5.submissions WHERE submissionid = '%s';" % s)
cur.execute("DELETE FROM lab5.submits_user WHERE username = '%s';" % user)
cur.execute("DELETE FROM lab5.sponsors WHERE username = '%s';" % user)
cur.execute("DELETE FROM lab5.judges WHERE username = '%s';" % user)
cur.execute("SELECT * FROM lab5.discussions WHERE username = '%s';" % user)
discussions = cur.fetchall()
for d in discussions:
cur.execute("DELETE FROM lab5.comments WHERE discussionid = '%s';" % d)
cur.execute("DELETE FROM lab5.discussions WHERE username = '%s';" % user)
cur.execute("DELETE FROM lab5.user_acc WHERE username = '%s';" % user)
database.commit()
response.delete_cookie("username")
return "%s now deleted from the Database" % user
else:
return "You are not logged in, in the first place <br> <a href='/'> << Go Back </a>"
run(host='localhost', port=8081, debug=True)