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

docs.dev.quickstart.md 7.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. ---
  2. {
  3. "_label": "Quickstart"
  4. }
  5. ---
  6. ### Preamble
  7. wnframework is, a Python based, meta-data driven framework. The framework implements
  8. its own object-relational model (ORM) and provides a rich client interface based on
  9. Javascript. It is primararily used to develop [ERPNext](https://github.com/webnotes/erpnext)
  10. To develop on wnframework, you must have a basic understanding of how web applications
  11. and client-server architectures work. On the server-side, requests are handled by Python
  12. modules via CGI. So each request is a new thread and there is no state preservation on
  13. the server. Session data is stored in memcached server.
  14. WNFramework also has way you metadata is defined, called a `DocType`. Everything
  15. object in the system like a Customer or Journal Voucher is a `DocType`.
  16. Overall, be prepared for a slight learning curve. A lot of the inner code / design
  17. is not very elegant and you might encounter spaghetti at certain places. We are working
  18. to reduce all of that.
  19. ---
  20. ## Meta data
  21. Base model in wnframework is called a `DocType`. A `DocType` represents a database table,
  22. a web form and a controller to execute business logic. In classical MVC terms it is all
  23. three model, view and controller to an extent.
  24. `DocType` objects have `DocField`s that are properties of the model.
  25. ---
  26. ## Client-Server Setup
  27. Let us understand how to setup web folders via ERPNext
  28. An ERPNext setup contains 2 repositories [erpnext](/webnotes/erpnext) and
  29. [wnframework](/webnotes/wnframework). In the main folder of the erpnext setup there
  30. are 3 folders:
  31. + lib
  32. + app
  33. + public
  34. The **lib** folder represents *wnframework*, the **app** folder represents *erpnext* and
  35. the **public** folder is served on the web.
  36. To build the public folder for the first time, run this utility from the base folder:
  37. `$ lib/wnf.py -b`
  38. All web pages are served by `public/web.py` and all data requests are served
  39. by `public/server.py`
  40. The server-side libraries are in `lib/webnotes` and client-side libraries are
  41. in `lib/public/wn` folders.
  42. ### Requests & Routings
  43. There are 2 types of requests, requests for web pages (when the user is not logged in) and
  44. data requests when the user is logged in. Let us see data requests:
  45. All data requests are made on `public/server.py`. The method and parameters are passed as
  46. form parameters.
  47. The `cmd` paramter represents the python method to be executed. This is the "routing" used
  48. in wnframwork. Use the `@webnotes.whitelist()` decorator to whitelist a particular method
  49. to be accessible by the web.
  50. For example, the request:
  51. `server.py?cmd=accounts.utils.get_account&account_name=Test`
  52. will call the `get_account` method in `app/accounts/utils.py`.
  53. #### Repsonse
  54. The return to that will be sent as a JSON object
  55. {
  56. "message": "returned by get_account",
  57. "server_messages":"Any popup messages to be displayed",
  58. "exc": "Any exceptions encountered"
  59. }
  60. Once the control is passed on to the method, the response is sent back via JSON.
  61. ---
  62. ## Front End
  63. The front end is a Javascript based client application. You can login by opening the login
  64. page from your browser. If you have setup your apache routes correctly, just go to
  65. `localhost/public/login` or equivalent to see the login page. This actually translates
  66. into `public/web.py?page=login`.
  67. Once you login, you will be redirected to `app.html` that fires up the application front-end.
  68. ### URL routing:
  69. Different pages / objects are accessed by url fragments `#`
  70. #### Forms
  71. All objects are accessible via `#Form/[DocType]/[Doument Name]` on the URL.
  72. To open the customer **DocType**, you can go to `#Form/DocType/Customer` or to open
  73. a Customer, **Customer A**, go to `#Form/Customer/Customer A`
  74. #### Pages
  75. Static pages in the application are accessed by their name. For example, the home
  76. page called `desktop` can be accessed by `#desktop`
  77. #### Client Application
  78. The client application is bunch of js libraries that help in navigation, rendering
  79. forms, reports and other components. The application code in `public/js/all-app.js`
  80. is built by combining files specified in `lib/public/build.json` and `app/public/build.json`.
  81. To rebuild the client application after making a change, call `lib/wnf.py -b` from the
  82. command line.
  83. ---
  84. ## Application / Module Development
  85. ### Creating / Editing DocTypes
  86. To create or edit the **DocType** "schema" you will have to fire the front-end via a
  87. web-browser and login as Administrator. To open a **DocType**,
  88. go to Document > Search > DocType and select the **DocType** to edit.
  89. The **DocType** form should be self explanatory. It has a list of fields that are
  90. used for both the database table and form. Special fields like `Column Break` and
  91. `Section Break` are present to make the form layout that is processed sequentially.
  92. DocType is discovered via permissions (`DocPerm`) and by URL routes.
  93. Once you save a **DocType**, the database schema is automatically update, while
  94. developing, you should fire up a mysql command-line or viewer to see the impact
  95. of your database changes.
  96. ### Adding code to DocTypes
  97. You can add business logic by writing event code on both client and server side.
  98. The server side events are written in Python and client side events are written in
  99. Javascript.
  100. The files from where these events are picked up are in the module folders in the
  101. repositories. Apart from the `core` module, all modules are parts
  102. of **erpnext** (`app` folder). Each DocType has its own folder in the module in a
  103. folder called `doctype`. If you browse the files of **erpnext**, you should be able
  104. to locate these files easily.
  105. #### Server-side modules
  106. For example, the server-side script for DocType **Account** in module **Accounts**
  107. will be present in the folder `app/accounts/doctype/account/account.py`
  108. The events are declared as a part of class called `DocType`. In the `DocType`
  109. class there are two main useful properties:
  110. - `doc`: Represents the main record.
  111. - `doclist`: Represents list of records (including child records) that are associated
  112. with this DocType. For example the `doclist` of **Sales Order** will have the main record
  113. and all **Sales Order Item** records.
  114. The main serverside events are:
  115. - `validate`: Called before the `INSERT` or `UPDATE` method is called.
  116. - `on_update`: Called after saving.
  117. - `on_submit`: Called after submission
  118. - `on_cancel`: Called after cancellation.
  119. See a sample server side file for more info!
  120. ---
  121. ## Custom UI: Pages
  122. Custom UI like **Chart of Accounts** etc, is made by Pages. Pages are free form virtual
  123. pages in that are rendered on the client side. A page can have an `.html` (layout),
  124. `.py` (server calls), `.js` (user interface) and `.css` (style) components.
  125. Understand how pages work, it is best to open an existing page and see how it works.
  126. ---
  127. ## Patching & Deployment
  128. Data / schema changes are done to wnframwork via patches released in the `app/patches`
  129. module (see erpnext folder for more details). To run all latest patches that have not
  130. been executed, run `lib/wnf.py -l`
  131. wnframework deployment is done by the `lib/wnf.py` utility.
  132. See `lib/wnf.py --help` for more help.
  133. _Good luck!_