You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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