[albatross-users] Random pages and application model
Matt Goodall
matt at pollenation.net
Sat Feb 22 12:37:28 EST 2003
On Fri, 2003-02-21 at 04:39, Dave Cole wrote:
> > There's a nice description of how the application model relates to
> > the page_xxx() methods for 'stateful' applications in the mailing
> > list archive but I've not come across an equivalent explanation for
> > 'random' applications.
> >
> > >From debug traces I see the following sequence whenever the page is
> > requested:
> >
> > page_enter(ctx)
> > page_process(ctx)
> > page_display(ctx)
> >
> > I have no problems with page_process() and page_display() but I
> > don't quite understand what use page_enter() has? It seems a bit
> > redundant to me since the request parameters are not merged into
> > ctx.locals until **after** page_enter() is called.
> >
> > It's probably worth explaining that what I expected to be able to do
> > is something like this (not tested):
> >
> > page1.py
> > --------
> >
> > def page_process(ctx):
> > if ctx.req_equals('edit'):
> > ctx.redirect('page2?objid=%s' % ctx.locals.objid)
>
> Doesn't that work?
>
> > page2.py
> > --------
> >
> > def page_enter(ctx):
> > ctx.locals.obj = get_obj_from_somewhere(ctx.locals.objid)
>
> Ahhh...
>
> In a random application the page_enter() function gets called every
> request. You could just take the page_enter() code and prepend it to
> the page_process() function.
>
> > def page_process(ctx):
> > if ctx.req_equals('submit'):
> > # save data
> > ctx.redirect('page1')
> > elif ctx.req_equals('cancel'):
> > ctx.redirect('page1')
> >
> > def page_display(ctx):
> > ctx.locals.attr1 = ctx.locals.obj.attr1
> > ctx.locals.attr2 = ctx.locals.obj.attr2
> > ctx.run_template('page2.html')
> >
> >
> > The problem is that, as mentioned above, the request parameter,
> > objid, is not available during page_enter(), it's only there for
> > page_process().
>
> That is a bit of a bugger. One thing you could do is subclass the
> application and replace the run() method with a slightly re-ordered
> sequence.
True, should be easy enough.
>
> class MyApp(RandomModularSessionApp):
> def run(self, req):
> # My own processing sequence copied from
> # RandomModularApp.run() but slightly resequenced.
>
> > One workaround is to put the object loading code in an else block in
> > page_process which doesn't feel like the right place for it. The
> > alternative is to transfer objid using a session var but I prefer
> > not to use the session where possible - it's just something else
> > that needs managing.
>
> See if overriding the application run method gives you what you need.
>
> The primary design goal of Albatross was to provide a toolkit from
> which you can build your own application models. We provide some
> examples pre-rolled which just happen to be useful for certain classes
> of application.
Fair point, I should make sure I understand all the mixins properly and
see if a DIY application class might suit me better.
>
> I am more than a little interested to see if replacing
> Application.run() is useful to you. Does it make your brain hurt, or
> is it something useful?
It works fine and it was easy. All I did was move
self.merge_request(ctx) to above self.load_page(ctx).
However, after thinking more about where the loading code should go made
me realise that I don't want to put it in page_enter() anyway. That's
called every time the page is requested, even when a form on the page is
submitted, so I was being stupid ;-).
Is there actually a good reason why request parameters are not merged
into ctx.locals before page_enter() is called? It seems strange that the
session attributes are there but not the request.
>
> - Dave
--
Matt Goodall, Technical Director
Pollenation Internet Ltd, http://www.pollenation.net
e: matt at pollenation.net
t: 0113 2252500
More information about the Albatross-users
mailing list