Back to index

plone3  3.1.7
StorageMigrationSupport.py
Go to the documentation of this file.
00001 #########################################################################
00002 # Copyright (c) 2006 Gregoire Weber
00003 # All Rights Reserved.
00004 #
00005 # This file is part of CMFEditions.
00006 #
00007 # CMFEditions is free software; you can redistribute it and/or modify
00008 # it under the terms of the GNU General Public License as published by
00009 # the Free Software Foundation; either version 2 of the License, or
00010 # (at your option) any later version.
00011 #
00012 # CMFEditions is distributed in the hope that it will be useful,
00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 # GNU General Public License for more details.
00016 #
00017 # You should have received a copy of the GNU General Public License
00018 # along with CMFEditions; if not, write to the Free Software
00019 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00020 #########################################################################
00021 """Storage Migration Support
00022 
00023 Creating a test hierarchy for migration tests.
00024 
00025 $Id$
00026 """
00027 import logging
00028 import os.path
00029 import time
00030 from Products.CMFCore.utils import getToolByName
00031 from Products.CMFEditions import PACKAGE_HOME
00032 
00033 logger = logging.getLogger('CMFEditions')
00034 
00035 def create(context, type, name):
00036     context.invokeFactory(type, name)
00037     obj = getattr(context, name)
00038     editMethods[type](obj, version=0)
00039     return obj
00040 
00041 def edit(obj, version):
00042     type = obj.getPortalTypeName()
00043     editMethods[type](obj, version)
00044 
00045 def editEvent(context, version=0):
00046     title = context.Title()
00047     desc = context.Description()
00048     eventType = context.Subject()
00049     location = context.location
00050     contact = context.contactName
00051     name = context.getId()
00052     if not title:
00053         title = "0: %s event title" % name
00054         desc = "0: %s event description" % name
00055         eventType = "Appointment"
00056         location = "0: %s event location" % name
00057         contact = "0: %s event contact" % name
00058     else:
00059         title = "%s%s" % (version, title[1:])
00060         desc = "%s%s" % (version, desc[1:])
00061         location = "%s%s" % (version, location[1:])
00062         contact = "%s%s" % (version, contact[1:])
00063     
00064     context.update(title=title, description=desc, 
00065                    eventType=eventType, location=location,
00066                    contactName=contact)
00067 
00068 def editFile(context, version=0):
00069     title = context.Title()
00070     desc = context.Description()
00071     file = context.index_html()
00072     name = context.getId()
00073     if not title:
00074         title = "0: %s file title" % name
00075         desc = "0: %s file description" % name
00076         file = 100 * ("0: %s file body\n" % name)
00077     else:
00078         title = "%s%s" % (version, title[1:])
00079         desc = "%s%s" % (version, desc[1:])
00080         file = 100 * ("%s%s" % (version, ": %s file body\n" % name))
00081     context.update(title=title, description=desc, file=file)
00082 
00083 def editFolder(context, version=0):
00084     title = context.Title()
00085     desc = context.Description()
00086     name = context.getId()
00087     if not title:
00088         title = "0: %s folder title" % name
00089         desc = "0: %s folder description" % name
00090     title = "%s%s" % (version, title[1:])
00091     desc = "%s%s" % (version, desc[1:])
00092     context.folder_edit(title=title, description=desc)
00093 
00094 def editImage(context, version=0):
00095     title = context.Title()
00096     desc = context.Description()
00097     image = context.index_html()
00098     name = context.getId()
00099     if name.endswith(".gif"):
00100         name = name[:-4]
00101     filename = "%s_v%s.gif" % (name, version)
00102     path = os.path.join(PACKAGE_HOME, "tests", "images", filename)
00103     if not title:
00104         title = "0: %s image title" % name
00105         desc = "0: %s image description" % name
00106         image = open(path).read()
00107     else:
00108         title = "%s%s" % (version, title[1:])
00109         desc = "%s%s" % (version, desc[1:])
00110         image = open(path).read()
00111     context.update(title=title, description=desc, image=image)
00112 
00113 def editLink(context, version=0):
00114     title = context.Title()
00115     desc = context.Description()
00116     remoteUrl = context.remoteUrl
00117     name = context.getId()
00118     if not title:
00119         title = "0: %s link title" % name
00120         desc = "0: %s link description" % name
00121         remoteUrl = "http://www.plone.org/#%s_v0" % name
00122     else:
00123         title = "%s%s" % (version, title[1:])
00124         desc = "%s%s" % (version, desc[1:])
00125         remoteUrl = "%s%s" % (remoteUrl[:-1], version)
00126     context.update(title=title, description=desc, remoteUrl=remoteUrl)
00127 
00128 def editNewsItem(context, version=0):
00129     title = context.Title()
00130     desc = context.Description()
00131     text = context.getText()
00132     name = context.getId()
00133     if not title:
00134         title = "0: %s news item title" % name
00135         desc = "0: %s news item description" % name
00136         text = "0: %s news item body" % name
00137     else:
00138         title = "%s%s" % (version, title[1:])
00139         desc = "%s%s" % (version, desc[1:])
00140         text = "%s%s" % (version, text[1:])
00141     context.update(title=title, description=desc, text=text)
00142 
00143 def editDocument(context, version=0):
00144     title = context.Title()
00145     desc = context.Description()
00146     text = context.getText()
00147     name = context.getId()
00148     if not title:
00149         title = "0: %s document title" % name
00150         desc = "0: %s document description" % name
00151         text = "0: %s document body" % name
00152     else:
00153         title = "%s%s" % (version, title[1:])
00154         desc = "%s%s" % (version, desc[1:])
00155         text = "%s%s" % (version, text[1:])
00156     context.update(title=title, description=desc, text=text)
00157 
00158 
00159 def editTopic(context, version=0):
00160 #    import pdb; pdb.set_trace()
00161     pass
00162 
00163 editMethods = {
00164     "Event": editEvent,
00165     "File": editFile,
00166     "Folder": editFolder,
00167     "Image": editImage,
00168     "Link": editLink,
00169     "News Item": editNewsItem,
00170     "Document": editDocument,
00171     "Topic": editTopic,
00172 }
00173 
00174 hierarchy = {
00175     "events": ("Folder", "Event", 4, 3),
00176     "files": ("Folder", "File", 4, 3),
00177     "folders": ("Folder", "Folder", 3, 3),
00178     "images": ("Folder", "Image", 2, 4, ".gif"),
00179     "links": ("Folder", "Link", 4, 3),
00180     "newsitems": ("Folder", "News Item", 4, 3),
00181     "documents": ("Folder", "Document", 4, 3),
00182     "topics": ("Folder", "Topic", 0, 0),
00183 }
00184 
00185 def createTestHierarchy(context):
00186     startTime = time.time()
00187     repo = getToolByName(context, "portal_repository")
00188     testRoot = create(context,  "Folder", "CMFEditionsTestHierarchy")
00189     nbrOfObjects = 0
00190     nbrOfEdits = 0
00191     nbrOfSaves = 0
00192     for name, type in hierarchy.items():
00193         logger.log(logging.INFO, "createTestHierarchy: creating container %s(%s)" \
00194             % (name, type[0]))
00195         folder = create(testRoot, type[0], name)
00196         nbrOfObjects += 1
00197         logger.log(logging.INFO, "createTestHierarchy: save #0")
00198         repo.save(folder, comment="save #0")
00199         nbrOfSaves += 1
00200         for i in range(type[2]):
00201             if len(type) == 5:
00202                 ext = type[4]
00203             else:
00204                 ext = ""
00205                 
00206             # create and save
00207             objName = name[:-1]+str(i+1)+ext
00208             logger.log(logging.INFO, "createTestHierarchy: creating %s(%s)" \
00209                 % (objName, type[1]))
00210             obj = create(folder, type[1], objName)
00211             nbrOfObjects += 1
00212             logger.log(logging.INFO, "createTestHierarchy: save #0")
00213             repo.save(obj, comment="save #0")
00214             nbrOfSaves += 1
00215             
00216             # edit and save a number of times
00217             for j in range(1, type[3]):
00218                 logger.log(logging.INFO, "createTestHierarchy: editing")
00219                 edit(obj, j)
00220                 nbrOfEdits += 1
00221                 logger.log(logging.INFO, "createTestHierarchy: save #%s" % j)
00222                 repo.save(obj, comment="save #%s" % j)
00223                 nbrOfSaves += 1
00224                 
00225                 vers = j + i*(type[3]-1)
00226                 logger.log(logging.INFO, "createTestHierarchy: editing parent")
00227                 edit(folder, vers)
00228                 nbrOfEdits += 1
00229                 logger.log(logging.INFO, "createTestHierarchy: save parent #%s" % vers)
00230                 repo.save(folder, comment="save #%s" % vers)
00231                 nbrOfSaves += 1
00232     
00233     totalTime = time.time() - startTime
00234     logger.log(logging.INFO,
00235         "createTestHierarchy: created %s objects, edited them %s times and saved %s versions in total in %.1f seconds" \
00236         % (nbrOfObjects, nbrOfEdits, nbrOfSaves, round(totalTime, 1)))
00237     
00238     return testRoot