webgo/login.py

183 lines
5.4 KiB
Python
Raw Normal View History

#import database,init_webgo,helper,gamecreation
import helper,database
from sets import Set as set
from cherrypy import cpg
from cherrypy.lib import httptools
"""
2005-09-06 19:52:46 +02:00
"""
"""
def navigation_bar(req,form):
"" "
gets request object and util.FieldStorage form.
writes the following to req:
- a button to return to the game overview
- a logout button
returns string
" ""
username = form["username"]
sessionid = form["sessionid"]
game = form["game"]
#TODO: buttons
data=" ""
<form method="post">
<input type="hidden" name="username" value="%s">
<input type="hidden" name="sessionid" value="%s">
<input type="hidden" name="game" value="%s">
<input type="submit" class="submit" name="logout" value="logout">
<input type="submit" class="submit" name="game overview" value="game overview">
<input type="submit" class="submit" name="refresh" value="refresh">
</form>
"" " % (username,sessionid,game)
return(data)
def logout(req,form):
"" "
gets request object and util.FieldStorage form.
reads username from form and clears timeout and sessionid from users table.
"" "
username = form["username"]
myuser = database.Users.byUsername(username)
myuser.sessionid = ""
myuser.timeout = 0
2005-09-06 19:52:46 +02:00
def main(req,form):
#debug
helper.debug(req,form)
#req.write(helper.footer())
2005-09-06 19:52:46 +02:00
if "sessionid" not in form.keys():
2005-10-10 00:11:29 +02:00
req.write(login_form(req,form))
elif ("game overview" in form.keys()) and ("username" in form.keys()) and ("sessionid" in form.keys()):
game_overview_form(req,form)
elif ("logout" in form.keys()) and ("username" in form.keys()):
logout(req,form)
process_form(req,form)
2005-09-06 19:52:46 +02:00
else:
process_form(req,form)
"""
class Login:
def index(self,username=None,password=None):
"""
Without arguments, this function writes a login form. Else the login data is evaluated
and the user is eventually logged in.
"""
if username == None and password == None:
return self.login_form()
else:
return self.process_form(username,password)
def login_form(self):
"""
print welcome message and html form.
"""
data = helper.header()
data += """
<form method="post">
<p>Name:<br>
<input name="username" type="text" class="text" size="20"></p>
<p>Password:<br>
<input name="password" type="text" class="text" size="20"></p>
<p><input type="submit" class="submit" value="login"></p>
</form>
<a href="/createaccount"> create account</a><br>
"""
data += helper.footer()
return data
def process_form(self,username,password):
"""
gets username and password, checks for validity, eventually the user is logged in.
TODO: check for session timeout
"""
data = helper.header()
sessionid = cpg.request.sessionMap["_sessionId"]
myuser = database.Users.byUsername(username)
origpassword = myuser.password
if password == origpassword:
#login accepted
myuser.sessionid = sessionid
myuser.set_timeout()
cpg.request.sessionMap["username"] = username
#now display list of games.
httptools.redirect("/overview")
#data += "Login accepted."
else:
data += "Login incorrect. Please try again.<br>"
data += self.login_form()
data += helper.footer()
return data
index.exposed=True
class Overview:
"""
give out a list of current games and stuff."""
def index(self):
username = cpg.request.sessionMap["username"]
myuser = database.Users.byUsername(username)
sessionid = cpg.request.sessionMap["_sessionId"]
if myuser.sessionid == sessionid:
return self.game_overview_form(username)
else:
return "You must be logged in to access this page."
def game_overview_form(self,username):
"""
gets:name of user,
prints a form with the option to select,create and delete games.
"""
myuser = database.Users.byUsername(username)
data = helper.header()+ """
<h2> Current Games: </h2>
"""
gamelist = myuser.gamelist()
#display list of current games
counter = 10 - len(helper.clean_list(gamelist))
tmp = ""
if helper.clean_list(gamelist) != []:
tmp += "<table><tr><td></td><td>Name of game</td><td>White</td>"
tmp += "<td>Black</td><td>Time of Creation</td><td>Time of last move</td>"
tmp += "<td></td></tr>"
for item in gamelist:
if (item != None) and (item != "None"):
tmp += '<tr>\n<form action="/playgame/" method="get">\n'
tmp += '<input type="hidden" name="game" value="%s">\n' % item
tmp += '<td><input type=submit class="submit" value="Play"></td>\n'
mygame = database.GobanTable.get(item)
description = mygame.description
if (description == None) or (description == "None") or (description == ""):
description = item
tmp += '<td>%s</td>\n' % description
players = mygame.players()
tmp += '<td>%s</td>\n' % players[0]
tmp += '<td>%s</td>\n' % players[1]
tmp += '<td>%s</td>\n' % helper.format_time(mygame.created)
tmp += '<td>%s</td>\n' % helper.format_time(mygame.lastmove)
tmp += '<td><input type=submit class="submit" name="delete" value="Delete"></td>\n'
tmp += '</tr></form>\n'
if helper.clean_list(gamelist) != []:
tmp += '</table>'
if gamelist == []: #no current games
data += "You don't have any running games.\n"
else:
data += tmp
#now comes the option for creating new games.
data += "<h2>New Game:</h2>\n"
if counter > 0:
data+= "You have %s free game slots.<br>" % counter
data += '<a href="/gamecreation"> Start a new game</a><br>'
else:
data+= "Sorry, all your game slots are in use."
data+=helper.footer()
return data
index.exposed = True