From pinted at tiscali.be Wed Jun 2 00:47:37 2004 From: pinted at tiscali.be (Didrik Pinte) Date: Tue, 01 Jun 2004 16:47:37 +0200 Subject: [albatross-users] newbie (coming from java) question Message-ID: <1086101257.4898.7.camel@geru-itea> Hi, I'm running a little website with albatross and have some question/problems with the basic albatross concepts. I'm used to Java Servlets vocabulary and need some help. Here is what I need to do I have an Python object managing searches in my dbxml. I need that object in the "application context", so that it will be used from anywhere in the app. Then, when the client request a documentname, i search for the doc, and depending on the result, i make appear the right page with the results. With what I understand, I've created a class ListPage that use the page_process() method to define what to do. So it searches for the documents, if no document found, redirect the user to an ErrorPage, otherwise, I want to store the doclist in the "session context" so that it will be used in the page_display() method. My code does not work (what a shame !). Can someone have a look on this ? Thank you in advance. Didrik Pinte Here is the code : class ListPage: '''This page shows the list of results due to the search ''' name = 'list' def page_process(self,ctx): if ctx.req_equals('detail'): ctx.set_page('detail') if ctx.req_equals('recherche'): container = ctx.getContainer() docInfoList = [] for result in container.searchContains('title',ctx.locals.title): docInfoList.append(result) if len(docInfoList)<1 : ctx.locals.error = "No document found" ctx.locals.doclist = [] ctx.set_page('error') else : ctx.locals.doclist = docInfoList ctx.add_session_vars('doclist') def page_display(self,ctx): ctx.load_template('headers.html').to_html(ctx) ctx.load_template('footer.html').to_html(ctx) ctx.run_template('list.html') class ErrorPage: ''' Managing errors ''' name = 'error' def page_display(self,ctx): ctx.run_template('error.html') class ApplicationContext(SimpleAppContext): '''Initialises the context of the application and defines some methods to be used in all the page ''' metaData = None def _initialiseContainer(self): self.metaData = MetaData('mila.dbxml') def getContainer(self): if self.metaData == None: self._initialiseContainer() return self.metaData class App(SimpleSessionApp): def __init__(self): SimpleSessionApp.__init__(self, base_url='application.py', template_path='.', start_page='search', secret='-=-secret-=-', session_appid='metadata') for page in (SearchPage,ListPage,DetailPage,ErrorPage): self.register_page(page.name, page()) def create_context(self): return ApplicationContext(self) if __name__ == '__main__': app.run(Request()) -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 189 bytes Desc: Ceci est une partie de message num?riquement sign?e. URL: From gnb at itga.com.au Wed Jun 2 10:32:06 2004 From: gnb at itga.com.au (Gregory Bond) Date: Wed, 02 Jun 2004 10:32:06 +1000 Subject: [albatross-users] newbie (coming from java) question In-Reply-To: Your message of Tue, 01 Jun 2004 16:47:37 +0200. Message-ID: <200406020032.KAA23210@lightning.itga.com.au> A couple of things spring to mind. pinted at tiscali.be said: > if ctx.req_equals('detail'): > ctx.set_page('detail') > if ctx.req_equals('recherche'): this should usually be elif - the set_page() call doesn't terminate the page_process function (tho depending on the html template, you may never hit the case where detail and recherche are both true...) > docInfoList = [] > for result in container.searchContains('title',ctx.locals.title): > docInfoList.append(result) Any particular reason this is not written as docInfoList = container.searchContains(....) (this is a general python nit not an albatross nit!) Are you expecting the ctx.metaData to be in the session? The code as written will have a single metadata object that is recreated each time an HTTP request is made (assuming this is a CGI and not, say, a mod_python script....) so any changes made to the metaData object by the searchContents() function will be lost. pinted at tiscali.be said: > My code does not work (what a shame !) Apart from the above, it looks reasonable enough to me. To go further, we'd need a fair bit more info. What "does not work'? Does it throw exceptions? What's broken? From pinted at tiscali.be Wed Jun 2 17:19:22 2004 From: pinted at tiscali.be (Didrik Pinte) Date: Wed, 02 Jun 2004 09:19:22 +0200 Subject: [albatross-users] newbie (coming from java) question In-Reply-To: <200406020032.KAA23210@lightning.itga.com.au> References: <200406020032.KAA23210@lightning.itga.com.au> Message-ID: <1086160762.4898.22.camel@geru-itea> Le mer 02/06/2004 ? 02:32, Gregory Bond a ?crit : > A couple of things spring to mind. > > pinted at tiscali.be said: > > if ctx.req_equals('detail'): > > ctx.set_page('detail') > > if ctx.req_equals('recherche'): > > this should usually be elif - the set_page() call doesn't terminate the > page_process function (tho depending on the html template, you may never hit > the case where detail and recherche are both true...) Great ! First intersting information. > > > docInfoList = [] > > for result in container.searchContains('title',ctx.locals.title): > > docInfoList.append(result) > > Any particular reason this is not written as > docInfoList = container.searchContains(....) > (this is a general python nit not an albatross nit!) > searchContains is a generator, so it can write this like you suggest but the type of docInfoList is not a list but a generator ! > Are you expecting the ctx.metaData to be in the session? The code as written > will have a single metadata object that is recreated each time an HTTP request > is made (assuming this is a CGI and not, say, a mod_python script....) so any > changes made to the metaData object by the searchContents() function will be > lost. I'm expecting my metaData to be an application variable, so not a session one. It has to be a singleton for the all application serving all the request. Where shall I declare it ? > pinted at tiscali.be said: > > My code does not work (what a shame !) > > Apart from the above, it looks reasonable enough to me. To go further, we'd > need a fair bit more info. What "does not work'? Does it throw exceptions? > What's broken? Here is the problem. In my ListPage class, i'm processing the request in the page_process() and create a docInfoList result. When page_display() is called, an error is raised saying that doclist cannot be found. If I process the request in the page_display() method, it works perfectly ! (see the second ListPage class below) Resulting question : how can I keep the docInfoList created in the page_process() so that it will be used in the page_diplay() ? I know I have to use the user session but how ? Thank you for the help. Didrik Pinte -------------------------------------------------------------- class ListPage: '''This page shows the list of results due to the search ''' name = 'list' def page_process(self,ctx): if ctx.req_equals('detail'): ctx.set_page('detail') if ctx.req_equals('recherche'): container = ctx.getContainer() docInfoList = [] for result in container.searchContains('title',ctx.locals.title): self.docInfoList.append(result) if len(self.docInfoList)<1 : ctx.locals.error = "No document found" ctx.set_page('error') else : ctx.locals.doclist = self.docInfoList ctx.add_session_vars('doclist') def page_display(self,ctx): ctx.load_template('headers.html').to_html(ctx) ctx.load_template('footer.html').to_html(ctx) ctx.run_template('list.html') ------------------------------------------------------------- Modified ListPage processing the request in the page_display() method class ListPage: '''This page shows the list of results due to the search ''' name = 'list' def page_process(self,ctx): if ctx.req_equals('detail'): ctx.set_page('detail') def page_display(self,ctx): ctx.load_template('headers.html').to_html(ctx) ctx.load_template('footer.html').to_html(ctx) if ctx.req_equals('recherche'): container = ctx.getContainer() docInfoList = [] for result in container.searchContains('title',ctx.locals.title): self.docInfoList.append(result) if len(self.docInfoList)<1 : ctx.locals.error = "No document found" ctx.locals.doclist = [] ctx.set_page('error') else : ctx.locals.doclist = self.docInfoList ctx.add_session_vars('doclist') ctx.run_template('list.html') -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 189 bytes Desc: Ceci est une partie de message num?riquement sign?e. URL: From gnb at itga.com.au Wed Jun 2 17:56:59 2004 From: gnb at itga.com.au (Gregory Bond) Date: Wed, 02 Jun 2004 17:56:59 +1000 Subject: [albatross-users] newbie (coming from java) question In-Reply-To: Your message of Wed, 02 Jun 2004 09:19:22 +0200. Message-ID: <200406020756.RAA11526@lightning.itga.com.au> Oh OK I think I see the problem. The _first time_ you get to the ListPage object, the call seq will be prevpage.page_exit() ListPage.page_enter() ListPage.page_display() ctx.locals.doclist is not set because it is only set once someone hits the "recherche" button and ListPager.page_process() is called. the fix: make ListPage.page_enter() like this: def page_enter(self, ctx): self.locals.doclist = [] ctx.add_session_variables('doclist') From pinted at tiscali.be Wed Jun 2 19:40:06 2004 From: pinted at tiscali.be (Didrik Pinte) Date: Wed, 02 Jun 2004 11:40:06 +0200 Subject: [albatross-users] newbie (coming from java) question In-Reply-To: <200406020756.RAA11526@lightning.itga.com.au> References: <200406020756.RAA11526@lightning.itga.com.au> Message-ID: <1086169206.4898.39.camel@geru-itea> Le mer 02/06/2004 ? 09:56, Gregory Bond a ?crit : > Oh OK I think I see the problem. > > The _first time_ you get to the ListPage object, the call seq will be > prevpage.page_exit() > ListPage.page_enter() > ListPage.page_display() > > ctx.locals.doclist is not set because it is only set once someone hits the > "recherche" button and ListPager.page_process() is called. > > the fix: make ListPage.page_enter() like this: > def page_enter(self, ctx): > self.locals.doclist = [] > ctx.add_session_variables('doclist') Here is the latest version of the class, it works, the page is displayed but the doclist seems empty. Do you know how I can debug this ? I've tried to add some sys.stderr.write('problem') in the code but I have no output ... class ListPage: '''This page shows the list of results due to the search ''' name = 'list' def page_enter(self, ctx): ctx.locals.doclist = [] ctx.add_session_vars('doclist') def page_process(self,ctx): if ctx.req_equals('detail'): ctx.set_page('detail') elif ctx.req_equals('recherche'): container = getContainer() for result in container.searchContains('title',ctx.locals.title): ctx.locals.doclist.append(result) if len(ctx.locals.doclist)<1 : ctx.locals.error = "No document found" ctx.set_page('error') else : ctx.add_session_vars('doclist') def page_display(self,ctx): ctx.load_template('headers.html').to_html(ctx) ctx.load_template('footer.html').to_html(ctx) ctx.run_template('list.html') -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 189 bytes Desc: Ceci est une partie de message num?riquement sign?e. URL: From pinted at tiscali.be Thu Jun 3 16:48:40 2004 From: pinted at tiscali.be (Didrik Pinte) Date: Thu, 03 Jun 2004 08:48:40 +0200 Subject: [albatross-users] newbie (coming from java) question In-Reply-To: <1086169206.4898.39.camel@geru-itea> References: <200406020756.RAA11526@lightning.itga.com.au> <1086169206.4898.39.camel@geru-itea> Message-ID: <1086245320.4898.59.camel@geru-itea> Le mer 02/06/2004 ? 11:40, Didrik Pinte a ?crit : > class ListPage: > '''This page shows the list of results due to the search > ''' > > name = 'list' > > def page_enter(self, ctx): > ctx.locals.doclist = [] > ctx.add_session_vars('doclist') > > def page_process(self,ctx): > if ctx.req_equals('detail'): > ctx.set_page('detail') > elif ctx.req_equals('recherche'): > container = getContainer() > for result in > container.searchContains('title',ctx.locals.title): > ctx.locals.doclist.append(result) > if len(ctx.locals.doclist)<1 : > ctx.locals.error = "No document found" > ctx.set_page('error') > else : > ctx.add_session_vars('doclist') > > def page_display(self,ctx): > ctx.load_template('headers.html').to_html(ctx) > ctx.load_template('footer.html').to_html(ctx) > ctx.run_template('list.html') Ok, i've added some debug to the page. I'm just printing out the name of the method called. It seems that it never goes in the page_process() method ... The workflow is this : 1) SearchPage : a form where the user enters a title, then submit de the from. Here is the class : class SearchPage: ''' This is the first page of the application. It contains a form with some fields that will be used to search the metadata ''' name = 'search' def page_enter(self, ctx): #Initialises the variables of the form to None ctx.locals.title = ctx.locals.denomination = ctx.locals.description = None def page_process(self,ctx): if ctx.req_equals('recherche'): ctx.set_page('list') def page_display(self,ctx): ctx.run_template('search.html') 2) Using the submit input name (recherche), the user must be redirected to the ListPage using the page_process() method of SearchPage, no ? Didrik