No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

184 líneas
5.4 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. try:
  92. module = __import__(get_module_name(doctype, module, prefix), fromlist=[''])
  93. return module
  94. except ImportError, e:
  95. # webnotes.errprint(webnotes.getTraceback())
  96. return None
  97. def get_obj(dt = None, dn = None, doc=None, doclist=[], with_children = 0):
  98. if dt:
  99. import webnotes.model.doc
  100. if not dn:
  101. dn = dt
  102. if with_children:
  103. doclist = webnotes.model.doc.get(dt, dn, from_controller=1)
  104. else:
  105. doclist = webnotes.model.doc.get(dt, dn, with_children = 0, from_controller=1)
  106. return get_server_obj(doclist[0], doclist)
  107. else:
  108. return get_server_obj(doc, doclist)
  109. def run_server_obj(server_obj, method_name, arg=None):
  110. """
  111. Executes a method (`method_name`) from the given object (`server_obj`)
  112. """
  113. if server_obj and hasattr(server_obj, method_name):
  114. if arg:
  115. return getattr(server_obj, method_name)(arg)
  116. else:
  117. return getattr(server_obj, method_name)()
  118. else:
  119. raise Exception, 'No method %s' % method_name
  120. def get_code(module, dt, dn, extn, fieldname=None):
  121. from webnotes.modules import scrub, get_module_path
  122. import os, webnotes
  123. # get module (if required)
  124. if not module:
  125. module = webnotes.conn.get_value(dt, dn, 'module')
  126. # no module, quit
  127. if not module:
  128. return ''
  129. # file names
  130. if dt in ('Page','Doctype'):
  131. dt, dn = scrub(dt), scrub(dn)
  132. # get file name
  133. fname = dn + '.' + extn
  134. # code
  135. code = ''
  136. try:
  137. file = open(os.path.join(get_module_path(scrub(module)), dt, dn, fname), 'r')
  138. code = file.read()
  139. file.close()
  140. except IOError, e:
  141. # no file, try from db
  142. if fieldname:
  143. code = webnotes.conn.get_value(dt, dn, fieldname)
  144. return code