Thank you too. It's good to hear about different solutions, althought I don't know why I didn't think of that before. But I think I'm going with decorators, that seems more elegant to me, and requires about the same amount of code.
On May 12, 7:52 pm, Aydın ŞEN <[email protected]> wrote: > 2010/5/12 Oskar <[email protected]> > > > Is there anyway I can put some code somewhere that gets executed no > > matter what page the user visits? Ideally I would like to send them to > > this one login page if they are not logged in. > > You can use inheritance for both of them (login required and general > template). > > class Base(object): > > def __init__(self): > self.content = "" > self.menu = "this is the menu" > self.header = "this is the header" > > def render(self): > web.template.Template.globals['menu'] = self.menu > web.template.Template.globals['header'] = self.header > return render.Base(self.content) > > def check_login(self): > if not session.loggedin: > raise web.seeother("/login") > > def GET(self): > self.check_login() > > def POST(self): > self.check_login() > > This is Base class that we inherit other classes from this class. We have > Base.html template which contains general fields (header, menu etc) and > content (self.htmlText) which will change for each page. The assumption is > you set to True session.loggedin variable when login successful. > > This is Hello class which is required login > > class Hello(Base): > > def GET(self): > super(Hello self).GET() > self.content = "this is the Hello page" > return self.render() > > def POST(self): > super(Hello, self).POST() > #do whatever you want > self.render() > > When Hello class requested Base class' GET and POST method will run first > and if user is not logged in it will redirect to "/login". > > And this is the Base.html > > $def with (content='') > <!--metadata bla bla--> > <div class='header'> $:header </div> > <div class='menu'> $:menu </div> > <div class = 'content'> $:content </div> > > -- > Aydın Şen > > -- > 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 > athttp://groups.google.com/group/webpy?hl=en. -- 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.
