Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Z
Zope
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
Zope
Commits
3ca45ffc
Commit
3ca45ffc
authored
Mar 03, 2004
by
Christian Theune
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- Added Post Traversal Hook and corresponding tests
parent
830b6bd4
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
168 additions
and
3 deletions
+168
-3
lib/python/ZPublisher/BaseRequest.py
lib/python/ZPublisher/BaseRequest.py
+27
-3
lib/python/ZPublisher/tests/testPostTraversal.py
lib/python/ZPublisher/tests/testPostTraversal.py
+141
-0
No files found.
lib/python/ZPublisher/BaseRequest.py
View file @
3ca45ffc
...
...
@@ -12,7 +12,7 @@
##############################################################################
""" Basic ZPublisher request management.
$Id: BaseRequest.py,v 1.5
4 2003/11/28 16:46:47 jim
Exp $
$Id: BaseRequest.py,v 1.5
5 2004/03/03 11:07:01 ctheune
Exp $
"""
from
urllib
import
quote
...
...
@@ -252,6 +252,9 @@ class BaseRequest:
request
[
'TraversalRequestNameStack'
]
=
request
.
path
=
path
# Set the posttraverse for duration of the traversal here
self
.
_post_traverse
=
post_traverse
=
[]
entry_name
=
''
try
:
# We build parents in the wrong order, so we
...
...
@@ -377,6 +380,9 @@ class BaseRequest:
finally
:
parents
.
reverse
()
# After traversal post traversal hooks aren't available anymore
del
self
.
_post_traverse
request
[
'PUBLISHED'
]
=
parents
.
pop
(
0
)
# Do authorization checks
...
...
@@ -449,8 +455,21 @@ class BaseRequest:
# Remove http request method from the URL.
request
[
'URL'
]
=
URL
# Run post traversal hooks here
result
=
None
if
post_traverse
:
result
=
exec_callables
(
post_traverse
)
if
result
is
not
None
:
object
=
result
return
object
def
post_traverse
(
self
,
f
,
args
=
()):
"""Set a callable object and argument tuple to be combined if traversal succeeds."""
if
hasattr
(
self
,
"_post_traverse"
):
self
.
_post_traverse
.
append
((
f
,
tuple
(
args
)))
retry_count
=
0
def
supports_retry
(
self
):
return
0
...
...
@@ -459,8 +478,13 @@ class BaseRequest:
"""
self
.
_held
=
self
.
_held
+
(
object
,)
def
exec_callables
(
callables
):
result
=
None
for
(
f
,
args
)
in
callables
:
# Don't catch exceptions here. And don't hide them anyway.
result
=
f
(
*
args
)
if
result
is
not
None
:
return
result
def
old_validation
(
groups
,
request
,
auth
,
roles
=
UNSPECIFIED_ROLES
):
...
...
lib/python/ZPublisher/tests/testPostTraversal.py
0 → 100644
View file @
3ca45ffc
from
unittest
import
TestCase
,
TestSuite
,
makeSuite
,
main
import
Zope
Zope
.
startup
()
from
Acquisition
import
Implicit
from
ZPublisher.BaseRequest
import
BaseRequest
from
ZPublisher.HTTPResponse
import
HTTPResponse
# Various post traversal methods
pt_simple_was_run
=
0
def
pt_simple
():
global
pt_simple_was_run
pt_simple_was_run
=
1
pass
def
pt_static_arg
(
request
,
b
):
request
.
set
(
'b'
,
b
)
pass
def
pt_simple_redirect
(
a
):
return
a
def
pt_chain_test
(
request
,
string
):
request
.
set
(
'a'
,
request
.
get
(
'a'
,
''
)
+
string
)
class
DummyObjectBasic
(
Implicit
):
""" Dummy class with docstring.
"""
def
_setObject
(
self
,
id
,
object
):
setattr
(
self
,
id
,
object
)
return
getattr
(
self
,
id
)
def
view
(
self
):
""" Attribute with docstring.
"""
return
'view content'
class
DummyObjectWithPTHook
(
DummyObjectBasic
):
""" Dummy class with docstring.
"""
traversal
=
[]
def
__before_publishing_traverse__
(
self
,
object
,
REQUEST
):
for
x
in
self
.
traversal
:
REQUEST
.
post_traverse
(
*
x
)
class
TestBaseRequestPT
(
TestCase
):
def
setUp
(
self
):
self
.
root
=
DummyObjectBasic
()
self
.
f1
=
self
.
root
.
_setObject
(
'folder'
,
DummyObjectBasic
()
)
self
.
f1
.
_setObject
(
'objBasic'
,
DummyObjectWithPTHook
()
)
def
makeBaseRequest
(
self
):
response
=
HTTPResponse
()
environment
=
{
'URL'
:
''
,
'PARENTS'
:
[
self
.
root
],
'steps'
:
[],
'_hacked_path'
:
0
,
'_test_counter'
:
0
,
'response'
:
response
}
return
BaseRequest
(
environment
)
def
test_post_basic
(
self
):
global
pt_simple_was_run
pt_simple_was_run
=
0
r
=
self
.
makeBaseRequest
()
# Set hook
self
.
f1
.
objBasic
.
traversal
=
[(
pt_simple
,)]
x
=
r
.
traverse
(
'folder/objBasic'
)
# Object should be self.f1.objBasic
self
.
assertEqual
(
x
,
self
.
f1
.
objBasic
)
self
.
assertEqual
(
pt_simple_was_run
,
1
)
self
.
f1
.
objBasic
.
traversal
=
[]
def
test_post_arg
(
self
):
r
=
self
.
makeBaseRequest
()
b
=
1
self
.
f1
.
objBasic
.
traversal
=
[(
pt_static_arg
,
(
r
,
b
))]
x
=
r
.
traverse
(
'folder/objBasic'
)
# Object should have been traversed normally
self
.
assertEqual
(
x
,
self
.
f1
.
objBasic
)
self
.
assertEqual
(
r
.
get
(
'b'
,
0
),
b
)
self
.
f1
.
objBasic
.
traversal
=
[]
def
test_hook_chain
(
self
):
r
=
self
.
makeBaseRequest
()
self
.
f1
.
objBasic
.
traversal
=
[
(
pt_chain_test
,
(
r
,
'a'
)),
(
pt_chain_test
,
(
r
,
'b'
)),
(
pt_chain_test
,
(
r
,
'c'
)),
(
pt_chain_test
,
(
r
,
'd'
))]
x
=
r
.
traverse
(
'folder/objBasic'
)
self
.
assertEqual
(
r
.
get
(
'a'
,
''
),
'abcd'
)
self
.
f1
.
objBasic
.
traversal
=
[]
def
test_hook_redirect
(
self
):
r
=
self
.
makeBaseRequest
()
check
=
[]
self
.
f1
.
objBasic
.
traversal
=
[(
pt_simple_redirect
,
(
check
,))]
x
=
r
.
traverse
(
'folder/objBasic'
)
self
.
assertEqual
(
x
,
check
)
def
test_hook_chain_redirect
(
self
):
r
=
self
.
makeBaseRequest
()
check
=
[]
self
.
f1
.
objBasic
.
traversal
=
[
(
pt_chain_test
,
(
r
,
'a'
)),
(
pt_chain_test
,
(
r
,
'b'
)),
(
pt_chain_test
,
(
r
,
'c'
)),
(
pt_simple_redirect
,
(
check
,)),
(
pt_simple_redirect
,
(
1
,)),
(
pt_chain_test
,
(
r
,
'd'
))]
x
=
r
.
traverse
(
'folder/objBasic'
)
self
.
assertEqual
(
r
.
get
(
'a'
,
''
),
'abc'
)
self
.
assertEqual
(
x
,
check
)
def
test_suite
():
return
TestSuite
(
(
makeSuite
(
TestBaseRequestPT
),
)
)
if
__name__
==
'__main__'
:
main
(
defaultTest
=
'test_suite'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment