Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

185 рядки
5.5 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. """
  5. This is where all the plug-in code is executed. The standard method for DocTypes is declaration of a
  6. standardized `DocType` class that has the methods of any DocType. When an object is instantiated using the
  7. `get_obj` method, it creates an instance of the `DocType` class of that particular DocType and sets the
  8. `doc` and `doclist` attributes that represent the fields (properties) of that record.
  9. methods in following modules are imported for backward compatibility
  10. * webnotes.*
  11. * webnotes.utils.*
  12. * webnotes.model.doc.*
  13. * webnotes.model.bean.*
  14. """
  15. custom_class = '''
  16. import webnotes
  17. 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, set_default, user_format, validate_email_add
  18. from webnotes.model import db_exists
  19. from webnotes.model.doc import Document, addchild, getchildren
  20. from webnotes.model.utils import getlist
  21. from webnotes.utils.email_lib import sendmail
  22. from webnotes.model.code import get_obj, get_server_obj, run_server_obj
  23. from webnotes import session, form, msgprint, errprint
  24. sql = webnotes.conn.sql
  25. class CustomDocType(DocType):
  26. def __init__(self, doc, doclist):
  27. DocType.__init__(self, doc, doclist)
  28. '''
  29. def execute(code, doc=None, doclist=[]):
  30. # functions used in server script of DocTypes
  31. # --------------------------------------------------
  32. 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, set_default, user_format, validate_email_add
  33. from webnotes.utils.email_lib import sendmail
  34. from webnotes.model import db_exists
  35. from webnotes.model.doc import Document, addchild, getchildren
  36. from webnotes.model.utils import getlist
  37. from webnotes import session, form, msgprint, errprint
  38. import webnotes
  39. sql = webnotes.conn.sql
  40. get_value = webnotes.conn.get_value
  41. convert_to_lists = webnotes.conn.convert_to_lists
  42. if webnotes.user:
  43. get_roles = webnotes.user.get_roles
  44. locals().update({'get_obj':get_obj, 'get_server_obj':get_server_obj, 'run_server_obj':run_server_obj})
  45. exec code in locals()
  46. if doc:
  47. d = DocType(doc, doclist)
  48. return d
  49. if locals().get('page_html'):
  50. return page_html
  51. if locals().get('out'):
  52. return out
  53. def get_server_obj(doc, doclist = [], basedoctype = ''):
  54. # for test
  55. import webnotes
  56. from webnotes.modules import scrub, get_doctype_module
  57. from core.doctype.custom_script.custom_script import get_custom_server_script
  58. # get doctype details
  59. module = get_doctype_module(doc.doctype) or "core"
  60. if not module:
  61. return
  62. DocType = get_doctype_class(doc.doctype, module)
  63. # custom?
  64. custom_script = get_custom_server_script(doc.doctype)
  65. if custom_script:
  66. global custom_class
  67. exec custom_class + custom_script.replace('\t',' ') in locals()
  68. return CustomDocType(doc, doclist)
  69. else:
  70. return DocType(doc, doclist)
  71. def get_doctype_class(doctype, module):
  72. from webnotes.utils import cint
  73. import webnotes
  74. module = load_doctype_module(doctype, module)
  75. if module:
  76. DocType = getattr(module, 'DocType')
  77. else:
  78. if not cint(webnotes.conn.get_value("DocType", doctype, "custom")):
  79. raise ImportError, "Unable to load module for: " + doctype
  80. class DocType:
  81. def __init__(self, d, dl):
  82. self.doc, self.doclist = d, dl
  83. return DocType
  84. def get_module_name(doctype, module, prefix):
  85. from webnotes.modules import scrub
  86. _doctype, _module = scrub(doctype), scrub(module)
  87. return '%s.doctype.%s.%s%s' % (_module, _doctype, prefix, _doctype)
  88. def load_doctype_module(doctype, module, prefix=""):
  89. import webnotes
  90. from webnotes.modules import scrub
  91. _doctype, _module = scrub(doctype), scrub(module)
  92. try:
  93. module = __import__(get_module_name(doctype, module, prefix), fromlist=[''])
  94. return module
  95. except ImportError, e:
  96. # webnotes.errprint(webnotes.getTraceback())
  97. return None
  98. def get_obj(dt = None, dn = None, doc=None, doclist=[], with_children = 0):
  99. if dt:
  100. import webnotes.model.doc
  101. if not dn:
  102. dn = dt
  103. if with_children:
  104. doclist = webnotes.model.doc.get(dt, dn, from_controller=1)
  105. else:
  106. doclist = webnotes.model.doc.get(dt, dn, with_children = 0, from_controller=1)
  107. return get_server_obj(doclist[0], doclist)
  108. else:
  109. return get_server_obj(doc, doclist)
  110. def run_server_obj(server_obj, method_name, arg=None):
  111. """
  112. Executes a method (`method_name`) from the given object (`server_obj`)
  113. """
  114. if server_obj and hasattr(server_obj, method_name):
  115. if arg:
  116. return getattr(server_obj, method_name)(arg)
  117. else:
  118. return getattr(server_obj, method_name)()
  119. else:
  120. raise Exception, 'No method %s' % method_name
  121. def get_code(module, dt, dn, extn, fieldname=None):
  122. from webnotes.modules import scrub, get_module_path
  123. import os, webnotes
  124. # get module (if required)
  125. if not module:
  126. module = webnotes.conn.get_value(dt, dn, 'module')
  127. # no module, quit
  128. if not module:
  129. return ''
  130. # file names
  131. if dt in ('Page','Doctype'):
  132. dt, dn = scrub(dt), scrub(dn)
  133. # get file name
  134. fname = dn + '.' + extn
  135. # code
  136. code = ''
  137. try:
  138. file = open(os.path.join(get_module_path(scrub(module)), dt, dn, fname), 'r')
  139. code = file.read()
  140. file.close()
  141. except IOError, e:
  142. # no file, try from db
  143. if fieldname:
  144. code = webnotes.conn.get_value(dt, dn, fieldname)
  145. return code