webgo/login.py

155 lines
4.4 KiB
Python
Raw Normal View History

#import database,init_webgo,helper,gamecreation
import helper,database,deletegame
from sets import Set as set
from cherrypy import cpg
from cherrypy.lib import httptools
2005-09-06 19:52:46 +02:00
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
"""
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,message=""):
"""
print welcome message and html form.
"""
return helper.cs_render("templates/login_form.cs",{"Data.Message":message})
def process_form(self,username,password):
"""
gets username and password, checks for validity, eventually the user is logged in.
TODO: check for session timeout
"""
sessionid = cpg.request.sessionMap["_sessionId"]
try:
myuser = database.Users.byUsername(username)
origpassword = myuser.password
except:
origpassword = ""
if origpassword != "" and password == origpassword:
#login accepted
myuser.sessionid = sessionid
myuser.set_timeout()
cpg.request.sessionMap["username"] = username
#now display list of games.
httptools.redirect("/overview")
else:
message = "Login incorrect. Please try again.<br>"
data = self.login_form(message)
return data
index.exposed=True
class Overview:
"""
give out a list of current games and stuff."""
def index(self,game=None,delete=None):
#load username and sesisonid from session data
username = cpg.request.sessionMap["username"]
sessionid = cpg.request.sessionMap["_sessionId"]
if username != None:
try:
myuser = database.Users.byUsername(username)
user = 1
except:
user = 0
if user:
if myuser.sessionid == sessionid:
if game!=None and delete != None: #delete given game
deletegame.main(game)
return self.game_overview_form(username)
else:
return helper.cs_render("templates/not_logged_in.cs")
else:
return helper.cs_render("templates/not_logged_in.cs")
else:
return helper.cs_render("templates/not_logged_in.cs")
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)
gamelist = myuser.gamelist()
settings = {}
cleanlist = helper.clean_list(gamelist)
#display list of current games
counter = 10 - len(cleanlist)
settings["Data.Counter"] = counter
settings["Data.Gamelist"] = cleanlist
tmp = ""
if cleanlist != []:
settings["Data.GamelistNotEmpty"] = 1
else:
settings["Data.GamelistNotEmpty"] = 0
for item in gamelist:
if (item != None) and (item != "None"):
mygame = database.GobanTable.get(item)
description = mygame.description
if (description == None) or (description == "None") or (description == ""):
description = item
settings["Data.Games.%s.Description" % item] = mygame.description
settings["Data.Games.%s.Name" % item] = item
players = mygame.players()
settings["Data.Games.%s.Player1" % item] = mygame.players()[0]
settings["Data.Games.%s.Player2" % item] = mygame.players()[1]
settings["Data.Games.%s.Created" % item] = helper.format_time(mygame.created)
settings["Data.Games.%s.Lastmove" % item] = helper.format_time(mygame.lastmove)
return helper.cs_render("templates/overview.cs",settings)
index.exposed = True