[ZPT] Page Template Preconditions?
Jeffrey P Shell
jeffrey@cuemedia.com
Fri, 09 Aug 2002 13:42:58 -0600
On a site I did recently - one of my first experiences using Session
management in Zope actually - I ran into a recurring need for preconditions.
There were certain pages where I had to check if the session has a 'user_id'
key in it - it's simple user management, there's no need (or desire) to put
in some user folder variation for this site. In DTML, I would have added
in:
<dtml-unless expr="REQUEST.SESSION.has_key('user_id')">
<dtml-raise redirect>(url to notification of session timeout)</dtml-raise>
or
<dtml-call expr="REQUEST.RESPONSE.redirect(...)">
</dtml-unless>
I tried and tried to find a way to do something similar in page templates,
but usually ended up with hideous (and very hacky) TALES expressions that
never worked anyways. So then I tried doing a <..tal:define="dummy
python:here.checkSession(...)">, but in any combination I tried the redirect
never happened - raising one just brought up TALESError, and I think a
'RESPONSE.redirect(...)' call just got swallowed.
Ultimately, I put some logic into standard_error_message that specially
caught Attribute/Key/NameError's and tried to determine if access one of the
two SESSION variables I was tracking might be the culprit, and then display
a "session timeout" information page. It still feels pretty hacky.
Other solutions I thought about also felt hacky, such as having some sort of
Python Script "wrapper" with traverse_subpath would make auto-URL generation
difficult and URL enforcement (making sure these pages were always accessed
via the Python Script) tricky.
So what I'm wondering is if there's been any thoughts to doing some sort of
'precondition' script/expression, or adding in some event hooks to Page
Templates? ASP.NET (and possibly earlier versions) has a Page_Load(...)
event handler that developers can write. I think something can be added to
the Zope Page Template to do something similar, and allow the
precondition/PageLoadHandler to redirect on its own, or prepare data to be
used in the Page Template.
--
Jeffrey P Shell
www.cuemedia.com