Commit 508c090d authored by Rafael Monnerat's avatar Rafael Monnerat

Added updateBusinessTemplateFromUrl, this method allows you:

  - Define after script list, before script list to be executed before/after bt5 installation.
  - Define a list of keep_original, to avoid remove/add unwanted objects when install a business template.

This is usefull to automate user actions during a upgrade, It is not recommentd use it to hack and provide behaviours that should be part of default business template installations. after/before script should be used only by projects that requires specific procedures.



git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@35694 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 5697a60d
...@@ -40,6 +40,7 @@ from Products.ERP5Type.DiffUtils import DiffFile ...@@ -40,6 +40,7 @@ from Products.ERP5Type.DiffUtils import DiffFile
from Products.ERP5Type.Tool.BaseTool import BaseTool from Products.ERP5Type.Tool.BaseTool import BaseTool
from Products.ERP5Type import Permissions, tarfile from Products.ERP5Type import Permissions, tarfile
from Products.ERP5.Document.BusinessTemplate import BusinessTemplateMissingDependency from Products.ERP5.Document.BusinessTemplate import BusinessTemplateMissingDependency
from Acquisition import aq_base
from tempfile import mkstemp, mkdtemp from tempfile import mkstemp, mkdtemp
from Products.ERP5 import _dtmldir from Products.ERP5 import _dtmldir
from cStringIO import StringIO from cStringIO import StringIO
...@@ -135,15 +136,28 @@ class TemplateTool (BaseTool): ...@@ -135,15 +136,28 @@ class TemplateTool (BaseTool):
DeprecationWarning('getInstalledBusinessTemplatesList is deprecated; Use getInstalledBusinessTemplateList instead.', DeprecationWarning) DeprecationWarning('getInstalledBusinessTemplatesList is deprecated; Use getInstalledBusinessTemplateList instead.', DeprecationWarning)
return self.getInstalledBusinessTemplateList() return self.getInstalledBusinessTemplateList()
def getInstalledBusinessTemplateList(self): def _getInstalledBusinessTemplateList(self, only_title=0):
"""Get the list of installed business templates. """Get the list of installed business templates.
""" """
installed_bts = [] installed_bts = []
for bt in self.contentValues(portal_type='Business Template'): for bt in self.contentValues(portal_type='Business Template'):
if bt.getInstallationState() == 'installed': if bt.getInstallationState() == 'installed':
installed_bts.append(bt) bt5 = bt
if only_title:
bt5 = bt.getTitle()
installed_bts.append(bt5)
return installed_bts return installed_bts
def getInstalledBusinessTemplateList(self):
"""Get the list of installed business templates.
"""
return self._getInstalledBusinessTemplateList(only_title=0)
def getInstalledBusinessTemplateTitleList(self):
"""Get the list of installed business templates.
"""
return self._getInstalledBusinessTemplateList(only_title=1)
def getInstalledBusinessTemplateRevision(self, title, **kw): def getInstalledBusinessTemplateRevision(self, title, **kw):
""" """
Return the revision of business template installed with the title Return the revision of business template installed with the title
...@@ -1052,6 +1066,42 @@ class TemplateTool (BaseTool): ...@@ -1052,6 +1066,42 @@ class TemplateTool (BaseTool):
opreation_log.append('Not found in repositories %s' % template_name) opreation_log.append('Not found in repositories %s' % template_name)
return opreation_log return opreation_log
security.declareProtected(Permissions.ManagePortal,
'updateBusinessTemplateFromUrl')
def updateBusinessTemplateFromUrl(self, download_url, id=None,
keep_original_list=[],
before_triggered_bt5_id_list=[],
after_triggered_bt5_id_list=[],
update_catalog=0):
"""
This method download and install a bt5, from a URL.
"""
imported_bt5 = self.download(url = download_url, id = id)
BusinessTemplate_getModifiedObject = \
aq_base(getattr(self, 'BusinessTemplate_getModifiedObject'))
install_kw = {}
listbox_object_list = BusinessTemplate_getModifiedObject.__of__(imported_bt5)()
for listbox_line in listbox_object_list:
# if a bt5 item is removed we may still want to keep it
if (listbox_line.object_state in ('Removed', 'Removed but used', 'Modified', 'New') and
listbox_line.object_id in keep_original_list):
install_kw[listbox_line.object_id] = 'nothing' #Keep original
else:
install_kw[listbox_line.object_id] = listbox_line.choice_item_list[0][1]
# Run before script list
for before_triggered_bt5_id in before_triggered_bt5_id_list:
getattr(imported_bt5, before_triggered_bt5_id)()
imported_bt5.install(object_to_update=install_kw,
update_catalog=update_catalog)
# Run After script list
for after_triggered_bt5_id in after_triggered_bt5_id_list:
getattr(imported_bt5, after_triggered_bt5_id)()
LOG('ERP5', INFO, "Updated %s from %s" %(imported_bt5.getTitle(), download_url))
security.declareProtected(Permissions.ManagePortal, security.declareProtected(Permissions.ManagePortal,
'getBusinessTemplateUrl') 'getBusinessTemplateUrl')
def getBusinessTemplateUrl(self, base_url_list, bt5_title): def getBusinessTemplateUrl(self, base_url_list, bt5_title):
......
...@@ -5275,6 +5275,33 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor): ...@@ -5275,6 +5275,33 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
self.assertNotEquals(None, self.getPortal()\ self.assertNotEquals(None, self.getPortal()\
.portal_templates.getInstalledBusinessTemplate('erp5_core')) .portal_templates.getInstalledBusinessTemplate('erp5_core'))
def test_getInstalledBusinessTemplateList(self):
templates_tool = self.getPortal().portal_templates
bt5_list = templates_tool.getInstalledBusinessTemplateList()
another_bt_list = [ i for i in templates_tool.contentValues() \
if i.getInstallationState() == 'installed']
self.assertEquals(len(bt5_list), len(another_bt_list))
for bt in bt5_list:
self.failUnless(bt in another_bt_list)
self.assertEquals(bt5_list,
templates_tool._getInstalledBusinessTemplateList())
def test_getInstalledBusinessTemplateTitleList(self):
templates_tool = self.getPortal().portal_templates
bt5_list = templates_tool.getInstalledBusinessTemplateTitleList()
another_bt_list = [ i.getTitle() for i in templates_tool.contentValues() \
if i.getInstallationState() == 'installed']
bt5_list.sort()
another_bt_list.sort()
self.assertEquals(bt5_list, another_bt_list)
for bt in bt5_list:
self.failUnless(bt in another_bt_list)
new_list = templates_tool._getInstalledBusinessTemplateList(only_title=1)
new_list.sort()
self.assertEquals(bt5_list, new_list)
def test_CompareVersions(self): def test_CompareVersions(self):
"""Tests compare version on template tool. """ """Tests compare version on template tool. """
compareVersions = self.getPortal().portal_templates.compareVersions compareVersions = self.getPortal().portal_templates.compareVersions
...@@ -5331,6 +5358,81 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor): ...@@ -5331,6 +5358,81 @@ class TestBusinessTemplate(ERP5TypeTestCase, LogInterceptor):
self.assertEquals(test_web.getTitle(), 'test_web') self.assertEquals(test_web.getTitle(), 'test_web')
self.assertTrue(test_web.getRevision()) self.assertTrue(test_web.getRevision())
def test_updateBusinessTemplateFromUrl_simple(self):
"""
Test updateBusinessTemplateFromUrl method
"""
template_tool = self.portal.portal_templates
old_bt = template_tool.getInstalledBusinessTemplate('erp5_csv_style')
url = 'https://svn.erp5.org/repos/public/erp5/trunk/bt5/erp5_csv_style'
template_tool.updateBusinessTemplateFromUrl(url)
new_bt = template_tool.getInstalledBusinessTemplate('erp5_csv_style')
self.assertNotEquals(old_bt, new_bt)
self.assertEquals('erp5_csv_style', new_bt.getTitle())
old_bt = new_bt
template_tool.updateBusinessTemplateFromUrl(url, id="new_erp5_csv_style")
new_bt = template_tool.getInstalledBusinessTemplate('erp5_csv_style')
self.assertNotEquals(old_bt, new_bt)
self.assertEquals('erp5_csv_style', new_bt.getTitle())
self.assertEquals('new_erp5_csv_style', new_bt.getId())
def test_updateBusinessTemplateFromUrl_keep_list(self):
"""
Test updateBusinessTemplateFromUrl method
"""
template_tool = self.portal.portal_templates
url = 'https://svn.erp5.org/repos/public/erp5/trunk/bt5/test_core'
# don't install test_file
keep_original_list = ( 'portal_skins/erp5_test/test_file', )
template_tool.updateBusinessTemplateFromUrl(url,
keep_original_list=keep_original_list)
bt = template_tool.getInstalledBusinessTemplate('test_core')
self.assertNotEquals(None, bt)
erp5_test = getattr(self.portal.portal_skins, 'erp5_test', None)
self.assertNotEquals(None, erp5_test)
test_file = getattr(erp5_test, 'test_file', None)
self.assertEquals(None, test_file)
def test_updateBusinessTemplateFromUrl_after_before_script(self):
"""
Test updateBusinessTemplateFromUrl method
"""
from Products.ERP5Type.tests.utils import createZODBPythonScript
portal = self.getPortal()
createZODBPythonScript(portal.portal_skins.custom,
'BT_dummyA',
'scripts_params=None',
'# Script body\n'
'return context.setDescription("MODIFIED")')
createZODBPythonScript(portal.portal_skins.custom,
'BT_dummyB',
'scripts_params=None',
'# Script body\n'
'return context.setChangeLog("MODIFIED")')
createZODBPythonScript(portal.portal_skins.custom,
'BT_dummyC',
'scripts_params=None',
'# Script body\n'
'return context.getPortalObject().setTitle("MODIFIED")')
template_tool = self.portal.portal_templates
url = 'https://svn.erp5.org/repos/public/erp5/trunk/bt5/test_html_style'
# don't install test_file
before_triggered_bt5_id_list = ['BT_dummyA', 'BT_dummyB']
after_triggered_bt5_id_list = ['BT_dummyC']
template_tool.updateBusinessTemplateFromUrl(url,
before_triggered_bt5_id_list=before_triggered_bt5_id_list,
after_triggered_bt5_id_list=after_triggered_bt5_id_list)
bt = template_tool.getInstalledBusinessTemplate('test_html_style')
self.assertNotEquals(None, bt)
self.assertEquals(bt.getDescription(), 'MODIFIED')
self.assertEquals(bt.getChangeLog(), 'MODIFIED')
self.assertEquals(portal.getTitle(), 'MODIFIED')
def stepCreateCustomWorkflow(self, sequence=None, sequence_list=None, **kw): def stepCreateCustomWorkflow(self, sequence=None, sequence_list=None, **kw):
""" """
Create a custom workflow Create a custom workflow
......
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