25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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