vendor/CMF/1.6.3/CMFCore

view FSDTMLMethod.py @ 0:587011552858

import CMF 1.6.3
author bdelbosc
date Mon, 23 Apr 2007 13:58:01 +0000
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
4 #
5 # This software is subject to the provisions of the Zope Public License,
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10 # FOR A PARTICULAR PURPOSE.
11 #
12 ##############################################################################
13 """ Customizable DTML methods that come from the filesystem.
15 $Id$
16 """
18 import Globals
19 from AccessControl import ClassSecurityInfo, getSecurityManager
20 from AccessControl.DTML import RestrictedDTML
21 from AccessControl.Role import RoleManager
22 from OFS.Cache import Cacheable
23 from OFS.DTMLMethod import DTMLMethod, decapitate, guess_content_type
25 from DirectoryView import registerFileExtension
26 from DirectoryView import registerMetaType
27 from FSObject import FSObject
28 from permissions import FTPAccess
29 from permissions import View
30 from permissions import ViewManagementScreens
31 from utils import _dtmldir
32 from utils import _setCacheHeaders, _checkConditionalGET
33 from utils import expandpath
36 _marker = [] # Create a new marker object.
39 class FSDTMLMethod(RestrictedDTML, RoleManager, FSObject, Globals.HTML):
40 """FSDTMLMethods act like DTML methods but are not directly
41 modifiable from the management interface."""
43 meta_type = 'Filesystem DTML Method'
44 _owner = None
46 manage_options=(
47 (
48 {'label':'Customize', 'action':'manage_main'},
49 {'label':'View', 'action':'',
50 'help':('OFSP','DTML-DocumentOrMethod_View.stx')},
51 {'label':'Proxy', 'action':'manage_proxyForm',
52 'help':('OFSP','DTML-DocumentOrMethod_Proxy.stx')},
53 )
54 +Cacheable.manage_options
55 )
57 _proxy_roles=()
58 _cache_namespace_keys=()
60 # Use declarative security
61 security = ClassSecurityInfo()
62 security.declareObjectProtected(View)
64 security.declareProtected(ViewManagementScreens, 'manage_main')
65 manage_main = Globals.DTMLFile('custdtml', _dtmldir)
67 _reading = 0
69 def __init__(self, id, filepath, fullname=None, properties=None):
70 FSObject.__init__(self, id, filepath, fullname, properties)
71 # Normally called via HTML.__init__ but we don't need the rest that
72 # happens there.
73 self.initvars(None, {})
75 def _createZODBClone(self):
76 """Create a ZODB (editable) equivalent of this object."""
77 return DTMLMethod(self.read(), __name__=self.getId())
79 def _readFile(self, reparse):
80 fp = expandpath(self._filepath)
81 file = open(fp, 'r') # not 'rb', as this is a text file!
82 try:
83 data = file.read()
84 finally:
85 file.close()
86 self.raw = data
87 if reparse:
88 self._reading = 1 # Avoid infinite recursion
89 try:
90 self.cook()
91 finally:
92 self._reading = 0
94 # Hook up chances to reload in debug mode
95 security.declarePrivate('read_raw')
96 def read_raw(self):
97 if not self._reading:
98 self._updateFromFS()
99 return Globals.HTML.read_raw(self)
101 #### The following is mainly taken from OFS/DTMLMethod.py ###
103 index_html=None # Prevent accidental acquisition
105 # Documents masquerade as functions:
106 func_code = DTMLMethod.func_code
108 default_content_type = 'text/html'
110 def __call__(self, client=None, REQUEST={}, RESPONSE=None, **kw):
111 """Render the document given a client object, REQUEST mapping,
112 Response, and key word arguments."""
114 self._updateFromFS()
116 kw['document_id'] =self.getId()
117 kw['document_title']=self.title
119 if client is not None:
120 if _checkConditionalGET(self, kw):
121 return ''
123 if not self._cache_namespace_keys:
124 data = self.ZCacheable_get(default=_marker)
125 if data is not _marker:
126 # Return cached results.
127 return data
129 __traceback_info__ = self._filepath
130 security=getSecurityManager()
131 security.addContext(self)
132 try:
133 r = Globals.HTML.__call__(self, client, REQUEST, **kw)
135 if client is None:
136 # Called as subtemplate, so don't need error propagation!
137 if RESPONSE is None: result = r
138 else: result = decapitate(r, RESPONSE)
139 if not self._cache_namespace_keys:
140 self.ZCacheable_set(result)
141 return result
143 if not isinstance(r, basestring) or RESPONSE is None:
144 if not self._cache_namespace_keys:
145 self.ZCacheable_set(r)
146 return r
148 finally: security.removeContext(self)
150 have_key=RESPONSE.headers.has_key
151 if not (have_key('content-type') or have_key('Content-Type')):
152 if self.__dict__.has_key('content_type'):
153 c=self.content_type
154 else:
155 c, e=guess_content_type(self.getId(), r)
156 RESPONSE.setHeader('Content-Type', c)
157 if RESPONSE is not None:
158 # caching policy manager hook
159 _setCacheHeaders(self, {})
160 result = decapitate(r, RESPONSE)
161 if not self._cache_namespace_keys:
162 self.ZCacheable_set(result)
163 return result
165 def getCacheNamespaceKeys(self):
166 '''
167 Returns the cacheNamespaceKeys.
168 '''
169 return self._cache_namespace_keys
171 def setCacheNamespaceKeys(self, keys, REQUEST=None):
172 '''
173 Sets the list of names that should be looked up in the
174 namespace to provide a cache key.
175 '''
176 ks = []
177 for key in keys:
178 key = strip(str(key))
179 if key:
180 ks.append(key)
181 self._cache_namespace_keys = tuple(ks)
182 if REQUEST is not None:
183 return self.ZCacheable_manage(self, REQUEST)
185 # Zope 2.3.x way:
186 def validate(self, inst, parent, name, value, md=None):
187 return getSecurityManager().validate(inst, parent, name, value)
189 security.declareProtected(FTPAccess, 'manage_FTPget')
190 manage_FTPget = DTMLMethod.manage_FTPget.im_func
192 security.declareProtected(ViewManagementScreens, 'PrincipiaSearchSource')
193 PrincipiaSearchSource = DTMLMethod.PrincipiaSearchSource.im_func
195 security.declareProtected(ViewManagementScreens, 'document_src')
196 document_src = DTMLMethod.document_src.im_func
198 security.declareProtected(ViewManagementScreens, 'manage_haveProxy')
199 manage_haveProxy = DTMLMethod.manage_haveProxy.im_func
201 Globals.InitializeClass(FSDTMLMethod)
203 registerFileExtension('dtml', FSDTMLMethod)
204 registerMetaType('DTML Method', FSDTMLMethod)