def compileFormulaScript(self, script_id, formula, with_args=False): # disable CSRF to allow script saving if hasattr(self, "REQUEST"): alsoProvides(self.REQUEST, IDisableCSRFProtection) # Remember the current user member = self.getCurrentMember() if member.__class__.__name__ == "SpecialUser": user = member else: user = member.getUser() # Switch to the db's owner (formula must be compiled with the higher # access rights, but their execution will always be perform with the # current access rights) owner = self.getOwner() newSecurityManager(None, owner) ps = self.getFormulaScript(script_id) if not ps: ps = PythonScript(script_id) self.scripts._setObject(script_id, ps) ps = self.getFormulaScript(script_id) if with_args: ps._params = "*args" safe_utils = get_utils() import_list = [] for module in safe_utils: import_list.append( "from %s import %s" % ( module, ", ".join(safe_utils[module])) ) import_list = ";".join(import_list) formula = _expandIncludes(self, formula) if (formula.strip().count('\n') == 0 and not formula.startswith('return ')): formula = "return " + formula str_formula = STR_FORMULA % { 'script_id': script_id, 'import_list': import_list, 'formula': formula } ps.write(str_formula) if self.debugMode: logger.info(script_id + " compiled") # Switch back to the original user newSecurityManager(None, user) return ps
def compileFormulaScript(self, script_id, formula, with_args=False): # disable CSRF to allow script saving if hasattr(self, "REQUEST"): alsoProvides(self.REQUEST, IDisableCSRFProtection) # Remember the current user member = self.getCurrentMember() if member.__class__.__name__ == "SpecialUser": user = member else: user = member.getUser() # Switch to the db's owner (formula must be compiled with the higher # access rights, but their execution will always be perform with the # current access rights) owner = self.getOwner() newSecurityManager(None, owner) ps = self.getFormulaScript(script_id) if not ps: ps = PythonScript(script_id) self.scripts._setObject(script_id, ps) ps = self.getFormulaScript(script_id) if with_args: ps._params = "*args" safe_utils = get_utils() import_list = [] for module in safe_utils: import_list.append("from %s import %s" % (module, ", ".join(safe_utils[module]))) import_list = ";".join(import_list) formula = _expandIncludes(self, formula) if (formula.strip().count('\n') == 0 and not formula.startswith('return ')): formula = "return " + formula str_formula = STR_FORMULA % { 'script_id': script_id, 'import_list': import_list, 'formula': formula } ps.write(str_formula) if self.debugMode: logger.info(script_id + " compiled") # Switch back to the original user newSecurityManager(None, user) return ps
def compileFormulaScript(self, script_id, formula, with_args=False): ps = self.getFormulaScript(script_id) if ps is None: ps = PythonScript(script_id) self.scripts._setObject(script_id, ps) ps = self.getFormulaScript(script_id) if with_args: ps._params = "*args" str_formula = "plominoContext = context\n" str_formula = str_formula + "plominoDocument = context\n" #str_formula=str_formula+"from Products.CMFPlomino.PlominoUtils import "+SAFE_UTILS+'\n' safe_utils = get_utils() import_list = [] for module in safe_utils: import_list.append("from %s import %s" % (module, ", ".join(safe_utils[module]))) str_formula = str_formula + ";".join(import_list) + '\n' r = re.compile('#Plomino import (.+)[\r\n]') for i in r.findall(formula): scriptname = i.strip() try: script_code = self.resources._getOb(scriptname).read() except: script_code = "#ALERT: " + scriptname + " not found in resources" formula = formula.replace('#Plomino import ' + scriptname, script_code) if formula.strip().count('\n') > 0: str_formula = str_formula + formula else: if formula.startswith('return '): str_formula = str_formula + formula else: str_formula = str_formula + "return " + formula ps.write(str_formula) if self.debugMode: logger.info(script_id + " compiled") return ps
def read(self): ps = PythonScript(self.id) ps._body = self._body ps._params = self._params return ps.read()