Commit f6e81b41 authored by Georgios Dagkakis's avatar Georgios Dagkakis

FolderMixIn: add method 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 testERP5Type
parent 19b687ab
......@@ -74,6 +74,7 @@ import warnings
from urlparse import urlparse
REINDEX_SPLIT_COUNT = 100 # if folder containes more than this, reindexing should be splitted.
from Products.ERP5Type.Message import translateString
from ZTUtils import make_query
# Dummy Functions for update / upgrade
def dummyFilter(object,REQUEST=None):
......@@ -496,6 +497,48 @@ class FolderMixIn(ExtensionClass.Base):
raise AccessControl_Unauthorized(method_id)
return self._recurseCallMethod(method_id, restricted=True, *args, **kw)
security.declarePublic('constructUrlFor')
def constructUrlFor(
self,
form_id=None,
document_reference=None,
parameter_dict=None,
**kw
):
'''
Utility method to help in the construction of urls.
It should be run in the context of a document that we want as base for the url.
Arguments:
- form_id: the form that is invoked
- document_reference: the reference of a document that is rendered,
- parameter_dict: dictionary containing all get parameters
Other parameters can be used for type based methods.
'''
# Try to get type based method, but not for ERP5 Site, since
# _getTypeBasedMethod is defined in Base so not available for ERP5 Site
if self.getPortalType() != 'ERP5 Site':
method = self._getTypeBasedMethod('constructUrlFor')
if method is not None:
return method(
form_id=form_id,
document_reference=document_reference,
parameter_dict=parameter_dict,
**kw
)
url = self.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 parameter_dict:
url = '%s?%s' % (url, make_query(parameter_dict))
return url
security.declarePublic('isURLAncestorOf')
def isURLAncestorOf(self, given_url):
"""
......
......@@ -3244,6 +3244,54 @@ return [
# but this did not affect the other role
self.assertTrue(hasRole(role2))
def test_constructUrlFor(self):
portal_url = self.portal.absolute_url()
# check url built for portal
portal_url = self.portal.absolute_url()
self.assertEqual(self.portal.constructUrlFor(), '%s' % portal_url)
# check urls built for a module
module_id = 'test_module'
module = self.portal.newContent(portal_type='Folder', id=module_id)
module_url = module.absolute_url()
self.assertEqual(
module.constructUrlFor(),
'%s/%s' % (portal_url, module_id)
)
# check url built for a form
form_id = 'view'
self.assertEqual(
module.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.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.constructUrlFor,
form_id=form_id,
document_reference=file_reference
)
parameter_dict = {'ignore_layout': 1, 'came_from': 'foo/bar'}
self.assertIn(
module.constructUrlFor(
parameter_dict=parameter_dict,
),
(
'%s/%s?came_from=foo/bar&ignore_layout:int=1' % (portal_url, module_id),
'%s/%s?ignore_layout:int=1&came_from=foo/bar' % (portal_url, module_id),
)
)
class TestAccessControl(ERP5TypeTestCase):
# Isolate test in a dedicaced class in order not to break other tests
# when this one fails.
......
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