webgo/login.py

136 lines
4.3 KiB
Python
Raw Normal View History

import psql,init_webgo,helper,gamecreation
2005-09-06 19:52:46 +02:00
from mod_python import *
def process_form(req,form):
"""
reads username and password from form
2005-09-06 19:52:46 +02:00
"""
#req.write("<br>"+"name="+form['name']+", password="+form['password']+"<hr>")
if form.keys() != []:
if ("name" in form.keys()) and ("password" in form.keys()):
#extract name and password
name = form["name"]
password = form["password"]
sessionid = form["sessionid"]
origpassword = psql.get_user_info(name,"password")
#debug:
helper.debug(req,form,'<hr>--password:'+str(password)+' ---origpassword:'+str(origpassword)+'<hr>')
#check if user exists (else we would get an error string)
2005-09-06 19:52:46 +02:00
if origpassword != "no such user": #no error message, now check password
if password == origpassword:
#login accepted
psql.set_user_sessionid(name,sessionid)
psql.set_user_timeout(name)
#now display list of games.
game_overview_form(req,form,name,sessionid)
2005-09-06 19:52:46 +02:00
else:
req.write("Login incorrect. Please try again.<br>")
req.write(login_form())
else: #no such user
req.write("Login incorrect. Please try again.-<br>")
2005-09-06 19:52:46 +02:00
req.write(login_form())
else: #one of (name,password) is missing:
req.write("Please enter your name and password.")
req.write(login_form())
def game_overview_form(req,form,user,sessionid):
2005-09-06 19:52:46 +02:00
"""
gets: request object, util.FieldStorage form, name of user, sessionid.
2005-09-06 19:52:46 +02:00
prints a form with the option to select,create and delete games.
"""
data = helper.header()+ """
<h2> Current Games: </h2>
<form method="post">
"""
gamelist = psql.get_user_game_list(user)
#debug
helper.debug(req,form,str(gamelist)+"<hr>\n")
2005-09-06 19:52:46 +02:00
#display list of current games
counter = 10
tmp = ""
for item in gamelist:
if (item != None) and (item != "None"):
tmp += '<input type="radio" name="game" value="%s"> %s<br>\n' % (item,item)
#later write partners name and whether its our turn or not
#data += "Your partner: %s."
counter -= 1
2005-09-06 19:52:46 +02:00
if tmp == "": #no current games
data += "You don't have any running games.\n"
else:
tmp += '<input type="hidden" name="sessionid" value="%s">\n' % sessionid
tmp += '<input type="hidden" name="username" value="%s">\n' % user
2005-09-06 19:52:46 +02:00
data += tmp
data += '<input type=submit id="submit" name="play" value="Play selected game">\n<input type=submit id="submit" name="delete" value="Delete selected game">\n</form>'
2005-09-06 19:52:46 +02:00
#now comes the option for creating new games.
data += "<h2>New Game:</h2>\n"
2005-09-06 19:52:46 +02:00
if counter > 0:
data+= "You have %s free game slots.<br>" % counter
data += """
<form method="post">
2005-09-06 19:52:46 +02:00
<input type="hidden" name="sessionid" value="%s">
<input type="hidden" name="username" value="%s">
<input type=submit id="submit" name="create" value="Start a new game">
2005-09-06 19:52:46 +02:00
</form>
""" % (sessionid, user)
else:
data+= "Sorry, all your game slots are in use."
#display "You have ... free game slots" + Button "create game"
#bei "create game neue funktion zum Erstellen + Mitspieler aus Liste ausw<73>hlen.
data+=helper.footer()
req.write(data)
2005-09-06 19:52:46 +02:00
def login_form():
"""
print welcome message and html form.
"""
data = helper.header() + """
2005-09-06 19:52:46 +02:00
<form method="post">
<p>Name:<br>
<input name="name" size="40"></p>
<p>Pasword:<br>
<input name="password" size="40"></p>
<input type="hidden" name="sessionid" value="%s">
<p><input type="submit" id ="submit" value="login"></p>
2005-09-06 19:52:46 +02:00
</form>
""" % helper.generate_session_id()
data += helper.footer()
return data
def navigation_bar(username,sessionid):
"""
gets username and sessionid
writes the following to req:
- a button to return to the game overview
- a logout button
returns string
"""
#TODO: buttons
data="""
<form method="post">
<input type="hidden" name="sessionid" value="%s">
<input type="hidden" name="username" value="%s">
<input type="submit" id="submit" name="logout" value="logout">
<input type="submit" id="submit" name="game overview" value="game overview">
</form>
""" % (username,sessionid)
return(data)
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():
req.write(login_form())
elif ("create" in form.keys()) and ("username" in form.keys()):
pass
elif ("game overview" in form.keys()) and ("username" in form.keys()) and ("sessionid" in form.keys()):
game_overview_form(req,form,form["username"],form["sessionid"])
2005-09-06 19:52:46 +02:00
else:
process_form(req,form)