Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

137 righe
3.8 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. def get_server_obj(doc, doclist = [], basedoctype = ''):
  16. # for test
  17. import webnotes
  18. from webnotes.modules import scrub, get_doctype_module
  19. from webnotes.plugins import get_code_and_execute
  20. # get doctype details
  21. module = get_doctype_module(doc.doctype) or "core"
  22. if not module:
  23. return
  24. DocType = get_doctype_class(doc.doctype, module)
  25. if webnotes.flags.in_import:
  26. return DocType(doc, doclist)
  27. # custom?
  28. namespace = {"DocType": DocType}
  29. get_code_and_execute(module, "DocType", doc.doctype, namespace=namespace)
  30. if namespace.get("CustomDocType"):
  31. return namespace["CustomDocType"](doc, doclist)
  32. else:
  33. return DocType(doc, doclist)
  34. def get_doctype_class(doctype, module):
  35. from webnotes.utils import cint
  36. import webnotes
  37. module = load_doctype_module(doctype, module)
  38. if module:
  39. DocType = getattr(module, 'DocType')
  40. else:
  41. if not cint(webnotes.conn.get_value("DocType", doctype, "custom")):
  42. raise ImportError, "Unable to load module for: " + doctype
  43. class DocType:
  44. def __init__(self, d, dl):
  45. self.doc, self.doclist = d, dl
  46. return DocType
  47. def get_module_name(doctype, module, prefix):
  48. from webnotes.modules import scrub
  49. _doctype, _module = scrub(doctype), scrub(module)
  50. return '%s.doctype.%s.%s%s' % (_module, _doctype, prefix, _doctype)
  51. def load_doctype_module(doctype, module=None, prefix=""):
  52. import webnotes
  53. from webnotes.modules import scrub, get_doctype_module
  54. if not module:
  55. module = get_doctype_module(doctype) or "core"
  56. try:
  57. module = __import__(get_module_name(doctype, module, prefix), fromlist=[''])
  58. return module
  59. except ImportError, e:
  60. # webnotes.errprint(webnotes.getTraceback())
  61. return None
  62. def get_obj(dt = None, dn = None, doc=None, doclist=[], with_children = 0):
  63. import webnotes.model.doc
  64. if dt:
  65. if isinstance(dt, list):
  66. return get_server_obj(dt[0], dt)
  67. if isinstance(dt, webnotes.model.doc.Document):
  68. return get_server_obj(dt, [dt])
  69. if not dn:
  70. dn = dt
  71. if with_children:
  72. doclist = webnotes.model.doc.get(dt, dn, from_controller=1)
  73. else:
  74. doclist = webnotes.model.doc.get(dt, dn, with_children = 0, from_controller=1)
  75. return get_server_obj(doclist[0], doclist)
  76. else:
  77. return get_server_obj(doc, doclist)
  78. def run_server_obj(server_obj, method_name, arg=None):
  79. """
  80. Executes a method (`method_name`) from the given object (`server_obj`)
  81. """
  82. if server_obj and hasattr(server_obj, method_name):
  83. if arg:
  84. return getattr(server_obj, method_name)(arg)
  85. else:
  86. return getattr(server_obj, method_name)()
  87. else:
  88. raise Exception, 'No method %s' % method_name
  89. def get_code(module, dt, dn, extn, fieldname=None):
  90. from webnotes.modules import scrub, get_module_path
  91. import os, webnotes
  92. # get module (if required)
  93. if not module:
  94. module = webnotes.conn.get_value(dt, dn, 'module')
  95. # no module, quit
  96. if not module:
  97. return ''
  98. # file names
  99. if dt in ('Page','Doctype'):
  100. dt, dn = scrub(dt), scrub(dn)
  101. # get file name
  102. fname = dn + '.' + extn
  103. # code
  104. code = ''
  105. try:
  106. file = open(os.path.join(get_module_path(scrub(module)), dt, dn, fname), 'r')
  107. code = file.read()
  108. file.close()
  109. except IOError, e:
  110. # no file, try from db
  111. if fieldname:
  112. code = webnotes.conn.get_value(dt, dn, fieldname)
  113. return code