[Zope] An accessrule in python script

Danny William Adair danny@adair.net
Wed, 3 Oct 2001 09:10:38 +1200


Hi Gilles!

Yes, your Python Script is _not_ equivalent. :-)

Try this (untested):
------------------------------------------
from string import lower
request = context.REQUEST
machine = lower(request.HTTP_HOST)
servermap = {
  'www.site1.com': 'site1',
  'www.site2.com': 'site2'
  }
if servermap.has_key(machine):
    request['TraversalRequestNameStack'].append(servermap[machine])
------------------------------------------


Explanation:
(sorry if this looks insultingly simple to you)

> machine = _.string.lower(context.REQUEST.HTTP_HOST)
better be:
------------------------------------------
from string import lower
machine = lower(context.REQUEST.HTTP_HOST)
------------------------------------------
from python scripts you don't access libraries through the current DTML 
namespace (check the "Bindinds" tab if you need to use the current DTML 
namespace), they're already there and available. You just import the library, 
or - as in this case - one or two functions "from" the library. If you had a 
line like "import string" you refer to the "lower" function as 
"string.lower", if you import a function directly, it's accessable direcly 
through its name (as in the above code).


btw, with a little string-splitting (very easy) you could also check for 
*.site1.com (not only "www")...


>     context.REQUEST.append(servermap[machine])
mmmh, never tried that. I guess this won't work. My access rule shows 
something like:
------------------------------------------
container.REQUEST['TraversalRequestNameStack'].append(servermap[machine])
------------------------------------------
this will append "site1" (resp. "site2"...) to the stack of object names that 
will be "climbed" through when an object is accessed (remember: your access 
rule is a special object that gets called _before_ Zope traverses any 
further). This results in an object call one level further down in your 
object hierarchy. (Yes, you could bring the request even further down by 
adding more object names...)


if you use "context.REQUEST" a couple of times,
------------------------------------------
request = context.REQUEST
------------------------------------------
as a first line (uh, second, first you import your (string) function(s)) 
might be handy, as seen in the "default" Python Script that you see when you 
add a new Python Script. After this line you can refer to the 
"context.REQUEST" object with shorter code, e.g. your (currently) first line 
would become 
------------------------------------------
machine = lower(request.HTTP_HOST)
------------------------------------------
and your last line would become
------------------------------------------
request['TraversalRequestNameStack'].append(servermap[machine])
------------------------------------------

hope this helps,
Danny

On Wednesday 03 October 2001 03:11, Gilles Lenfant wrote:
> Hi,
>
> I wanted to improve the speed of my sites translating the DTML method
> accessrule into a python script.
> But when the DTML method works, the equivalent (?) python script doesn't
> (always show the default Zope page).
>
> Any clue ?
>
> Thanks in advance
>
> --Gilles
>
> ========My DTML method========
> <dtml-let domaine="_.string.split(_.string.lower(_.string.split(HTTP_HOST,
> ':')[0]), '.')[-3:]">
> <dtml-if "_['domaine']==['www', 'site1', 'com']">
>    <dtml-call "REQUEST.path.append('site1')">
> <dtml-elif "_['domaine']==['www', 'site2', 'com']">
>    <dtml-call "REQUEST.path.append('site2')">
> </dtml-if>
> </dtml-let>
> ========End DTML method========
> ========My Python script ========
> machine = _.string.lower(context.REQUEST.HTTP_HOST)
> servermap = {
>   'www.site1.com': 'site1',
>   'www.site2.com': 'site2'
>   }
> if servermap.has_key(machine):
>     context.REQUEST.append(servermap[machine])
> return
>
>
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists -
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope-dev )