On Sep 19, 4:20 am, michael kapelko <[email protected]> wrote:
> Hi.
>
> I have lots of classes that look like this:
>
> class PAGE_NAME:
> def GET(self, param):
> if (session.get("login", None)):
> i = web.input()
> c = db()
> return render.PAGE_NAME(session.login, c, i)
> else:
> raise web.seeother("/")
>
> If user is not logged it, it redirects to login page. Otherwise it
> displays necessary info.
> I have about 10 classes already and more to come. The code is all the
> same except for PAGE_NAME. Is there a way not to duplicate things?
Here's a simple way that worked for me;
urls = (
'/', 'index',
'/foo', 'foo',
'/login', 'login',
'/logout', 'logout',
)
# define some classes
def session_hook():
web.ctx.session = session
web.template.Template.globals['session'] = session
def authenticated():
if (not (web.ctx.path == '/login')):
if (len(session.username) == 0):
raise web.seeother('/login')
application = web.application(urls, globals())
session = web.session.Session(application,
web.session.DiskStore('/tmp/sessions'),
initializer={'username': ''})
application.add_processor(web.loadhook(session_hook))
application.add_processor(web.loadhook(authenticated))
if (prod):
application = application.wsgifunc()
else:
if __name__ == "__main__":
application.run()
--
You received this message because you are subscribed to the Google Groups
"web.py" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/webpy?hl=en.