Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

219 Zeilen
5.5 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. """
  23. globals attached to webnotes module
  24. + some utility functions that should probably be moved
  25. """
  26. code_fields_dict = {
  27. 'Page':[('script', 'js'), ('content', 'html'), ('style', 'css'), ('static_content', 'html'), ('server_code', 'py')],
  28. 'DocType':[('server_code_core', 'py'), ('client_script_core', 'js')],
  29. 'Search Criteria':[('report_script', 'js'), ('server_script', 'py'), ('custom_query', 'sql')],
  30. 'Patch':[('patch_code', 'py')],
  31. 'Stylesheet':['stylesheet', 'css'],
  32. 'Page Template':['template', 'html'],
  33. 'Control Panel':[('startup_code', 'js'), ('startup_css', 'css')]
  34. }
  35. version = 'v170'
  36. form_dict = {}
  37. auth_obj = None
  38. conn = None
  39. form = None
  40. session = None
  41. user = None
  42. is_testing = None
  43. incoming_cookies = {}
  44. add_cookies = {} # append these to outgoing request
  45. cookies = {}
  46. auto_masters = {}
  47. tenant_id = None
  48. response = {'message':'', 'exc':''}
  49. debug_log = []
  50. message_log = []
  51. class ValidationError(Exception):
  52. pass
  53. class AuthenticationError(Exception):
  54. pass
  55. class UnknownDomainError(Exception):
  56. def __init__(self, value):
  57. self.value = value
  58. def __str__(self):
  59. return repr(self.value)
  60. class SessionStopped(Exception):
  61. def __init__(self, value):
  62. self.value = value
  63. def __str__(self):
  64. return repr(self.value)
  65. def getTraceback():
  66. import utils
  67. return utils.getTraceback()
  68. def errprint(msg):
  69. """
  70. Append to the :data:`debug log`
  71. """
  72. from utils import cstr
  73. debug_log.append(cstr(msg or ''))
  74. def msgprint(msg, small=0, raise_exception=0, as_table=False):
  75. """
  76. Append to the :data:`message_log`
  77. """
  78. from utils import cstr
  79. if as_table and type(msg) in (list, tuple):
  80. 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>'
  81. message_log.append((small and '__small:' or '')+cstr(msg or ''))
  82. if raise_exception:
  83. raise ValidationError
  84. def is_apache_user():
  85. import os
  86. if os.environ.get('USER') == 'apache':
  87. return True
  88. else:
  89. return (not os.environ.get('USER'))
  90. # os.environ does not have user, so allows a security vulnerability,fixed now.
  91. def get_index_path():
  92. import os
  93. return os.sep.join(os.path.dirname(os.path.abspath(__file__)).split(os.sep)[:-2])
  94. def get_files_path():
  95. import defs, os
  96. if not conn:
  97. raise Exception, 'You must login first'
  98. if defs.files_path:
  99. return os.path.join(defs.files_path, conn.cur_db_name)
  100. else:
  101. return os.path.join(get_index_path(), 'user_files', conn.cur_db_name)
  102. def create_folder(path):
  103. """
  104. Wrapper function for os.makedirs (does not throw exception if directory exists)
  105. """
  106. import os
  107. try:
  108. os.makedirs(path)
  109. except OSError, e:
  110. if e.args[0]!=17:
  111. raise e
  112. def create_symlink(source_path, link_path):
  113. """
  114. Wrapper function for os.symlink (does not throw exception if directory exists)
  115. """
  116. import os
  117. try:
  118. os.symlink(source_path, link_path)
  119. except OSError, e:
  120. if e.args[0]!=17:
  121. raise e
  122. def remove_file(path):
  123. """
  124. Wrapper function for os.remove (does not throw exception if file/symlink does not exists)
  125. """
  126. import os
  127. try:
  128. os.remove(path)
  129. except OSError, e:
  130. if e.args[0]!=2:
  131. raise e
  132. def connect(db_name=None):
  133. """
  134. Connect to this db (or db), if called from command prompt
  135. """
  136. if is_apache_user():
  137. raise Exception, 'Not for web users!'
  138. import webnotes.db
  139. global conn
  140. conn = webnotes.db.Database(user=db_name)
  141. global session
  142. session = {'user':'Administrator'}
  143. import webnotes.profile
  144. global user
  145. user = webnotes.profile.Profile('Administrator')
  146. def get_env_vars(env_var):
  147. import os
  148. return os.environ.get(env_var,'None')
  149. remote_ip = get_env_vars('REMOTE_ADDR') #Required for login from python shell
  150. logger = None
  151. def get_db_password(db_name):
  152. """get db password from defs"""
  153. import defs
  154. if hasattr(defs, 'get_db_password'):
  155. return defs.get_db_password(db_name)
  156. elif hasattr(defs, 'db_password'):
  157. return defs.db_password
  158. else:
  159. return db_name
  160. whitelisted = []
  161. guest_methods = []
  162. def whitelist(allow_guest=False):
  163. """
  164. decorator for whitelisting a function
  165. Note: if the function is allowed to be accessed by a guest user,
  166. it must explicitly be marked as allow_guest=True
  167. """
  168. def innerfn(fn):
  169. global whitelisted, guest_methods
  170. whitelisted.append(fn)
  171. if allow_guest:
  172. guest_methods.append(fn)
  173. return fn
  174. return innerfn
  175. def clear_cache():
  176. """clear boot cache"""
  177. from webnotes.session_cache import clear
  178. clear()