webgo/createaccount.py

78 lines
No EOL
2.3 KiB
Python

import helper,psql
maxuser = 1000
def display_create_form(req,form):
"""
gets a request object and a util.FieldStorage form.
writes a HTML page containing one name and two password fields.
"""
data = """
<form method="post">
<p>Your username:<br>
<input name="username" type="text" id="text" size="20"></p>
<p>Your Password:<br>
<input name="password1" type="text" id="text" size="20"></p>
<p>Please retype your Password:<br>
<input name="password2" type="text" id="text" size="20"></p>
<input type="hidden" name="sessionid" value="%s">
<input type="hidden" name="createaccount" value="process">
<p><input type="submit" id ="submit" value="create account"></p>
</form>
""" % helper.generate_session_id()
req.write(data)
def process_form(req,form):
"""
gets a request object and a util.FieldStorage form.
Tries to read out username, password1 and password2 from form.
If all needed data is there, create the named user and return a 'success'
page. Else fail with detailed error.
"""
try:
password1 = form["password1"]
except:
password1 = ""
try:
password2 = form["password2"]
except:
password2 = ""
try:
username = form["username"]
except:
username = ""
if (username != "") and (password1 != "") and (password2 != "") and (password1 == password2):
psql.add_webgo_user(username,password1)
req.write("User %s has been successfully created. Click the following button to login:<br>" % username)
data = """
<form method="post">
<input name="username" type="hidden" value="%s"></p>
<input name="password" type="hidden" value="%s"></p>
<input type="hidden" name="sessionid" value="%s">
<p><input type="submit" id ="submit" value="login"></p>
</form>
""" % (username,password1,helper.generate_session_id())
req.write(data)
else:
if username == "":
req.write("Please enter a username you would like to have.<br>")
if (password1 == "") or (password2 == "") or (password1 != password2):
req.write("Both given passwords have to be the same and non-empty.<br>")
display_create_form(req,form)
def main(req,form):
req.write(helper.header())
helper.debug(req,form,str(form.keys()))
try:
createvalue = form["createaccount"]
except:
createvalue = ""
if createvalue == "process":
process_form(req,form)
else:
display_create_form(req,form)
req.write(helper.footer())