Back to index

plone3  3.1.7
utils.py
Go to the documentation of this file.
00001 #  ATContentTypes http://plone.org/products/atcontenttypes/
00002 #  Archetypes reimplementation of the CMF core types
00003 #  Copyright (c) 2003-2006 AT Content Types development team
00004 #
00005 #  This program is free software; you can redistribute it and/or modify
00006 #  it under the terms of the GNU General Public License as published by
00007 #  the Free Software Foundation; either version 2 of the License, or
00008 #  (at your option) any later version.
00009 #
00010 #  This program is distributed in the hope that it will be useful,
00011 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 #  GNU General Public License for more details.
00014 #
00015 #  You should have received a copy of the GNU General Public License
00016 #  along with this program; if not, write to the Free Software
00017 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 #
00019 from AccessControl import ClassSecurityInfo
00020 from Globals import InitializeClass, package_home
00021 from UserDict import UserDict
00022 import ExtensionClass
00023 from Acquisition import Implicit
00024 from ZPublisher.BeforeTraverse import registerBeforeTraverse
00025 from Persistence import Persistent
00026 import os
00027 
00028 PACKAGE_HOME = package_home(globals())
00029 
00030 class FakeRequestSession(ExtensionClass.Base, UserDict):
00031     """Dummy dict like object with set method for SESSION and REQUEST
00032     """
00033     security = ClassSecurityInfo()
00034     security.setDefaultAccess('allow')
00035     security.declareObjectPublic()
00036     
00037     def __init__(self):
00038         UserDict.__init__(self)
00039         # add a dummy because request mustn't be empty for test
00040         # like 'if REQUEST:'
00041         self['__dummy__'] = None
00042     
00043     def __nonzero__(self):
00044         return True
00045     
00046     def set(self, key, value):
00047         self[key] = value
00048 
00049 InitializeClass(FakeRequestSession)
00050 FakeRequestSession()
00051 
00052 class DummySessionDataManager(Implicit):
00053     """Dummy sdm for sessioning
00054     
00055     Uses traversal hooks to add the SESSION object as lazy item to the request
00056     """
00057     security = ClassSecurityInfo()
00058     security.setDefaultAccess('allow')
00059     security.declareObjectPublic()
00060     
00061     id = 'session_data_manager'
00062     
00063     def __init__(self):
00064         self.session = FakeRequestSession()
00065         
00066     def manage_afterAdd(self, item, container):
00067         """Register traversal hooks to add SESSION to request
00068         """
00069         parent = self.aq_inner.aq_parent
00070         hook = DummySDMTraverseHook()
00071         registerBeforeTraverse(parent, hook, 'SessionDataManager', 50)
00072 
00073     def getSessionData(self, create=1):
00074         """ """
00075         return self.session
00076     
00077     def hasSessionData(self):
00078         """ """
00079         return True
00080     
00081     def getSessionDataByKey(self, key):
00082         """ """
00083         return self.session
00084     
00085     def getBrowserIdManager(self):
00086         """ """
00087         # dummy
00088         return self
00089 
00090 InitializeClass(DummySessionDataManager)
00091 
00092 class DummySDMTraverseHook(Persistent):
00093     """Traversal hook for dummy sessions
00094     
00095     See Products.Sessions.SessionDataManager.SessionDataManagerTraverser
00096     """
00097     
00098     def __call__(self, container, request):
00099         id = DummySessionDataManager.id
00100         sdm = getattr(container, id)
00101         getSessionData = sdm.getSessionData
00102         request.set_lazy('SESSION', getSessionData)
00103 
00104 def Xprint(s):
00105     """print helper
00106 
00107     print data via print is not possible, you have to use
00108     ZopeTestCase._print or this function
00109     """
00110     ZopeTestCase._print(str(s)+'\n')
00111 
00112 from DateTime import DateTime
00113 def dcEdit(obj):
00114     """dublin core edit (inplace)
00115     """
00116     obj.setTitle('Test title')
00117     obj.setDescription('Test description')
00118     obj.setSubject('Test subject')
00119     obj.setContributors(('test user a',))
00120     obj.setEffectiveDate(DateTime() -1)
00121     obj.setExpirationDate(DateTime() +2)
00122     obj.setFormat('text/structured')
00123     obj.setLanguage('de')
00124     obj.setRights('GPL')
00125     
00126 from Products.validation import ValidationChain
00127 EmptyValidator = ValidationChain('isEmpty')
00128 EmptyValidator.appendSufficient('isEmpty')
00129 idValidator = ValidationChain('isValidId')
00130 idValidator.appendSufficient('isEmptyNoError')
00131 idValidator.appendRequired('isValidId')
00132 TidyHTMLValidator = ValidationChain('isTidyHtmlChain')
00133 TidyHTMLValidator.appendRequired('isTidyHtmlWithCleanup')
00134 NotRequiredTidyHTMLValidator = ValidationChain('isTidyHtmlNotRequiredChain')
00135 NotRequiredTidyHTMLValidator.appendSufficient('isEmptyNoError')
00136 NotRequiredTidyHTMLValidator.appendRequired('isTidyHtmlWithCleanup')
00137 URLValidator = ValidationChain('isURL')
00138 URLValidator.appendSufficient('isEmptyNoError')
00139 URLValidator.appendRequired('isURL')
00140 EmailValidator = ValidationChain('isEmailChain')
00141 EmailValidator.appendSufficient('isEmptyNoError')
00142 EmailValidator.appendSufficient('isMailto')
00143 EmailValidator.appendRequired('isEmail')
00144 EmailValidator = ValidationChain('isEmailChain')
00145 EmailValidator.appendSufficient('isEmptyNoError')
00146 EmailValidator.appendRequired('isEmail')
00147 PhoneValidator = ValidationChain('isPhoneChain')
00148 PhoneValidator.appendSufficient('isEmptyNoError')
00149 PhoneValidator.appendRequired('isInternationalPhoneNumber')
00150 
00151 PREFIX = os.path.abspath(os.path.dirname(__file__)) 
00152 
00153 def input_file_path(file): 
00154     return os.path.join(PREFIX, 'input', file)