Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 158 lines (139 sloc) 5.487 kb
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
import os
import datetime
import string
import userdb
import questiondb
import simplejson as json
from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class index(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/html'
path = os.path.join(os.path.dirname(__file__), 'templates/index.html')
self.response.out.write(template.render(path,""))

class contestStart(webapp.RequestHandler):
def get(self,var):
user = users.get_current_user()
if not user:
self.redirect(users.create_login_url(self.request.uri))
else:
if not userdb.userPlayExist():
userdb.userPlayStart(var)
self.redirect('/?auth=1')
else:
self.response.headers['Content-Type'] = 'text/html'
self.response.out.write("You have already started the contest. You were not supposed to do this. You can continue the contest <a href='/'>here</a>. You will not have a timer and you may be penalized for this. Contact admins for further details." )


class contestStop(webapp.RequestHandler):
def get(self):
self.response.out.write(userdb.userPlayStop())
self.redirect('/?end=1')


class contestQuestion(webapp.RequestHandler):
def get(self,var):
self.response.headers['Content-Type'] = 'application/json'
r = questiondb.getQuestion(int(userdb.userPermutation(int(var))),int(var))
self.response.out.write(r)

class contestAnswer(webapp.RequestHandler):
def get(self):
self.response.out.write(userdb.userAnswerSubmit(
int(userdb.userPermutation(int(self.request.get('question')))),
self.request.get('answer')))

class contestActiveuser(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'application/json'
user = users.get_current_user()
if user:
self.response.out.write("{ \"name\" : \""+user.nickname()+"\" }")
else:
self.response.out.write("{ \"name\" : \"NULL\" }")


class adminQuestionsAdd(webapp.RequestHandler):
def get(self):
if users.get_current_user():
if users.is_current_user_admin():
self.response.headers['Content-Type'] = 'text/html'
path = os.path.join(os.path.dirname(__file__), 'templates/question.html')
self.response.out.write(template.render(path,""))
else:
print "Not allowed!"
else:
self.redirect(users.create_login_url(self.request.uri))


class adminQuestionsSubmit(webapp.RequestHandler):
def post(self):
if users.get_current_user():
if users.is_current_user_admin():
q = questiondb.question(questionNumber=string.atoi(self.request.get('qno')),
question=self.request.get('ques'),
qimage=self.request.get('qimg'),
opt1=self.request.get('opt1'),
opt2=self.request.get('opt2'),
opt3=self.request.get('opt3'),
opt4=self.request.get('opt4'),
ans=self.request.get('ans'))
q.put()
self.redirect('/admin/questions/list/')
else:
print "Not allowed!"
else:
self.redirect(users.create_login_url(self.request.uri))

class adminQuestionsList(webapp.RequestHandler):
def get(self):
if users.get_current_user():
if users.is_current_user_admin():
self.response.headers['Content-Type'] = 'text/html'
query = questiondb.question.all()
result = query.fetch(100)
template_values = { 'questions' : result }
path = os.path.join(os.path.dirname(__file__), 'templates/questionlist.html')
self.response.out.write(template.render(path, template_values))
else:
print "Not allowed!"
else:
self.redirect(users.create_login_url(self.request.uri))

class adminQuestionsAnswered(webapp.RequestHandler):
def get(self):
if users.get_current_user():
if users.is_current_user_admin():
self.response.headers['Content-Type'] = 'text/html'
self.response.out.write("Answered Questions<br />")
query = userdb.userAnswer.all().order('elapsedTime')
result = query.fetch(100)
for ans in result:
self.response.out.write(ans.user.nickname()+str(ans.question)+
ans.answer+str(ans.elapsedTime)+"<br />")
else:
print "Not allowed!"
else:
self.redirect(users.create_login_url(self.request.uri))

class adminContestUsers(webapp.RequestHandler):
def get(self):
if users.get_current_user():
if users.is_current_user_admin():
self.response.headers['Content-Type'] = 'text/html'
query = userdb.userPlay.all().order('startTime')
result = query.fetch(100)
for ans in result:
self.response.out.write(ans.user.nickname()+ans.tathvaID+"<br />")
self.response.out.write(ans.questionSet)
self.response.out.write("<br />")
else:
print "Not allowed!"
else:
self.redirect(users.create_login_url(self.request.uri))

application = webapp.WSGIApplication(
[('/', index),
('/contest/start/(.*)|/',contestStart),
('/contest/stop/',contestStop),
('/contest/question/(\d*)|/', contestQuestion),
('/contest/answer/', contestAnswer),
('/contest/activeuser/', contestActiveuser),
('/admin/questions/add/', adminQuestionsAdd),
('/admin/questions/submit/', adminQuestionsSubmit),
('/admin/questions/list/',adminQuestionsList),
('/admin/questions/answered/',adminQuestionsAnswered),
('/admin/contest/users/',adminContestUsers)],
debug=True)

def main():
run_wsgi_app(application)

if __name__ == "__main__":
main()
Something went wrong with that request. Please try again.