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.

code.py 6.7 KiB

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