Commit 2f3a791a authored by Georgios Dagkakis's avatar Georgios Dagkakis

erp5_core: add script Base_constructUrlFor. As an API to be able to construct urls

for a base document, given the form_id, document_reference and GET parameters

Commit contains also a test in testERP5Core
parent acb26ce5
'''
- context: should the document that we need as base for the url.
In case it is not given, the context will be used
- form_id: the form that is invoked
- document_reference: the reference of a document that is rendered,
- parameter_dict: dictionary containing all get parameters
- add_trailing_slash: set to True if we want to add trailing slash
'''
from ZTUtils import make_query
absolute_url = context.absolute_url()
if context.getPortalType() in ('Web Section', 'Web Site') and absolute_url.endswith('/'):
# Web Section and Web Site absulute_url should be overriden to add trailing slash.
# Yet we do chekck if it ends with slash, for backwards compatibility
url = absolute_url[:-1]
else:
url = absolute_url
# form_id and document_reference are mutually exclusive,
# we should not expect both in a url
assert not (form_id and document_reference), 'Not allowed to have both form and document in the same url'
# Note that form_id and document_reference are handled the same way,
# it is different variable for semantic reasons
if form_id:
url = '%s/%s' % (url, form_id)
elif document_reference:
url = '%s/%s' % (url, document_reference)
if add_trailing_slash:
url = url + '/'
if parameter_dict:
url = '%s?%s' % (url, make_query(parameter_dict))
return url
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>Script_magic</string> </key>
<value> <int>3</int> </value>
</item>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
</klass>
<tuple/>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_container</string> </key>
<value> <string>container</string> </value>
</item>
<item>
<key> <string>name_context</string> </key>
<value> <string>context</string> </value>
</item>
<item>
<key> <string>name_m_self</string> </key>
<value> <string>script</string> </value>
</item>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>form_id=None, document_reference=None, parameter_dict=None, add_trailing_slash=False</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Base_constructUrlFor</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
...@@ -653,6 +653,60 @@ class TestERP5Core(ERP5TypeTestCase, ZopeTestCase.Functional): ...@@ -653,6 +653,60 @@ class TestERP5Core(ERP5TypeTestCase, ZopeTestCase.Functional):
self.assertEqual(response.getStatus(), 401) self.assertEqual(response.getStatus(), 401)
self.assertNotIn("Also, the following error occurred", str(response)) self.assertNotIn("Also, the following error occurred", str(response))
def test_Base_constructUrlFor(self):
# check urls built for portal
portal_url = self.portal.absolute_url()
self.assertEqual(self.portal.Base_constructUrlFor(), '%s' % portal_url)
self.assertEqual(self.portal.Base_constructUrlFor(
add_trailing_slash=True), '%s/' % portal_url
)
# check urls built for a mofule
module_id = 'test_module'
module = self.portal.newContent(portal_type='Folder', id=module_id)
module_url = module.absolute_url()
self.assertEqual(
module.Base_constructUrlFor(),
'%s/%s' % (portal_url, module_id)
)
self.assertEqual(
module.Base_constructUrlFor(add_trailing_slash=True),
'%s/%s/' % (portal_url, module_id)
)
# check url built for a form
form_id = 'view'
self.assertEqual(
module.Base_constructUrlFor(form_id=form_id),
'%s/%s' % (module_url, form_id)
)
# check url built for a document reference
file_reference = 'test_file_reference'
self.portal.test_module.newContent(
portal_type='File',
reference=file_reference
)
self.assertEqual(
module.Base_constructUrlFor(document_reference=file_reference),
'%s/%s' % (module_url, file_reference)
)
# check that form_id and document_reference are exclusive
# check url built with GET parameters
self.assertRaises(
AssertionError,
module.Base_constructUrlFor,
form_id=form_id,
document_reference=file_reference
)
parameter_dict = {'ingore_layout': 1, 'came_from': 'foo/bar'}
self.assertIn(
module.Base_constructUrlFor(
parameter_dict=parameter_dict, add_trailing_slash=True
),
(
'%s/%s/?came_from=foo/bar&ingore_layout:int=1' % (portal_url, module_id),
'%s/%s/?ingore_layout:int=1&came_from=foo/bar' % (portal_url, module_id),
)
)
def test_suite(): def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestERP5Core)) suite.addTest(unittest.makeSuite(TestERP5Core))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment