diff --git a/product/ERP5/Tool/TemplateTool.py b/product/ERP5/Tool/TemplateTool.py index 6ad389693f06ef3faef322fa85bffc8226ed1cf1..046e5460b02c5e329387ee9c0b85c78322050f96 100644 --- a/product/ERP5/Tool/TemplateTool.py +++ b/product/ERP5/Tool/TemplateTool.py @@ -44,6 +44,7 @@ from tempfile import mkstemp, mkdtemp from Products.ERP5 import _dtmldir from cStringIO import StringIO from urllib import pathname2url, urlopen, splittype, urlretrieve +import urllib2 import re from xml.dom.minidom import parse from xml.parsers.expat import ExpatError @@ -1051,4 +1052,36 @@ class TemplateTool (BaseTool): opreation_log.append('Not found in repositories %s' % template_name) return opreation_log + security.declareProtected(Permissions.ManagePortal, + 'getBusinessTemplateUrl') + def getBusinessTemplateUrl(self, base_url_list, bt5_title): + """ + This method verify if the business template are available + into one url (repository). + """ + # This list could be preconfigured at some properties or + # at preferences. + for base_url in base_url_list: + url = "%s/%s" % (base_url, bt5_title) + if base_url == "INSTANCE_HOME_REPOSITORY": + url = "file://%s/bt5/%s" % (getConfiguration().instancehome, + bt5_title) + LOG('ERP5', INFO, "TemplateTool: INSTANCE_HOME_REPOSITORY is %s." \ + % url) + try: + urllib2.urlopen(url) + return url + except (urllib2.HTTPError, OSError): + # XXX Try again with ".bt5" in case the folder format be used + # Instead tgz one. + url = "%s.bt5" % url + try: + urllib2.urlopen(url) + return url + except (urllib2.HTTPError, OSError): + pass + LOG('ERP5', INFO, 'TemplateTool: %s was not found into the url list: ' + '%s.' % (bt5_title, base_url_list)) + return None + InitializeClass(TemplateTool) diff --git a/product/ERP5/tests/testBusinessTemplate.py b/product/ERP5/tests/testBusinessTemplate.py index c7722e6185449e25e8b8d91dd0bce9e48df3cc10..f916cceb64a0dd906367c7799c3700a7b5b5b923 100644 --- a/product/ERP5/tests/testBusinessTemplate.py +++ b/product/ERP5/tests/testBusinessTemplate.py @@ -6385,6 +6385,40 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor): self.assertFalse(getattr(portal.some_file, 'isClassOverriden', False)) self.assertFalse(getattr(portal.another_file, 'isClassOverriden', False)) + def test_getBusinessTemplateUrl(self): + """ Test if this method can find which repository is the business + template + """ + # How to define an existing and use INSTANCE_HOME_REPOSITORY? + url_list = [ 'https://svn.erp5.org/repos/public/erp5/trunk/bt5', + 'http://www.erp5.org/dists/snapshot/bt5', + 'http://www.erp5.org/dists/release/5.4.5/bt5', + 'file:///opt/does/not/exist', + "INSTANCE_HOME_REPOSITORY"] + + exist_bt5 = 'erp5_base' + not_exist_bt5 = "erp5_not_exist" + template_tool = self.portal.portal_templates + getBusinessTemplateUrl = template_tool.getBusinessTemplateUrl + + # Test Exists + self.assertEquals(getBusinessTemplateUrl(url_list, exist_bt5), + 'https://svn.erp5.org/repos/public/erp5/trunk/bt5/erp5_base') + self.assertEquals(getBusinessTemplateUrl(url_list[1:], exist_bt5), + 'http://www.erp5.org/dists/snapshot/bt5/erp5_base.bt5') + self.assertEquals(getBusinessTemplateUrl(url_list[2:], exist_bt5), + 'http://www.erp5.org/dists/release/5.4.5/bt5/erp5_base.bt5') + self.assertEquals(getBusinessTemplateUrl(url_list[3:], exist_bt5), None) + # XXX Remains test for INSTANCE_HOME_REPOSITORY and file:/// where erp5_base + # exists. + + # Test Not exists + self.assertEquals(getBusinessTemplateUrl(url_list, not_exist_bt5), None) + self.assertEquals(getBusinessTemplateUrl(url_list[1:], not_exist_bt5), None) + self.assertEquals(getBusinessTemplateUrl(url_list[2:], not_exist_bt5), None) + self.assertEquals(getBusinessTemplateUrl(url_list[3:], not_exist_bt5), None) + self.assertEquals(getBusinessTemplateUrl(url_list[4:], not_exist_bt5), None) + def test_type_provider(self): self.portal._setObject('dummy_type_provider', DummyTypeProvider()) type_provider = self.portal.dummy_type_provider