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

186 行
4.1 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. class UnknownDomainError(Exception):
  35. def __init__(self, value):
  36. self.value = value
  37. def __str__(self):
  38. return repr(self.value)
  39. def getTraceback():
  40. import utils
  41. return utils.getTraceback()
  42. def errprint(msg):
  43. """
  44. Append to the :data:`debug log`
  45. """
  46. from utils import cstr
  47. debug_log.append(cstr(msg or ''))
  48. def msgprint(msg, small=0, raise_exception=0, as_table=False):
  49. """
  50. Append to the :data:`message_log`
  51. """
  52. from utils import cstr
  53. if as_table and type(msg) in (list, tuple):
  54. 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>'
  55. message_log.append((small and '__small:' or '')+cstr(msg or ''))
  56. if raise_exception:
  57. raise ValidationError
  58. def is_apache_user():
  59. import os
  60. if os.environ.get('USER') == 'apache':
  61. return True
  62. else:
  63. return (not os.environ.get('USER'))
  64. # os.environ does not have user, so allows a security vulnerability,fixed now.
  65. def get_index_path():
  66. import os
  67. return os.sep.join(os.path.dirname(os.path.abspath(__file__)).split(os.sep)[:-2])
  68. def get_files_path():
  69. import defs, os
  70. if not conn:
  71. raise Exception, 'You must login first'
  72. if defs.files_path:
  73. return os.path.join(defs.files_path, conn.cur_db_name)
  74. else:
  75. return os.path.join(get_index_path(), 'user_files', conn.cur_db_name)
  76. def create_folder(path):
  77. """
  78. Wrapper function for os.makedirs (does not throw exception if directory exists)
  79. """
  80. import os
  81. try:
  82. os.makedirs(path)
  83. except OSError, e:
  84. if e.args[0]!=17:
  85. raise e
  86. def create_symlink(source_path, link_path):
  87. """
  88. Wrapper function for os.symlink (does not throw exception if directory exists)
  89. """
  90. import os
  91. try:
  92. os.symlink(source_path, link_path)
  93. except OSError, e:
  94. if e.args[0]!=17:
  95. raise e
  96. def remove_file(path):
  97. """
  98. Wrapper function for os.remove (does not throw exception if file/symlink does not exists)
  99. """
  100. import os
  101. try:
  102. os.remove(path)
  103. except OSError, e:
  104. if e.args[0]!=2:
  105. raise e
  106. def connect(db_name=None):
  107. """
  108. Connect to this db (or db), if called from command prompt
  109. """
  110. if is_apache_user():
  111. raise Exception, 'Not for web users!'
  112. import webnotes.db
  113. global conn
  114. conn = webnotes.db.Database(user=db_name)
  115. global session
  116. session = {'user':'Administrator'}
  117. import webnotes.profile
  118. global user
  119. user = webnotes.profile.Profile('Administrator')
  120. def get_env_vars(env_var):
  121. import os
  122. return os.environ.get(env_var,'None')
  123. remote_ip = get_env_vars('REMOTE_ADDR') #Required for login from python shell
  124. logger = None
  125. def get_db_password(db_name):
  126. """get db password from defs"""
  127. import defs
  128. if hasattr(defs, 'get_db_password'):
  129. return defs.get_db_password(db_name)
  130. elif hasattr(defs, 'db_password'):
  131. return defs.db_password
  132. else:
  133. return db_name
  134. whitelisted = []
  135. guest_methods = []
  136. def whitelist(allow_guest=False):
  137. """
  138. decorator for whitelisting a function
  139. Note: if the function is allowed to be accessed by a guest user,
  140. it must explicitly be marked as allow_guest=True
  141. """
  142. def innerfn(fn):
  143. global whitelisted, guest_methods
  144. whitelisted.append(fn)
  145. if allow_guest:
  146. guest_methods.append(fn)
  147. return fn
  148. return innerfn