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.
 
 
 
 
 
 

191 rivejä
6.1 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. module = scrub(module)
  86. dt = scrub(doc.doctype)
  87. try:
  88. module = __import__('%s.doctype.%s.%s' % (module, dt, dt), fromlist=[''])
  89. DocType = getattr(module, 'DocType')
  90. except ImportError, e:
  91. from webnotes.utils import cint
  92. if not cint(webnotes.conn.get_value("DocType", doc.doctype, "custom")):
  93. raise e
  94. class DocType:
  95. def __init__(self, d, dl):
  96. self.doc, self.doclist = d, dl
  97. # custom?
  98. custom_script = get_custom_script(doc.doctype, 'Server')
  99. if custom_script:
  100. global custom_class
  101. exec custom_class + custom_script.replace('\t',' ') in locals()
  102. return CustomDocType(doc, doclist)
  103. else:
  104. return DocType(doc, doclist)
  105. def get_obj(dt = None, dn = None, doc=None, doclist=[], with_children = 0):
  106. if dt:
  107. import webnotes.model.doc
  108. if not dn:
  109. dn = dt
  110. if with_children:
  111. doclist = webnotes.model.doc.get(dt, dn, from_controller=1)
  112. else:
  113. doclist = webnotes.model.doc.get(dt, dn, with_children = 0, from_controller=1)
  114. return get_server_obj(doclist[0], doclist)
  115. else:
  116. return get_server_obj(doc, doclist)
  117. def run_server_obj(server_obj, method_name, arg=None):
  118. """
  119. Executes a method (`method_name`) from the given object (`server_obj`)
  120. """
  121. if server_obj and hasattr(server_obj, method_name):
  122. if arg:
  123. return getattr(server_obj, method_name)(arg)
  124. else:
  125. return getattr(server_obj, method_name)()
  126. else:
  127. raise Exception, 'No method %s' % method_name
  128. def get_code(module, dt, dn, extn, fieldname=None):
  129. from webnotes.modules import scrub, get_module_path
  130. import os, webnotes
  131. # get module (if required)
  132. if not module:
  133. module = webnotes.conn.get_value(dt, dn, 'module')
  134. # no module, quit
  135. if not module:
  136. return ''
  137. # file names
  138. if dt in ('Page','Doctype','Search Criteria'):
  139. dt, dn = scrub(dt), scrub(dn)
  140. # get file name
  141. fname = dn + '.' + extn
  142. # code
  143. code = ''
  144. try:
  145. file = open(os.path.join(get_module_path(scrub(module)), dt, dn, fname), 'r')
  146. code = file.read()
  147. file.close()
  148. except IOError, e:
  149. # no file, try from db
  150. if fieldname:
  151. code = webnotes.conn.get_value(dt, dn, fieldname)
  152. return code