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 3.9 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  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