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

239 行
8.4 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. This is where all the plug-in code is executed. The standard method for DocTypes is declaration of a
  24. standardized `DocType` class that has the methods of any DocType. When an object is instantiated using the
  25. `get_obj` method, it creates an instance of the `DocType` class of that particular DocType and sets the
  26. `doc` and `doclist` attributes that represent the fields (properties) of that record.
  27. methods in following modules are imported for backward compatibility
  28. * webnotes.*
  29. * webnotes.utils.*
  30. * webnotes.model.doc.*
  31. * webnotes.model.doclist.*
  32. """
  33. custom_class = '''
  34. # Please edit this list and import only required elements
  35. import webnotes
  36. from webnotes.utils import add_days, add_months, add_years, cint, cstr, date_diff, default_fields, flt, fmt_money, formatdate, generate_hash, getTraceback, get_defaults, get_first_day, get_last_day, getdate, has_common, month_name, now, nowdate, replace_newlines, sendmail, set_default, str_esc_quote, user_format, validate_email_add
  37. from webnotes.model import db_exists
  38. from webnotes.model.doc import Document, addchild, removechild, getchildren, make_autoname, SuperDocType
  39. from webnotes.model.utils import getlist
  40. from webnotes.model.code import get_obj, get_server_obj, run_server_obj, updatedb, check_syntax
  41. from webnotes import session, form, is_testing, msgprint, errprint
  42. set = webnotes.conn.set
  43. sql = webnotes.conn.sql
  44. get_value = webnotes.conn.get_value
  45. in_transaction = webnotes.conn.in_transaction
  46. convert_to_lists = webnotes.conn.convert_to_lists
  47. # -----------------------------------------------------------------------------------------
  48. class CustomDocType(DocType):
  49. def __init__(self, doc, doclist):
  50. DocType.__init__(self, doc, doclist)
  51. '''
  52. #=================================================================================
  53. # execute a script with a lot of globals - deprecated
  54. #=================================================================================
  55. def execute(code, doc=None, doclist=[]):
  56. """
  57. Execute the code, if doc is given, then return the instance of the `DocType` class created
  58. """
  59. # functions used in server script of DocTypes
  60. # --------------------------------------------------
  61. from webnotes.utils import add_days, add_months, add_years, cint, cstr, date_diff, default_fields, flt, fmt_money, formatdate, generate_hash, getTraceback, get_defaults, get_first_day, get_last_day, getdate, has_common, month_name, now, nowdate, replace_newlines, sendmail, set_default, str_esc_quote, user_format, validate_email_add
  62. from webnotes.model import db_exists
  63. from webnotes.model.doc import Document, addchild, removechild, getchildren
  64. from webnotes.model.utils import getlist
  65. from webnotes import session, form, msgprint, errprint
  66. import webnotes
  67. sql = webnotes.conn.sql
  68. get_value = webnotes.conn.get_value
  69. convert_to_lists = webnotes.conn.convert_to_lists
  70. if webnotes.user:
  71. get_roles = webnotes.user.get_roles
  72. locals().update({'get_obj':get_obj, 'get_server_obj':get_server_obj, 'run_server_obj':run_server_obj, 'updatedb':updatedb, 'check_syntax':check_syntax})
  73. exec code in locals()
  74. if doc:
  75. d = DocType(doc, doclist)
  76. return d
  77. if locals().get('page_html'):
  78. return page_html
  79. if locals().get('out'):
  80. return out
  81. #=================================================================================
  82. # load the DocType class from module & return an instance
  83. #=================================================================================
  84. def get_custom_script(doctype, script_type):
  85. """
  86. Returns custom script if set in doctype `Custom Script`
  87. """
  88. import webnotes
  89. try:
  90. custom_script = webnotes.conn.sql("select script from `tabCustom Script` where dt=%s and script_type=%s", (doctype, script_type))
  91. except Exception, e:
  92. if e.args[0]==1146:
  93. return None
  94. else: raise e
  95. if custom_script and custom_script[0][0]:
  96. return custom_script[0][0]
  97. def get_server_obj(doc, doclist = [], basedoctype = ''):
  98. """
  99. Returns the instantiated `DocType` object. Will also manage caching & compiling
  100. """
  101. # for test
  102. import webnotes
  103. # get doctype details
  104. module = webnotes.conn.get_value('DocType', doc.doctype, 'module')
  105. # no module specified (must be really old), can't get code so quit
  106. if not module:
  107. return
  108. module = module.replace(' ','_').lower()
  109. dt = doc.doctype.replace(' ','_').replace('-', '_').lower()
  110. # import
  111. try:
  112. exec 'from %s.doctype.%s.%s import DocType' % (module, dt, dt)
  113. except ImportError, e:
  114. # declare it here
  115. class DocType:
  116. def __init__(self, d, dl):
  117. self.doc, self.doclist = d, dl
  118. # custom?
  119. custom_script = get_custom_script(doc.doctype, 'Server')
  120. if custom_script:
  121. global custom_class
  122. exec custom_class + custom_script.replace('\t',' ') in locals()
  123. return CustomDocType(doc, doclist)
  124. else:
  125. return DocType(doc, doclist)
  126. #=================================================================================
  127. # get object (from dt and/or dn or doclist)
  128. #=================================================================================
  129. def get_obj(dt = None, dn = None, doc=None, doclist=[], with_children = 0):
  130. """
  131. Returns the instantiated `DocType` object. Here you can pass the DocType and name (ID) to get the object.
  132. If with_children is true, then all child records will be laoded and added in the doclist.
  133. """
  134. if dt:
  135. import webnotes.model.doc
  136. if not dn:
  137. dn = dt
  138. if with_children:
  139. doclist = webnotes.model.doc.get(dt, dn, from_get_obj=1)
  140. else:
  141. doclist = webnotes.model.doc.get(dt, dn, with_children = 0, from_get_obj=1)
  142. return get_server_obj(doclist[0], doclist)
  143. else:
  144. return get_server_obj(doc, doclist)
  145. #=================================================================================
  146. # get object and run method
  147. #=================================================================================
  148. def run_server_obj(server_obj, method_name, arg=None):
  149. """
  150. Executes a method (`method_name`) from the given object (`server_obj`)
  151. """
  152. if server_obj and hasattr(server_obj, method_name):
  153. if arg:
  154. return getattr(server_obj, method_name)(arg)
  155. else:
  156. return getattr(server_obj, method_name)()
  157. else:
  158. raise Exception, 'No method %s' % method_name
  159. #=================================================================================
  160. # deprecated methods to keep v160 apps happy
  161. #=================================================================================
  162. def updatedb(doctype, userfields = [], args = {}):
  163. pass
  164. def check_syntax(code):
  165. return ''
  166. #===================================================================================
  167. def get_code(module, dt, dn, extn, is_static=None, fieldname=None):
  168. from webnotes.modules import scrub, get_module_path
  169. import os, webnotes
  170. # get module (if required)
  171. if not module:
  172. module = webnotes.conn.sql("select module from `tab%s` where name=%s" % (dt,'%s'),dn)[0][0]
  173. # no module, quit
  174. if not module:
  175. return ''
  176. # file names
  177. if scrub(dt) in ('page','doctype','search_criteria'):
  178. dt, dn = scrub(dt), scrub(dn)
  179. # get file name
  180. fname = dn + '.' + extn
  181. if is_static:
  182. fname = dn + '_static.' + extn
  183. # code
  184. code = ''
  185. try:
  186. file = open(os.path.join(get_module_path(scrub(module)), dt, dn, fname), 'r')
  187. code = file.read()
  188. file.close()
  189. except IOError, e:
  190. # no file, try from db
  191. if fieldname:
  192. code = webnotes.conn.get_value(dt, dn, fieldname)
  193. return code