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.
 
 
 
 
 
 

208 lines
6.7 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
  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. def execute(code, doc=None, doclist=[]):
  48. # functions used in server script of DocTypes
  49. # --------------------------------------------------
  50. 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
  51. from webnotes.model import db_exists
  52. from webnotes.model.doc import Document, addchild, getchildren
  53. from webnotes.model.utils import getlist
  54. from webnotes import session, form, msgprint, errprint
  55. import webnotes
  56. sql = webnotes.conn.sql
  57. get_value = webnotes.conn.get_value
  58. convert_to_lists = webnotes.conn.convert_to_lists
  59. if webnotes.user:
  60. get_roles = webnotes.user.get_roles
  61. locals().update({'get_obj':get_obj, 'get_server_obj':get_server_obj, 'run_server_obj':run_server_obj})
  62. exec code in locals()
  63. if doc:
  64. d = DocType(doc, doclist)
  65. return d
  66. if locals().get('page_html'):
  67. return page_html
  68. if locals().get('out'):
  69. return out
  70. def get_custom_script(doctype, script_type):
  71. import webnotes
  72. custom_script = webnotes.conn.sql("""select script from `tabCustom Script`
  73. where dt=%s and script_type=%s""", (doctype, script_type))
  74. if custom_script and custom_script[0][0]:
  75. return custom_script[0][0]
  76. def get_server_obj(doc, doclist = [], basedoctype = ''):
  77. # for test
  78. import webnotes
  79. from webnotes.modules import scrub
  80. # get doctype details
  81. module = webnotes.conn.get_value('DocType', doc.doctype, 'module')
  82. # no module specified (must be really old), can't get code so quit
  83. if not module:
  84. return
  85. DocType = get_doctype_class(doc.doctype, module)
  86. # custom?
  87. custom_script = get_custom_script(doc.doctype, 'Server')
  88. if custom_script:
  89. global custom_class
  90. exec custom_class + custom_script.replace('\t',' ') in locals()
  91. return CustomDocType(doc, doclist)
  92. else:
  93. return DocType(doc, doclist)
  94. def get_doctype_class(doctype, module):
  95. from webnotes.utils import cint
  96. import webnotes
  97. module = load_doctype_module(doctype, module)
  98. if module:
  99. DocType = getattr(module, 'DocType')
  100. else:
  101. if not cint(webnotes.conn.get_value("DocType", doctype, "custom")):
  102. raise Exception, "Unable to load module for :" + doctype
  103. class DocType:
  104. def __init__(self, d, dl):
  105. self.doc, self.doclist = d, dl
  106. return DocType
  107. def get_module_name(doctype, module, prefix):
  108. from webnotes.modules import scrub
  109. _doctype, _module = scrub(doctype), scrub(module)
  110. return '%s.doctype.%s.%s%s' % (_module, _doctype, prefix, _doctype)
  111. def load_doctype_module(doctype, module, prefix=""):
  112. from webnotes.modules import scrub
  113. _doctype, _module = scrub(doctype), scrub(module)
  114. try:
  115. module = __import__(get_module_name(doctype, module, prefix), fromlist=[''])
  116. return module
  117. except ImportError, e:
  118. return None
  119. def get_obj(dt = None, dn = None, doc=None, doclist=[], with_children = 0):
  120. if dt:
  121. import webnotes.model.doc
  122. if not dn:
  123. dn = dt
  124. if with_children:
  125. doclist = webnotes.model.doc.get(dt, dn, from_controller=1)
  126. else:
  127. doclist = webnotes.model.doc.get(dt, dn, with_children = 0, from_controller=1)
  128. return get_server_obj(doclist[0], doclist)
  129. else:
  130. return get_server_obj(doc, doclist)
  131. def run_server_obj(server_obj, method_name, arg=None):
  132. """
  133. Executes a method (`method_name`) from the given object (`server_obj`)
  134. """
  135. if server_obj and hasattr(server_obj, method_name):
  136. if arg:
  137. return getattr(server_obj, method_name)(arg)
  138. else:
  139. return getattr(server_obj, method_name)()
  140. else:
  141. raise Exception, 'No method %s' % method_name
  142. def get_code(module, dt, dn, extn, fieldname=None):
  143. from webnotes.modules import scrub, get_module_path
  144. import os, webnotes
  145. # get module (if required)
  146. if not module:
  147. module = webnotes.conn.get_value(dt, dn, 'module')
  148. # no module, quit
  149. if not module:
  150. return ''
  151. # file names
  152. if dt in ('Page','Doctype','Search Criteria'):
  153. dt, dn = scrub(dt), scrub(dn)
  154. # get file name
  155. fname = dn + '.' + extn
  156. # code
  157. code = ''
  158. try:
  159. file = open(os.path.join(get_module_path(scrub(module)), dt, dn, fname), 'r')
  160. code = file.read()
  161. file.close()
  162. except IOError, e:
  163. # no file, try from db
  164. if fieldname:
  165. code = webnotes.conn.get_value(dt, dn, fieldname)
  166. return code