Package orm2 :: Package ui :: Package ORMMode :: Module ORMFSPageTemplate
[hide private]
[frames] | no frames]

Source Code for Module orm2.ui.ORMMode.ORMFSPageTemplate

  1  #!/usr/bin/env python 
  2  # -*- coding: iso-8859-1 -*- 
  3   
  4  ##  This file is part of orm, The Object Relational Membrane Version 2. 
  5  ## 
  6  ##  Copyright 2002-2006 by Diedrich Vorberg <diedrich@tux4web.de> 
  7  ## 
  8  ##  All Rights Reserved 
  9  ## 
 10  ##  For more Information on orm see the README file. 
 11  ## 
 12  ##  This program is free software; you can redistribute it and/or modify 
 13  ##  it under the terms of the GNU General Public License as published by 
 14  ##  the Free Software Foundation; either version 2 of the License, or 
 15  ##  (at your option) any later version. 
 16  ## 
 17  ##  This program is distributed in the hope that it will be useful, 
 18  ##  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 19  ##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 20  ##  GNU General Public License for more details. 
 21  ## 
 22  ##  You should have received a copy of the GNU General Public License 
 23  ##  along with this program; if not, write to the Free Software 
 24  ##  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 25  ## 
 26  ##  I have added a copy of the GPL in the file gpl.txt. 
 27   
 28  # Changelog (orm2) 
 29  # --------------- 
 30  # $Log: ORMFSPageTemplate.py,v $ 
 31  # Revision 1.1  2006/04/28 09:56:41  diedrich 
 32  # Initial commit 
 33  # 
 34  # 
 35   
 36  # Changelog (orm) 
 37  # --------------- 
 38  # 
 39  # $Log: ORMFSPageTemplate.py,v $ 
 40  # Revision 1.1  2006/04/28 09:56:41  diedrich 
 41  # Initial commit 
 42  # 
 43  # Revision 1.9  2004/10/02 11:37:49  t4w00-diedrich 
 44  # The user variable is not only available within the pt but also in the 
 45  # parameters dict. 
 46  # 
 47  # Revision 1.8  2004/08/30 11:06:47  t4w00-diedrich 
 48  # - added Security tab 
 49  # - added "Call ORM mode function" Zope permission 
 50  # 
 51  # Revision 1.7  2004/08/28 13:54:53  t4w00-diedrich 
 52  # Added processing of path variables. 
 53  # 
 54  # Revision 1.6  2004/08/18 10:21:43  t4w00-diedrich 
 55  # Provide a user variable within the PageTemplate like CMFFSPageTemplate 
 56  # does. This is required for Plone to work with us 
 57  # 
 58  # Revision 1.5  2004/08/03 16:33:38  t4w00-diedrich 
 59  # Changed to Implicit Acquisition 
 60  # 
 61  # Revision 1.4  2004/08/03 10:52:26  t4w00-diedrich 
 62  # Added db_charset parameter. 
 63  # 
 64  # Revision 1.3  2004/08/03 09:23:26  t4w00-diedrich 
 65  # Added caching support 
 66  # 
 67  # Revision 1.2  2004/08/03 07:39:52  t4w00-diedrich 
 68  # Actually use the PageTemplate interface. 
 69  # 
 70  # Revision 1.1  2004/08/02 23:04:43  t4w00-diedrich 
 71  # Initial commit 
 72  # 
 73  # 
 74  # 
 75   
 76  # Python 
 77  import sys, os 
 78  from string import split, join 
 79  from types import StringType 
 80  import re 
 81   
 82  # Zope 
 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  # ORMMode 
 95  from ORMMode import ORMMode, ORMModeException 
 96   
97 -class InvalidPathOrigin(ORMModeException): pass
98 -class InvalidFilePath(ORMModeException): pass
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 # The same permissions like External Methods 162 __ac_permissions__ = ( 163 ('View management screens', ('manage_main',)), 164 ('Change Page Templates', ('manage_edit',)), 165 ('View', ('__call__','')), 166 ) 167 168 # Declare security for unprotected PageTemplate methods. 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 # read the source file to see if it's there and pass the 202 # result to PageTemplate (write method) 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
245 - def source(self):
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 # this is from ZopePageTemplate, mostly. 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, # CMFCore.FSPageTemplate defines this. 284 # That's why it's needed for Plone to 285 # work. 286 'rv': self.om_exec(user=user) # also pass the user object to the 287 } # mode functin. Can't hurt :-) 288 return c
289