Code

ded1d29ef46fe2f5f5d976b391ae682aca21d429
[roundup.git] / roundup / cgi / PageTemplates / PathIterator.py
1 ##############################################################################
2 #
3 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
4
5 # This software is subject to the provisions of the Zope Public License,
6 # Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10 # FOR A PARTICULAR PURPOSE
11
12 ##############################################################################
14 """Path Iterator
16 A TALES Iterator with the ability to use first() and last() on
17 subpaths of elements.
18 """
20 __version__='$Revision: 1.1 $'[11:-2]
22 import TALES
23 from Expressions import restrictedTraverse, Undefs, getSecurityManager
24 from string import split
26 class Iterator(TALES.Iterator):
27     def __bobo_traverse__(self, REQUEST, name):
28         if name in ('first', 'last'):
29             path = REQUEST['TraversalRequestNameStack']
30             names = list(path)
31             names.reverse()
32             path[:] = [tuple(names)]
33         return getattr(self, name)
35     def same_part(self, name, ob1, ob2):
36         if name is None:
37             return ob1 == ob2
38         if isinstance(name, type('')):
39             name = split(name, '/')
40         name = filter(None, name)
41         securityManager = getSecurityManager()
42         try:
43             ob1 = restrictedTraverse(ob1, name, securityManager)
44             ob2 = restrictedTraverse(ob2, name, securityManager)
45         except Undefs:
46             return 0
47         return ob1 == ob2