Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

199 wiersze
6.4 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, 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_server_obj(doc, doclist = [], basedoctype = ''):
  71. # for test
  72. import webnotes
  73. from webnotes.modules import scrub, get_doctype_module
  74. from core.doctype.custom_script.custom_script import get_custom_server_script
  75. # get doctype details
  76. module = get_doctype_module(doc.doctype)
  77. if not module:
  78. return
  79. DocType = get_doctype_class(doc.doctype, module)
  80. # custom?
  81. custom_script = get_custom_server_script(doc.doctype)
  82. if custom_script:
  83. global custom_class
  84. exec custom_class + custom_script.replace('\t',' ') in locals()
  85. return CustomDocType(doc, doclist)
  86. else:
  87. return DocType(doc, doclist)
  88. def get_doctype_class(doctype, module):
  89. from webnotes.utils import cint
  90. import webnotes
  91. module = load_doctype_module(doctype, module)
  92. if module:
  93. DocType = getattr(module, 'DocType')
  94. else:
  95. if not cint(webnotes.conn.get_value("DocType", doctype, "custom")):
  96. raise Exception, "Unable to load module for :" + doctype
  97. class DocType:
  98. def __init__(self, d, dl):
  99. self.doc, self.doclist = d, dl
  100. return DocType
  101. def get_module_name(doctype, module, prefix):
  102. from webnotes.modules import scrub
  103. _doctype, _module = scrub(doctype), scrub(module)
  104. return '%s.doctype.%s.%s%s' % (_module, _doctype, prefix, _doctype)
  105. def load_doctype_module(doctype, module, prefix=""):
  106. from webnotes.modules import scrub
  107. _doctype, _module = scrub(doctype), scrub(module)
  108. try:
  109. module = __import__(get_module_name(doctype, module, prefix), fromlist=[''])
  110. return module
  111. except ImportError, e:
  112. return None
  113. def get_obj(dt = None, dn = None, doc=None, doclist=[], with_children = 0):
  114. if dt:
  115. import webnotes.model.doc
  116. if not dn:
  117. dn = dt
  118. if with_children:
  119. doclist = webnotes.model.doc.get(dt, dn, from_controller=1)
  120. else:
  121. doclist = webnotes.model.doc.get(dt, dn, with_children = 0, from_controller=1)
  122. return get_server_obj(doclist[0], doclist)
  123. else:
  124. return get_server_obj(doc, doclist)
  125. def run_server_obj(server_obj, method_name, arg=None):
  126. """
  127. Executes a method (`method_name`) from the given object (`server_obj`)
  128. """
  129. if server_obj and hasattr(server_obj, method_name):
  130. if arg:
  131. return getattr(server_obj, method_name)(arg)
  132. else:
  133. return getattr(server_obj, method_name)()
  134. else:
  135. raise Exception, 'No method %s' % method_name
  136. def get_code(module, dt, dn, extn, fieldname=None):
  137. from webnotes.modules import scrub, get_module_path
  138. import os, webnotes
  139. # get module (if required)
  140. if not module:
  141. module = webnotes.conn.get_value(dt, dn, 'module')
  142. # no module, quit
  143. if not module:
  144. return ''
  145. # file names
  146. if dt in ('Page','Doctype','Search Criteria'):
  147. dt, dn = scrub(dt), scrub(dn)
  148. # get file name
  149. fname = dn + '.' + extn
  150. # code
  151. code = ''
  152. try:
  153. file = open(os.path.join(get_module_path(scrub(module)), dt, dn, fname), 'r')
  154. code = file.read()
  155. file.close()
  156. except IOError, e:
  157. # no file, try from db
  158. if fieldname:
  159. code = webnotes.conn.get_value(dt, dn, fieldname)
  160. return code