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

203 行
6.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. from __future__ import unicode_literals
  23. """
  24. This is where all the plug-in code is executed. The standard method for DocTypes is declaration of a
  25. standardized `DocType` class that has the methods of any DocType. When an object is instantiated using the
  26. `get_obj` method, it creates an instance of the `DocType` class of that particular DocType and sets the
  27. `doc` and `doclist` attributes that represent the fields (properties) of that record.
  28. methods in following modules are imported for backward compatibility
  29. * webnotes.*
  30. * webnotes.utils.*
  31. * webnotes.model.doc.*
  32. * webnotes.model.bean.*
  33. """
  34. custom_class = '''
  35. import webnotes
  36. from webnotes.utils import add_days, add_months, add_years, cint, cstr, date_diff, default_fields, flt, fmt_money, formatdate, getTraceback, get_defaults, get_first_day, get_last_day, getdate, has_common, now, nowdate, set_default, user_format, validate_email_add
  37. from webnotes.model import db_exists
  38. from webnotes.model.doc import Document, addchild, getchildren
  39. from webnotes.model.utils import getlist
  40. from webnotes.utils.email_lib import sendmail
  41. from webnotes.model.code import get_obj, get_server_obj, run_server_obj
  42. from webnotes import session, form, msgprint, errprint
  43. sql = webnotes.conn.sql
  44. class CustomDocType(DocType):
  45. def __init__(self, doc, doclist):
  46. DocType.__init__(self, doc, doclist)
  47. '''
  48. def execute(code, doc=None, doclist=[]):
  49. # functions used in server script of DocTypes
  50. # --------------------------------------------------
  51. from webnotes.utils import add_days, add_months, add_years, cint, cstr, date_diff, default_fields, flt, fmt_money, formatdate, getTraceback, get_defaults, get_first_day, get_last_day, getdate, has_common, now, nowdate, set_default, user_format, validate_email_add
  52. from webnotes.utils.email_lib import sendmail
  53. from webnotes.model import db_exists
  54. from webnotes.model.doc import Document, addchild, getchildren
  55. from webnotes.model.utils import getlist
  56. from webnotes import session, form, msgprint, errprint
  57. import webnotes
  58. sql = webnotes.conn.sql
  59. get_value = webnotes.conn.get_value
  60. convert_to_lists = webnotes.conn.convert_to_lists
  61. if webnotes.user:
  62. get_roles = webnotes.user.get_roles
  63. locals().update({'get_obj':get_obj, 'get_server_obj':get_server_obj, 'run_server_obj':run_server_obj})
  64. exec code in locals()
  65. if doc:
  66. d = DocType(doc, doclist)
  67. return d
  68. if locals().get('page_html'):
  69. return page_html
  70. if locals().get('out'):
  71. return out
  72. def get_server_obj(doc, doclist = [], basedoctype = ''):
  73. # for test
  74. import webnotes
  75. from webnotes.modules import scrub, get_doctype_module
  76. from core.doctype.custom_script.custom_script import get_custom_server_script
  77. # get doctype details
  78. module = get_doctype_module(doc.doctype) or "core"
  79. if not module:
  80. return
  81. DocType = get_doctype_class(doc.doctype, module)
  82. # custom?
  83. custom_script = get_custom_server_script(doc.doctype)
  84. if custom_script:
  85. global custom_class
  86. exec custom_class + custom_script.replace('\t',' ') in locals()
  87. return CustomDocType(doc, doclist)
  88. else:
  89. return DocType(doc, doclist)
  90. def get_doctype_class(doctype, module):
  91. from webnotes.utils import cint
  92. import webnotes
  93. module = load_doctype_module(doctype, module)
  94. if module:
  95. DocType = getattr(module, 'DocType')
  96. else:
  97. if not cint(webnotes.conn.get_value("DocType", doctype, "custom")):
  98. raise ImportError, "Unable to load module for: " + doctype
  99. class DocType:
  100. def __init__(self, d, dl):
  101. self.doc, self.doclist = d, dl
  102. return DocType
  103. def get_module_name(doctype, module, prefix):
  104. from webnotes.modules import scrub
  105. _doctype, _module = scrub(doctype), scrub(module)
  106. return '%s.doctype.%s.%s%s' % (_module, _doctype, prefix, _doctype)
  107. def load_doctype_module(doctype, module, prefix=""):
  108. import webnotes
  109. from webnotes.modules import scrub
  110. _doctype, _module = scrub(doctype), scrub(module)
  111. try:
  112. module = __import__(get_module_name(doctype, module, prefix), fromlist=[''])
  113. return module
  114. except ImportError, e:
  115. # webnotes.errprint(webnotes.getTraceback())
  116. return None
  117. def get_obj(dt = None, dn = None, doc=None, doclist=[], with_children = 0):
  118. if dt:
  119. import webnotes.model.doc
  120. if not dn:
  121. dn = dt
  122. if with_children:
  123. doclist = webnotes.model.doc.get(dt, dn, from_controller=1)
  124. else:
  125. doclist = webnotes.model.doc.get(dt, dn, with_children = 0, from_controller=1)
  126. return get_server_obj(doclist[0], doclist)
  127. else:
  128. return get_server_obj(doc, doclist)
  129. def run_server_obj(server_obj, method_name, arg=None):
  130. """
  131. Executes a method (`method_name`) from the given object (`server_obj`)
  132. """
  133. if server_obj and hasattr(server_obj, method_name):
  134. if arg:
  135. return getattr(server_obj, method_name)(arg)
  136. else:
  137. return getattr(server_obj, method_name)()
  138. else:
  139. raise Exception, 'No method %s' % method_name
  140. def get_code(module, dt, dn, extn, fieldname=None):
  141. from webnotes.modules import scrub, get_module_path
  142. import os, webnotes
  143. # get module (if required)
  144. if not module:
  145. module = webnotes.conn.get_value(dt, dn, 'module')
  146. # no module, quit
  147. if not module:
  148. return ''
  149. # file names
  150. if dt in ('Page','Doctype'):
  151. dt, dn = scrub(dt), scrub(dn)
  152. # get file name
  153. fname = dn + '.' + extn
  154. # code
  155. code = ''
  156. try:
  157. file = open(os.path.join(get_module_path(scrub(module)), dt, dn, fname), 'r')
  158. code = file.read()
  159. file.close()
  160. except IOError, e:
  161. # no file, try from db
  162. if fieldname:
  163. code = webnotes.conn.get_value(dt, dn, fieldname)
  164. return code