您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

87 行
2.7 KiB

  1. Server Side Scripts
  2. ===================
  3. Introduction
  4. ------------
  5. On the server-side, scripts are embedded in DocTypes. All scripts have to reside in classes in the DocTypes.
  6. To add a server script, open a DocType you want to attach the script to and open the "Server Script" tab.
  7. .. note::
  8. If you do not want the server script to be attached to any particular DocType, or call it from many
  9. DocTypes, you can create a "Single" DocType. Then using the get_obj method, you can call it from
  10. anywhere. More about it later.
  11. Declaring a Class
  12. -----------------
  13. Server Side methods (functions) always reside in a "DocType" class, hence all your DocType classes will
  14. be declared in the following manner::
  15. class DocType:
  16. # standard constructor
  17. def __init__(self, doc, doclist):
  18. self.doc = doc
  19. self.doclist = doclist
  20. Let us see this constructor line by line
  21. #. **class DocType** - This is the standard declaration of a class (for any DocType, the class will be labeled DocType)
  22. #. **def __init__(self, doc, doclist):** - This is the constructor. The object will be constructed by the framework
  23. and the framework will supply the data record "doc" and a bundle of data-records including child records
  24. of this object in "doclist"
  25. #. **self.doc = doc** - Set class property "doc" as the data object
  26. #. **self.doclist = doclist** - Set the class property "doclist" as the list of child records
  27. validate method
  28. ---------------
  29. The validate method is called just before the user saves a record using the "Save" button. To stop the user
  30. from saving, raise an Exception
  31. Example::
  32. def validate(self):
  33. if self.doc.start_date > self.doc.finish_date:
  34. msgprint('Start date must be before finish date')
  35. raise Exception
  36. on_update, on_submit, on_cancel methods
  37. ---------------------------------------
  38. These methods are called at various stages of saving a document, as defined in :doc:`save_submit`
  39. The on_update method is called after the document values are saved in the database. If you raise an
  40. Exception in any of these methods, the entire transaction will be rolled back.
  41. Adding Child Records
  42. --------------------
  43. Child records can be added on the server side by the addchild method::
  44. addchild(parent, fieldname, childtype = '', local=0, doclist=None)
  45. here is an example::
  46. c = Document('Contact','ABC')
  47. d = addchild(c, 'contact_updates', 'Contact Update', local = 1)
  48. d.last_updated = 'Phone call'
  49. d.save(1)
  50. Debugging
  51. ---------
  52. For de-bugging on the server side, you can
  53. #. Print messages via msgprint(message)
  54. #. Print error messages via errprint(message)
  55. The full traceback of your error can be seen in **Tools -> Error Console**