[ZPT] Has_role in ZPT (and if-then-else) ?

Tony McDonald tony.mcdonald@ncl.ac.uk
Sun, 21 Oct 2001 14:34:17 +0100


On 20/10/01 9:05 pm, "Dieter Maurer" <dieter@handshake.de> wrote:

> Tony McDonald writes:

>> We were trying to change this code;
>> <dtml-if "AUTHENTICATED_USER.has_role('fmcc_man') or
>> AUTHENTICATED_USER.has_role('fmcc_admin') or
>> AUTHENTICATED_USER.has_role('fmcc') or AUTHENTICATED_USER.has_role('ltsn')
>> or AUTHENTICATED_USER.has_role('ltsn_man')">
>> 
>> <select> ... </select>
>> </dtml-if>
> The direct translation is
> 
> <select tal:condition="python: user.has_role('fmcc_man')
>                                or user.has_role('fmcc_admin')
> or ....">
>    ...
> </select>
> 

Thanks Dieter,
The code that actually worked was
 <select tal:condition="python: (user.has_role('fmcc_man')
                                or user.has_role('fmcc_admin'))">

(the PT didn't save if the brackets were omitted)

>> I then made a PythonScript that I thought might work;
>> ...
>> if request.AUTHENTICATED_USER.has_role(therole):
>>     return 1
>> else:
>>     return 0
>> 
>> Now this *does* work if the AUTHENTICATED_USER has the role I'm looking for
>> (ie '1' returned). If AUTHENTICATED_USER does not have that role, this
>> PythonScript doesn't even return :(
> What does happen instead?
> An exception, an infinite loop?
> 

Seemed like an infinite loop - no exception came back.

> In fact, the Python Script should work.
> 
>> My colleague also asked about the
>> <dtml-if >
>> ....
>> <dtml-else>
>> ....
>> </dtml-if>
>> 
>> construct too, and how this might be done in ZPT. Again, what's the best
>> practice on this as well?
> ZPT does not have a two way conditional. Thus, the direct
> translation is:
> 
>  <tag condition="expr">
>    ....
>  </tag>
>  <tag condition="not: expr">
>    ....
>  </tag>
> 

I thought that might be the case, I think I might be able to convince my
colleague that we can work in this way!

Of course, we could 'split' the PT into two parts, eg

<tag condition="expr">
 <div metal:use-macro="here/mymacros/macros/ablock"></div>
</tag>
<tag condition="not: expr">
 <div metal:use-macro="here/mymacros/macros/ablock"></div>
</tag>

Not optimal, but I can probably live with it.
Tone
-- 
Dr Tony McDonald,  Assistant Director, FMCC, http://www.fmcc.org.uk/
The Medical School, Newcastle University Tel: +44 191 243 6140
A Zope list for UK HE/FE  http://www.fmcc.org.uk/mailman/listinfo/zope