Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. """
  2. This is where all the plug-in code is executed. The standard method for DocTypes is declaration of a
  3. standardized `DocType` class that has the methods of any DocType. When an object is instantiated using the
  4. `get_obj` method, it creates an instance of the `DocType` class of that particular DocType and sets the
  5. `doc` and `doclist` attributes that represent the fields (properties) of that record.
  6. methods in following modules are imported for backward compatibility
  7. * webnotes.*
  8. * webnotes.utils.*
  9. * webnotes.model.doc.*
  10. * webnotes.model.doclist.*
  11. """
  12. custom_class = '''
  13. # Please edit this list and import only required elements
  14. import webnotes
  15. from webnotes.utils import add_days, add_months, add_years, cint, cstr, date_diff, default_fields, flt, fmt_money, formatdate, generate_hash, getTraceback, get_defaults, get_first_day, get_last_day, getdate, has_common, month_name, now, nowdate, replace_newlines, sendmail, set_default, str_esc_quote, user_format, validate_email_add
  16. from webnotes.model import db_exists
  17. from webnotes.model.doc import Document, addchild, removechild, getchildren, make_autoname, SuperDocType
  18. from webnotes.model.utils import getlist
  19. from webnotes.model.code import get_obj, get_server_obj, run_server_obj, updatedb, check_syntax
  20. from webnotes import session, form, is_testing, msgprint, errprint
  21. set = webnotes.conn.set
  22. sql = webnotes.conn.sql
  23. get_value = webnotes.conn.get_value
  24. in_transaction = webnotes.conn.in_transaction
  25. convert_to_lists = webnotes.conn.convert_to_lists
  26. # -----------------------------------------------------------------------------------------
  27. class CustomDocType(DocType):
  28. def __init__(self, doc, doclist):
  29. DocType.__init__(self, doc, doclist)
  30. '''
  31. #=================================================================================
  32. # execute a script with a lot of globals - deprecated
  33. #=================================================================================
  34. def execute(code, doc=None, doclist=[]):
  35. """
  36. Execute the code, if doc is given, then return the instance of the `DocType` class created
  37. """
  38. # functions used in server script of DocTypes
  39. # --------------------------------------------------
  40. from webnotes.utils import add_days, add_months, add_years, cint, cstr, date_diff, default_fields, flt, fmt_money, formatdate, generate_hash, getTraceback, get_defaults, get_first_day, get_last_day, getdate, has_common, month_name, now, nowdate, replace_newlines, sendmail, set_default, str_esc_quote, user_format, validate_email_add
  41. from webnotes.model import db_exists
  42. from webnotes.model.doc import Document, addchild, removechild, getchildren
  43. from webnotes.model.utils import getlist
  44. from webnotes import session, form, msgprint, errprint
  45. import webnotes
  46. sql = webnotes.conn.sql
  47. get_value = webnotes.conn.get_value
  48. convert_to_lists = webnotes.conn.convert_to_lists
  49. if webnotes.user:
  50. get_roles = webnotes.user.get_roles
  51. locals().update({'get_obj':get_obj, 'get_server_obj':get_server_obj, 'run_server_obj':run_server_obj, 'updatedb':updatedb, 'check_syntax':check_syntax})
  52. exec code in locals()
  53. if doc:
  54. d = DocType(doc, doclist)
  55. return d
  56. if locals().get('page_html'):
  57. return page_html
  58. if locals().get('out'):
  59. return out
  60. #=================================================================================
  61. # load the DocType class from module & return an instance
  62. #=================================================================================
  63. def get_custom_script(doctype, script_type):
  64. """
  65. Returns custom script if set in doctype `Custom Script`
  66. """
  67. import webnotes
  68. try:
  69. custom_script = webnotes.conn.sql("select script from `tabCustom Script` where dt=%s and script_type=%s", (doctype, script_type))
  70. except Exception, e:
  71. if e.args[0]==1146:
  72. return None
  73. else: raise e
  74. if custom_script and custom_script[0][0]:
  75. return custom_script[0][0]
  76. def get_server_obj(doc, doclist = [], basedoctype = ''):
  77. """
  78. Returns the instantiated `DocType` object. Will also manage caching & compiling
  79. """
  80. # for test
  81. import webnotes
  82. # get doctype details
  83. module = webnotes.conn.get_value('DocType', doc.doctype, 'module')
  84. # no module specified (must be really old), can't get code so quit
  85. if not module:
  86. return
  87. module = module.replace(' ','_').lower()
  88. dt = doc.doctype.replace(' ','_').lower()
  89. # import
  90. try:
  91. exec 'from %s.doctype.%s.%s import DocType' % (module, dt, dt)
  92. except ImportError, e:
  93. # declare it here
  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. #=================================================================================
  106. # get object (from dt and/or dn or doclist)
  107. #=================================================================================
  108. def get_obj(dt = None, dn = None, doc=None, doclist=[], with_children = 0):
  109. """
  110. Returns the instantiated `DocType` object. Here you can pass the DocType and name (ID) to get the object.
  111. If with_children is true, then all child records will be laoded and added in the doclist.
  112. """
  113. if dt:
  114. import webnotes.model.doc
  115. if not dn:
  116. dn = dt
  117. if with_children:
  118. doclist = webnotes.model.doc.get(dt, dn, from_get_obj=1)
  119. else:
  120. doclist = webnotes.model.doc.get(dt, dn, with_children = 0, from_get_obj=1)
  121. return get_server_obj(doclist[0], doclist)
  122. else:
  123. return get_server_obj(doc, doclist)
  124. #=================================================================================
  125. # get object and run method
  126. #=================================================================================
  127. def run_server_obj(server_obj, method_name, arg=None):
  128. """
  129. Executes a method (`method_name`) from the given object (`server_obj`)
  130. """
  131. if server_obj and hasattr(server_obj, method_name):
  132. if arg:
  133. return getattr(server_obj, method_name)(arg)
  134. else:
  135. return getattr(server_obj, method_name)()
  136. else:
  137. raise Exception, 'No method %s' % method_name
  138. #=================================================================================
  139. # deprecated methods to keep v160 apps happy
  140. #=================================================================================
  141. def updatedb(doctype, userfields = [], args = {}):
  142. pass
  143. def check_syntax(code):
  144. return ''
  145. #===================================================================================
  146. def get_code(module, dt, dn, extn, is_static=None, fieldname=None):
  147. from webnotes.modules import scrub, get_module_path
  148. import os, webnotes
  149. # get module (if required)
  150. if not module:
  151. module = webnotes.conn.sql("select module from `tab%s` where name=%s" % (dt,'%s'),dn)[0][0]
  152. # no module, quit
  153. if not module:
  154. return ''
  155. # file names
  156. if scrub(dt) in ('page','doctype','search_criteria'):
  157. dt, dn = scrub(dt), scrub(dn)
  158. # get file name
  159. fname = dn + '.' + extn
  160. if is_static:
  161. fname = dn + '_static.' + extn
  162. # code
  163. code = ''
  164. try:
  165. file = open(os.path.join(get_module_path(scrub(module)), dt, dn, fname), 'r')
  166. code = file.read()
  167. file.close()
  168. except IOError, e:
  169. # no file, try from db
  170. if fieldname:
  171. code = webnotes.conn.get_value(dt, dn, fieldname)
  172. return code