custom scripts from file
This commit is contained in:
parent
2dd28afcb7
commit
2ce962ed7e
2 changed files with 11 additions and 20 deletions
|
|
@ -15,23 +15,6 @@ class DocType:
|
|||
if self.doc.script_type=="Server" and webnotes.session.user!="Administrator":
|
||||
webnotes.throw("Only Administrator is allowed to edit Server Script")
|
||||
|
||||
# fix indentation
|
||||
tabs = None
|
||||
for line in self.doc.script.split("\n"):
|
||||
if line.strip():
|
||||
for i, char in enumerate(line):
|
||||
if char=="\t":
|
||||
tabs = "\t"
|
||||
break
|
||||
if char!=" ":
|
||||
if i==0:
|
||||
webnotes.throw("Custom Script must be indented by one tab")
|
||||
tabs = " " * i
|
||||
break
|
||||
if tabs:
|
||||
break
|
||||
|
||||
self.doc.script = self.doc.script.replace(tabs, " ")
|
||||
if not self.doc.script.startswith("\n"):
|
||||
self.doc.script = "\n" + self.doc.script
|
||||
|
||||
|
|
@ -44,10 +27,18 @@ class DocType:
|
|||
webnotes.cache().delete_value("_server_script:" + self.doc.dt)
|
||||
|
||||
def get_custom_server_script(doctype):
|
||||
from webnotes.modules import scrub
|
||||
from webnotes import conf
|
||||
from webnotes.utils import get_site_path
|
||||
import os
|
||||
custom_script = webnotes.cache().get_value("_server_script:" + doctype)
|
||||
if not custom_script:
|
||||
custom_script = webnotes.conn.get_value("Custom Script", {"dt": doctype, "script_type":"Server"},
|
||||
"script") or ""
|
||||
script_path = get_site_path(conf.custom_scripts_path, scrub(doctype) + '.py')
|
||||
if os.path.exists(script_path):
|
||||
with open(script_path, 'r') as f:
|
||||
custom_script = f.read()
|
||||
else:
|
||||
custom_script = None
|
||||
webnotes.cache().set_value("_server_script:" + doctype, custom_script)
|
||||
|
||||
return custom_script
|
||||
|
|
|
|||
|
|
@ -81,7 +81,7 @@ def get_server_obj(doc, doclist = [], basedoctype = ''):
|
|||
if custom_script:
|
||||
global custom_class
|
||||
|
||||
exec custom_class + '\n' + custom_script.replace('\t',' ') in locals()
|
||||
exec custom_script in locals()
|
||||
|
||||
return CustomDocType(doc, doclist)
|
||||
else:
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue