web.py User Authentication with PostgreSQL database example
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()
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 /login
'ThreadedDict' object has no attribute 'login'
 at /login
'ThreadedDict' object has no attribute 'privilege'

Here is the error output to the terminal for the second error. (the first is almost identical)

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 239, in process
    return self.handle()
  File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 230, in handle
    return self._delegate(fn, self.fvars, args)
  File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 420, in _delegate
    return handle_class(cls)
  File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 396, in handle_class
    return tocall(*args)
  File "/home/erik/Dropbox/Python/Web.py/Code.py", line 44, in GET
    render = create_render(session.privilege)
  File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/session.py", line 71, in __getattr__
    return getattr(self._data, name)
AttributeError: 'ThreadedDict' object has no attribute 'privilege'

127.0.0.1:36420 - - [25/Aug/2012 01:12:38] "HTTP/1.1 GET /login" - 500 Internal Server Error


Here is my code.py file. Pretty much cut-n-paste from the cookbook. I tried putting all of the class and def on top of the main code. I have also tried launching python with sudo as mentioned in another post.

import web

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():
    return False  #I added this to test error #1, Now I get error #2
    #if session.login==1:
    #    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:
        render = web.template.render('templates/communs')
    return render



class Login:

    def GET(self):
        if logged():
            render = create_render(session.privilege)
            return '%s' % render.login_double()
        else:
            # This is where error #2 is
            render = create_render(session.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.privilege)
                return render.login_ok()
            else:
                session.login = 0
                session.privilege = 0
                render = create_render(session.privilege)
                return render.login_error()
        except:
            session.login = 0
            session.privilege = 0
            render = create_render(session.privilege)
            return render.login_error()


class Reset:

    def GET(self):
        session.login = 0
        session.kill()
        render = create_render(session.privilege)
        return render.logout()




#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(dbn='postgres', user='hdsfgsdfgsd', pw='dfgsdfgsdfg', db='postgres', host='fdfgdfgd.com')

store = web.session.DiskStore('sessions')

# Too me, it seems this is being ignored, at least the 'initializer' part
session = web.session.Session(app, store, initializer={'login': 0, 'privilege': 0})



if __name__ == "__main__": app.run()