Web.py User Authentication With Postgresql Database Example
I am trying to copy and use the example 'User Authentication with PostgreSQL database' from the web.py cookbook. I can not figure out why I am getting the following errors. at /l
Solution 1:
Okay, I was able to figure out what I did wrong. Total newbie stuff and all part of the learning process. This code now works, well mostly. The part that I was stuck on is now working. See my comments in the code
Thanks
import web web.config.debug = False render = web.template.render('templates/', base='layout') urls = ( '/', 'index', '/add', 'add', '/login', 'Login', '/reset', 'Reset' ) app = web.application(urls, globals()) db = web.database(blah, blah, blah) store = web.session.DiskStore('sessions') session = web.session.Session(app, store, initializer={'login': 0, 'privilege': 0}) class index: def GET(self): todos = db.select('todo') return render.index(todos) class add: def POST(self): i = web.input() n = db.insert('todo', title=i.title) raise web.seeother('/') def logged(): if session.get('login', False): return True else: return False def create_render(privilege): if logged(): if privilege == 0: render = web.template.render('templates/reader') elif privilege == 1: render = web.template.render('templates/user') elif privilege == 2: render = web.template.render('templates/admin') else: render = web.template.render('templates/communs') else: ## This line is key, i do not have a communs folder, thus returning an unusable object #render = web.template.render('templates/communs') #Original code from example render = web.template.render('templates/', base='layout') return render class Login: def GET(self): if logged(): ## Using session.get('something') instead of session.something does not blow up when it does not exit render = create_render(session.get('privilege')) return '%s' % render.login_double() else: render = create_render(session.get('privilege')) return '%s' % render.login() def POST(self): name, passwd = web.input().name, web.input().passwd ident = db.select('users', where='name=$name', vars=locals())[0] try: if hashlib.sha1("sAlT754-"+passwd).hexdigest() == ident['pass']: session.login = 1 session.privilege = ident['privilege'] render = create_render(session.get('privilege')) return render.login_ok() else: session.login = 0 session.privilege = 0 render = create_render(session.get('privilege')) return render.login_error() except: session.login = 0 session.privilege = 0 render = create_render(session.get('privilege')) return render.login_error() class Reset: def GET(self): session.login = 0 session.kill() render = create_render(session.get('privilege')) return render.logout() if __name__ == "__main__": app.run()
Post a Comment for "Web.py User Authentication With Postgresql Database Example"