<s> import cgi , os , sys , time <EOL> import types <EOL> from snakeserver . snakelet import Snakelet <EOL> from snakeserver . user import LoginUser <EOL> class Account ( Snakelet ) : <EOL> def init ( self ) : <EOL> pass <EOL> def getDescription ( self ) : <EOL> return "<STR_LIT>" <EOL> def serve ( self , request , response ) : <EOL> form = request . getForm ( ) <EOL> action = form . get ( "<STR_LIT:action>" ) <EOL> if action == "<STR_LIT>" : <EOL> self . prefillFormValues ( form , request . getContext ( ) ) <EOL> name = form . get ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> login = form . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> passw1 = form . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> passw2 = form . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if len ( name ) < <NUM_LIT:3> : <EOL> request . getContext ( ) . fieldErrors [ "<STR_LIT:name>" ] = '<STR_LIT>' <EOL> name = '<STR_LIT>' <EOL> if len ( login ) < <NUM_LIT:4> : <EOL> request . getContext ( ) . fieldErrors [ "<STR_LIT>" ] = '<STR_LIT>' <EOL> if len ( passw1 ) < <NUM_LIT:4> : <EOL> request . getContext ( ) . fieldErrors [ "<STR_LIT>" ] = '<STR_LIT>' <EOL> elif passw1 != passw2 : <EOL> request . getContext ( ) . fieldErrors [ "<STR_LIT>" ] = '<STR_LIT>' <EOL> if request . getContext ( ) . fieldErrors : <EOL> self . redirect ( '<STR_LIT>' , request , response ) <EOL> else : <EOL> rctx = request . getContext ( ) <EOL> users = self . getWebApp ( ) . getConfigItem ( "<STR_LIT>" ) <EOL> if login in users : <EOL> request . getContext ( ) . fieldErrors [ "<STR_LIT>" ] = '<STR_LIT>' <EOL> self . redirect ( '<STR_LIT>' , request , response ) <EOL> else : <EOL> users [ login ] = LoginUser ( login , passw1 , name ) <EOL> rctx . name = name <EOL> rctx . login = login <EOL> self . redirect ( '<STR_LIT>' , request , response ) <EOL> elif action == "<STR_LIT>" : <EOL> self . prefillFormValues ( form , request . getContext ( ) ) <EOL> name = form . get ( '<STR_LIT:name>' , "<STR_LIT>" ) <EOL> passwd = form . get ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> newpass1 = form . get ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> newpass2 = form . get ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> if len ( name ) < <NUM_LIT:3> : <EOL> request . getContext ( ) . fieldErrors [ "<STR_LIT:name>" ] = "<STR_LIT>" <EOL> name = '<STR_LIT>' <EOL> if newpass1 and len ( newpass1 ) < <NUM_LIT:4> : <EOL> request . getContext ( ) . fieldErrors [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> elif newpass1 != newpass2 : <EOL> request . getContext ( ) . fieldErrors [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> if newpass1 and not request . getSession ( ) . getLoggedInUser ( ) . checkPassword ( passwd ) : <EOL> request . getContext ( ) . fieldErrors [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> if request . getContext ( ) . fieldErrors : <EOL> self . redirect ( '<STR_LIT>' , request , response ) <EOL> else : <EOL> request . getSession ( ) . getLoggedInUser ( ) . name = name <EOL> if newpass1 : <EOL> request . getSession ( ) . getLoggedInUser ( ) . password = newpass1 <EOL> self . clearFormValues ( request . getContext ( ) ) <EOL> self . redirect ( '<STR_LIT>' , request , response ) <EOL> else : <EOL> response . sendError ( <NUM_LIT> , "<STR_LIT>" ) <EOL> def prefillFormValues ( self , form , requestCtx ) : <EOL> fieldValues = { } <EOL> for key in form . keys ( ) : <EOL> value = form [ key ] <EOL> if type ( value ) in types . StringTypes : <EOL> fieldValues [ key ] = value <EOL> requestCtx . fieldValues = fieldValues <EOL> requestCtx . fieldErrors = { } <EOL> def clearFormValues ( self , requestCtx ) : <EOL> requestCtx . fieldValues = { } <EOL> requestCtx . fieldErrors = { } </s>
