From pinted at tiscali.be Tue Jun 1 14: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> --=-e3f1CN2/TLfblUqWk4Nt Content-Type: text/plain Content-Transfer-Encoding: quoted-printable 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 =3D 'list' def page_process(self,ctx): if ctx.req_equals('detail'): ctx.set_page('detail') if ctx.req_equals('recherche'): container =3D ctx.getContainer() docInfoList =3D [] for result in container.searchContains('title',ctx.locals.title): docInfoList.append(result) if len(docInfoList)<1 : ctx.locals.error =3D "No document found" ctx.locals.doclist =3D [] ctx.set_page('error') else : ctx.locals.doclist =3D docInfoList ctx.add_session_vars('doclist') =20 def page_display(self,ctx): ctx.load_template('headers.html').to_html(ctx) ctx.load_template('footer.html').to_html(ctx) =20 ctx.run_template('list.html') class ErrorPage: ''' Managing errors ''' =20 name =3D '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 =3D None def _initialiseContainer(self): self.metaData =3D MetaData('mila.dbxml') =20 def getContainer(self): if self.metaData =3D=3D None: self._initialiseContainer() return self.metaData class App(SimpleSessionApp): def __init__(self): SimpleSessionApp.__init__(self, base_url=3D'application.py', template_path=3D'.', start_page=3D'search', secret=3D'-=3D-secret-=3D-', session_appid=3D'metadata') for page in (SearchPage,ListPage,DetailPage,ErrorPage): self.register_page(page.name, page()) def create_context(self): return ApplicationContext(self) if __name__ =3D=3D '__main__': app.run(Request()) --=-e3f1CN2/TLfblUqWk4Nt Content-Type: application/pgp-signature; name=signature.asc Content-Description: Ceci est une partie de message =?ISO-8859-1?Q?num=E9riquement?= =?ISO-8859-1?Q?_sign=E9e=2E?= -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (GNU/Linux) iD8DBQBAvJcJ9Rlh4Zs4yBMRAs7HAKCs/tlb9f0wyqTInGK9E7+2a3kKdACdG7dW +2fAltiixzhgtbQbOfhjOb0= =k1tU -----END PGP SIGNATURE----- --=-e3f1CN2/TLfblUqWk4Nt-- From gnb at itga.com.au Wed Jun 2 00: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@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@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 07: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> --=-hhfoHHT/DFyABMfvTCo7 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Le mer 02/06/2004 =E0 02:32, Gregory Bond a =E9crit : > A couple of things spring to mind. >=20 > pinted@tiscali.be said: > > if ctx.req_equals('detail'): > > ctx.set_page('detail') > > if ctx.req_equals('recherche'):=20 >=20 > this should usually be elif - the set_page() call doesn't terminate the=20 > page_process function (tho depending on the html template, you may never = hit=20 > the case where detail and recherche are both true...) Great ! First intersting information. >=20 > > docInfoList =3D [] > > for result in container.searchContains('title',ctx.locals.title): > > docInfoList.append(result) >=20 > Any particular reason this is not written as=20 > docInfoList =3D container.searchContains(....) > (this is a general python nit not an albatross nit!) >=20 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 wri= tten > will have a single metadata object that is recreated each time an HTTP re= quest > 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@tiscali.be said: > > My code does not work (what a shame !) >=20 > Apart from the above, it looks reasonable enough to me. To go further, w= e'd > need a fair bit more info. What "does not work'? Does it throw exceptio= ns? > 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 =3D 'list' =20 def page_process(self,ctx): if ctx.req_equals('detail'): ctx.set_page('detail') if ctx.req_equals('recherche'): container =3D ctx.getContainer() docInfoList =3D [] for result in container.searchContains('title',ctx.locals.title): self.docInfoList.append(result) if len(self.docInfoList)<1 : ctx.locals.error =3D "No document found" ctx.set_page('error') else : ctx.locals.doclist =3D self.docInfoList ctx.add_session_vars('doclist') =20 =20 def page_display(self,ctx): ctx.load_template('headers.html').to_html(ctx) ctx.load_template('footer.html').to_html(ctx) =20 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 =3D 'list' =20 def page_process(self,ctx): if ctx.req_equals('detail'): ctx.set_page('detail') =20 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 =3D ctx.getContainer() docInfoList =3D [] for result in container.searchContains('title',ctx.locals.title): self.docInfoList.append(result) if len(self.docInfoList)<1 : ctx.locals.error =3D "No document found" ctx.locals.doclist =3D [] ctx.set_page('error') else : ctx.locals.doclist =3D self.docInfoList ctx.add_session_vars('doclist') =20 ctx.run_template('list.html') --=-hhfoHHT/DFyABMfvTCo7 Content-Type: application/pgp-signature; name=signature.asc Content-Description: Ceci est une partie de message =?ISO-8859-1?Q?num=E9riquement?= =?ISO-8859-1?Q?_sign=E9e=2E?= -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (GNU/Linux) iD8DBQBAvX969Rlh4Zs4yBMRAmeLAJ4ki+yLrwc36Lde91aOSr1dbD3EwgCgkEue fK+f3fPJyESlR45WHQxPrZc= =B7n2 -----END PGP SIGNATURE----- --=-hhfoHHT/DFyABMfvTCo7-- From gnb at itga.com.au Wed Jun 2 07: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 09: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> --=-N2bydbjLKMJJMC8PYba/ Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Le mer 02/06/2004 =E0 09:56, Gregory Bond a =E9crit : > Oh OK I think I see the problem. >=20 > The _first time_ you get to the ListPage object, the call seq will be > prevpage.page_exit() > ListPage.page_enter() > ListPage.page_display() >=20 > ctx.locals.doclist is not set because it is only set once someone hits th= e > "recherche" button and ListPager.page_process() is called. >=20 > the fix: make ListPage.page_enter() like this: > def page_enter(self, ctx): > self.locals.doclist =3D [] > ctx.add_session_variables('doclist') Here is the latest version of the class, it works, the page is displayed but the doclist seems empty.=20 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 =3D 'list' def page_enter(self, ctx): ctx.locals.doclist =3D [] ctx.add_session_vars('doclist') =20 def page_process(self,ctx): if ctx.req_equals('detail'): ctx.set_page('detail') elif ctx.req_equals('recherche'): container =3D getContainer() for result in container.searchContains('title',ctx.locals.title): ctx.locals.doclist.append(result) if len(ctx.locals.doclist)<1 : ctx.locals.error =3D "No document found" ctx.set_page('error') else : ctx.add_session_vars('doclist') =20 def page_display(self,ctx): ctx.load_template('headers.html').to_html(ctx) ctx.load_template('footer.html').to_html(ctx) =20 ctx.run_template('list.html') --=-N2bydbjLKMJJMC8PYba/ Content-Type: application/pgp-signature; name=signature.asc Content-Description: Ceci est une partie de message =?ISO-8859-1?Q?num=E9riquement?= =?ISO-8859-1?Q?_sign=E9e=2E?= -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (GNU/Linux) iD8DBQBAvaB19Rlh4Zs4yBMRAuO0AKCagU5oMVBEAUO4xYG0gwUeqb2udQCfXsAQ 5Pwfk/nk23b/75W6Yz7wQhw= =QW/p -----END PGP SIGNATURE----- --=-N2bydbjLKMJJMC8PYba/-- From pinted at tiscali.be Thu Jun 3 06: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