您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

155 行
3.6 KiB

  1. """
  2. globals attached to webnotes module
  3. + some utility functions that should probably be moved
  4. """
  5. code_fields_dict = {
  6. 'Page':[('script', 'js'), ('content', 'html'), ('style', 'css'), ('static_content', 'html'), ('server_code', 'py')],
  7. 'DocType':[('server_code_core', 'py'), ('client_script_core', 'js')],
  8. 'Search Criteria':[('report_script', 'js'), ('server_script', 'py'), ('custom_query', 'sql')],
  9. 'Patch':[('patch_code', 'py')],
  10. 'Stylesheet':['stylesheet', 'css'],
  11. 'Page Template':['template', 'html'],
  12. 'Control Panel':[('startup_code', 'js'), ('startup_css', 'css')]
  13. }
  14. version = 'v170'
  15. form_dict = {}
  16. auth_obj = None
  17. conn = None
  18. form = None
  19. session = None
  20. user = None
  21. is_testing = None
  22. incoming_cookies = {}
  23. add_cookies = {} # append these to outgoing request
  24. cookies = {}
  25. auto_masters = {}
  26. tenant_id = None
  27. response = {'message':'', 'exc':''}
  28. debug_log = []
  29. message_log = []
  30. class ValidationError(Exception):
  31. pass
  32. class AuthenticationError(Exception):
  33. pass
  34. def getTraceback():
  35. import utils
  36. return utils.getTraceback()
  37. def errprint(msg):
  38. """
  39. Append to the :data:`debug log`
  40. """
  41. from utils import cstr
  42. debug_log.append(cstr(msg or ''))
  43. def msgprint(msg, small=0, raise_exception=0, as_table=False):
  44. """
  45. Append to the :data:`message_log`
  46. """
  47. from utils import cstr
  48. if as_table and type(msg) in (list, tuple):
  49. msg = '<table border="1px" style="border-collapse: collapse" cellpadding="2px">' + ''.join(['<tr>'+''.join(['<td>%s</td>' % c for c in r])+'</tr>' for r in msg]) + '</table>'
  50. message_log.append((small and '__small:' or '')+cstr(msg or ''))
  51. if raise_exception:
  52. raise ValidationError
  53. def is_apache_user():
  54. import os
  55. if os.environ.get('USER') == 'apache':
  56. return True
  57. else:
  58. return (not os.environ.get('USER'))
  59. # os.environ does not have user, so allows a security vulnerability,fixed now.
  60. def get_index_path():
  61. import os
  62. return os.sep.join(os.path.dirname(os.path.abspath(__file__)).split(os.sep)[:-2])
  63. def get_files_path():
  64. import defs, os
  65. if not conn:
  66. raise Exception, 'You must login first'
  67. if defs.files_path:
  68. return os.path.join(defs.files_path, conn.cur_db_name)
  69. else:
  70. return os.path.join(get_index_path(), 'user_files', conn.cur_db_name)
  71. def create_folder(path):
  72. """
  73. Wrapper function for os.makedirs (does not throw exception if directory exists)
  74. """
  75. import os
  76. try:
  77. os.makedirs(path)
  78. except Exception, e:
  79. if e.args[0]==17:
  80. pass
  81. else:
  82. raise e
  83. def connect(db_name=None):
  84. """
  85. Connect to this db (or db), if called from command prompt
  86. """
  87. if is_apache_user():
  88. raise Exception, 'Not for web users!'
  89. import webnotes.db
  90. global conn
  91. conn = webnotes.db.Database(user=db_name)
  92. global session
  93. session = {'user':'Administrator'}
  94. import webnotes.profile
  95. global user
  96. user = webnotes.profile.Profile('Administrator')
  97. def get_env_vars(env_var):
  98. import os
  99. return os.environ.get(env_var,'None')
  100. remote_ip = get_env_vars('REMOTE_ADDR') #Required for login from python shell
  101. logger = None
  102. def get_db_password(db_name):
  103. """get db password from defs"""
  104. import defs
  105. if hasattr(defs, 'get_db_password'):
  106. return defs.get_db_password(db_name)
  107. elif hasattr(defs, 'db_password'):
  108. return defs.db_password
  109. else:
  110. return db_name
  111. whitelisted = []
  112. guest_methods = []
  113. def whitelist(allow_guest=False):
  114. """
  115. decorator for whitelisting a function
  116. Note: if the function is allowed to be accessed by a guest user,
  117. it must explicitly be marked as allow_guest=True
  118. """
  119. def innerfn(fn):
  120. global whitelisted, guest_methods
  121. whitelisted.append(fn)
  122. if allow_guest:
  123. guest_methods.append(fn)
  124. return fn
  125. return innerfn