1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 import sys, os
78 from string import split, join
79 from types import StringType
80 import re
81
82
83 from Globals import DevelopmentMode
84 from Products.PageTemplates.PageTemplate import PageTemplate
85 from App.config import getConfiguration
86 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
87 from Products.PageTemplates.Expressions import SecureModuleImporter
88 from OFS.SimpleItem import Item
89 from AccessControl import ClassSecurityInfo
90 from OFS.Cache import Cacheable
91 from Acquisition import Implicit
92 from AccessControl import getSecurityManager
93
94
95 from ORMMode import ORMMode, ORMModeException
96
99
100 -def manage_addORMFSPageTemplate(self, id, module_name, mode_function_name,
101 db_connection_name, db_charset,
102 file_path, path_origin,
103 session_on=False,
104 path_variables_on=False, path_variables="",
105 REQUEST=None):
106 """
107 Add an ORMFSPageTemplate Object to the current Zope instance
108 """
109 id = str(id)
110
111 module = str(module_name)
112 mode_function = str(mode_function_name)
113 db_connection = str(db_connection_name)
114 db_charset = str(db_charset)
115
116 file_path = str(file_path)
117 path_origin = str(path_origin)
118
119 if str(session_on) == "on":
120 session_on = True
121 else:
122 session_on = False
123
124 if str(path_variables_on == "on"):
125 path_variables_on = True
126 else:
127 path_variables_on = False
128
129
130 obj = ORMFSPageTemplate(id, module, mode_function,
131 db_connection, db_charset, session_on,
132 path_variables_on, path_variables,
133 file_path, path_origin)
134 self._setObject(id, obj)
135
136 if REQUEST is not None:
137 return self.manage_main(self, REQUEST)
138
139 manage_addORMFSPageTemplateForm = PageTemplateFile("www/ORMFSPageTemplate.pt",
140 globals())
141
142 -class ORMFSPageTemplate(Implicit, ORMMode, PageTemplate):
143 """
144 This object type combines ORMMode and Zope's PageTemplates. The
145 mode functions's return falue is made available within the page
146 template as rt ('return value') and can be used in any tal
147 expression.
148 """
149 security=ClassSecurityInfo()
150
151 meta_type = "ORM Filesystem Page Template"
152
153 manage_main = PageTemplateFile("www/ORMFSPageTemplate.pt", globals())
154
155 manage_options = (
156 (
157 {"label": "Edit", "action": "manage_main"},
158 {"label": "Security", "action": "manage_access"}
159 ) + Item.manage_options + Cacheable.manage_options)
160
161
162 __ac_permissions__ = (
163 ('View management screens', ('manage_main',)),
164 ('Change Page Templates', ('manage_edit',)),
165 ('View', ('__call__','')),
166 )
167
168
169 security.declarePrivate('pt_edit', 'write')
170
171 - def __init__(self, id, module_name, mode_function_name,
172 db_connection_name, db_charset, session_on,
173 path_variables_on, path_variables,
174 file_path, path_origin, REQUEST=None):
180
181 - def manage_edit(self, module_name, mode_function_name,
182 db_connection_name, db_charset,
183 file_path, path_origin, session_on=False,
184 path_variables_on=False, path_variables="",
185 REQUEST=None):
186 """
187 Edit the current object.
188 """
189 ORMMode.manage_edit(self, module_name, mode_function_name,
190 db_connection_name, db_charset, session_on,
191 path_variables_on, path_variables,
192 None)
193
194 self._file_path = file_path
195
196 if path_origin not in ("root", "module", "instance"):
197 raise InvalidPathOrigin(path_origin)
198
199 self._path_origin = path_origin
200
201
202
203 self.write(self.source())
204
205 if REQUEST is not None:
206 message="ORMFSPageTemplate updated."
207 return self.manage_main(self, REQUEST, manage_tabs_message=message)
208
209 - def file_path(self):
210 "Accessor."
211 return self._file_path
212
213 - def path_origin(self):
214 "Accessor."
215 return self._path_origin
216
217 - def base_dir(self):
218 """
219 Return the beginning of the source file path determined by
220 the path_origin (including a trailing path seperator).
221
222 If path_origin is 'root' it returns '', because the file_path
223 is supposed to be absolute.
224
225 If the file_path() is absolute already it will return '',
226 too.
227 """
228 if os.path.isabs(self.file_path()):
229 return ""
230 elif self.path_origin() == "root":
231 return os.path.sep
232 elif self.path_origin() == "module":
233 tpl = os.path.split(self.module().__file__)
234 module_path = tpl[0]
235
236 return module_path + os.path.sep
237 elif self.path_origin() == "instance":
238 cfg = getConfiguration()
239 if cfg.instancehome[-1] == os.path.sep:
240 return cfg.instancehome
241 else:
242 return cfg.instancehome + os.path.sep
243
244
246 """
247 Return the contents of the file pointed to by file_path and
248 path_origin
249 """
250 path = self.base_dir() + self.file_path()
251 f = open(path)
252 return f.read()
253
254 - def __call__(self):
255 if DevelopmentMode:
256 self.write(self.source())
257
258 result = self.ZCacheable_get(default=None)
259 if result is None:
260 result = self.pt_render()
261 self.ZCacheable_set(data=result)
262
263 return result
264
265 - def pt_getContext(self):
266 security=getSecurityManager()
267
268
269 root = self.getPhysicalRoot()
270 user = security.getUser()
271 c = {'template': self,
272 'here': self,
273 'context': self,
274 'container': self.aq_parent,
275 'nothing': None,
276 'options': {},
277 'root': root,
278 'request': getattr(root, 'REQUEST', None),
279 'modules': SecureModuleImporter,
280 'parent': self.aq_parent,
281 'inner': self.aq_inner,
282 'container': self.aq_parent,
283 'user': user,
284
285
286 'rv': self.om_exec(user=user)
287 }
288 return c
289