Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

69 строки
2.3 KiB

  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. import webnotes
  16. from webnotes.modules import get_doctype_module
  17. import webnotes.model.doc
  18. def get_obj(dt = None, dn = None, doc=None, doclist=None, with_children = 0):
  19. import webnotes.model.doc
  20. if not doclist: doclist = []
  21. if dt:
  22. if isinstance(dt, list):
  23. return get_server_obj(dt[0], dt)
  24. if isinstance(dt, webnotes.model.doc.Document):
  25. return get_server_obj(dt, [dt])
  26. if not dn:
  27. dn = dt
  28. if with_children:
  29. doclist = webnotes.model.doc.get(dt, dn, from_controller=1)
  30. else:
  31. doclist = webnotes.model.doc.get(dt, dn, with_children = 0, from_controller=1)
  32. return get_server_obj(doclist[0], doclist)
  33. else:
  34. return get_server_obj(doc, doclist)
  35. def get_server_obj(doc, doclist = [], basedoctype = ''):
  36. # for test
  37. module = get_doctype_module(doc.doctype)
  38. return load_doctype_module(doc.doctype, module).DocType(doc, doclist)
  39. def load_doctype_module(doctype, module=None, prefix=""):
  40. if not module:
  41. module = get_doctype_module(doctype)
  42. return webnotes.get_module(get_module_name(doctype, module, prefix))
  43. def get_module_name(doctype, module, prefix=""):
  44. from webnotes.modules import scrub
  45. return '{app}.{module}.doctype.{doctype}.{prefix}{doctype}'.format(\
  46. app = scrub(webnotes.local.module_app[scrub(module)]),
  47. module = scrub(module), doctype = scrub(doctype), prefix=prefix)
  48. def run_server_obj(server_obj, method_name, arg=None):
  49. """
  50. Executes a method (`method_name`) from the given object (`server_obj`)
  51. """
  52. if server_obj and hasattr(server_obj, method_name):
  53. if arg:
  54. return getattr(server_obj, method_name)(arg)
  55. else:
  56. return getattr(server_obj, method_name)()
  57. else:
  58. raise Exception, 'No method %s' % method_name