#file: testpos/login.py
from pso.parser import Tag
class LoginLogout(Tag):
""" if user is login in shows welcome otherwise shows link to login screen"""
WELCOME="Welcome"
def __call__(self, handler, cdata=''):
action = handler.req().getInput('action','')
if action in ('login', 'join'):
# don't show anything in these screens
return ''
user = self.getLogin(handler)
# login in ?
if user:
if handler.req().hasInputs('logout'):
# does user want to logout
self.logOut(handler)
return """ %s""" % "Logout"
return """ %s""" % cdata
def logIn(self, handler, id):
" fill in later"
handler.req().getSession()['user'] = id
def logOut(self, handler):
del handler.req().getSession()['user']
handler.req().redirect('test.cgi')
def getLogin(self, handler):
return handler.req().getSession().get('user')
class LoginForm(Tag):
""" if no errors sets session with this user and redirects to home page
if errors sets handler's scratch with error message
when form is needed prints hidden action field"""
NOMATCH='wrong id or password'
NOENTRY='please enter id and password'
def __call__(self, handler, cdata=''):
html =""
if handler.req().hasInputs('login'): # check to see in login button pressed
id = handler.req().getInput('id')
passwd = handler.req().getInput('passwd')
if id and passwd:
user = self.checkUser(handler, id, passwd)
if user:
LoginLogout().logIn(handler, id )
handler.req().redirect('test.cgi')
else:
handler.scratch()['message'] = self.getAttrs().get('nomatch', self.NOMATCH)
else:
handler.scratch()['message'] = self.getAttrs().get('noentry', self.NOENTRY)
return """"""
def checkUser(self, handler, id, passwd):
" fill in later "
return id == passwd