選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

205 行
12 KiB

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Server Side Scripts &mdash; Documentation</title>
  7. <link rel="stylesheet" href="_static/sphinxdoc.css" type="text/css" />
  8. <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
  9. <script type="text/javascript">
  10. var DOCUMENTATION_OPTIONS = {
  11. URL_ROOT: '',
  12. VERSION: '',
  13. COLLAPSE_MODINDEX: false,
  14. FILE_SUFFIX: '.html',
  15. HAS_SOURCE: true
  16. };
  17. </script>
  18. <script type="text/javascript" src="_static/jquery.js"></script>
  19. <script type="text/javascript" src="_static/doctools.js"></script>
  20. <link rel="top" title="Documentation" href="index.html" />
  21. <link rel="up" title="Writing Scripts" href="writing_scripts.html" />
  22. <link rel="next" title="Creating Pages" href="creating_pages.html" />
  23. <link rel="prev" title="Writing Scripts" href="writing_scripts.html" />
  24. </head>
  25. <body>
  26. <div style="background-color: #FFF; text-align: left; padding: 8px 0px"><img src="_static/banner300910.gif"></div>
  27. <div class="related">
  28. <h3>Navigation</h3>
  29. <ul>
  30. <li class="right" style="margin-right: 10px">
  31. <a href="genindex.html" title="General Index"
  32. accesskey="I">index</a></li>
  33. <li class="right" >
  34. <a href="modindex.html" title="Global Module Index"
  35. accesskey="M">modules</a> |</li>
  36. <li class="right" >
  37. <a href="creating_pages.html" title="Creating Pages"
  38. accesskey="N">next</a> |</li>
  39. <li class="right" >
  40. <a href="writing_scripts.html" title="Writing Scripts"
  41. accesskey="P">previous</a> |</li>
  42. <li><a href="index.html">Documentation</a> &raquo;</li>
  43. <li><a href="build_app.html" >2. Building an Application</a> &raquo;</li>
  44. <li><a href="writing_scripts.html" accesskey="U">Writing Scripts</a> &raquo;</li>
  45. </ul>
  46. </div>
  47. <div class="sphinxsidebar">
  48. <div class="sphinxsidebarwrapper">
  49. <h3><a href="index.html">Table Of Contents</a></h3>
  50. <ul>
  51. <li><a class="reference external" href="">Server Side Scripts</a><ul>
  52. <li><a class="reference external" href="#introduction">Introduction</a></li>
  53. <li><a class="reference external" href="#declaring-a-class">Declaring a Class</a></li>
  54. <li><a class="reference external" href="#validate-method">validate method</a></li>
  55. <li><a class="reference external" href="#on-update-on-submit-on-cancel-methods">on_update, on_submit, on_cancel methods</a></li>
  56. <li><a class="reference external" href="#adding-child-records">Adding Child Records</a></li>
  57. <li><a class="reference external" href="#debugging">Debugging</a></li>
  58. </ul>
  59. </li>
  60. </ul>
  61. <h4>Previous topic</h4>
  62. <p class="topless"><a href="writing_scripts.html"
  63. title="previous chapter">Writing Scripts</a></p>
  64. <h4>Next topic</h4>
  65. <p class="topless"><a href="creating_pages.html"
  66. title="next chapter">Creating Pages</a></p>
  67. <h3>This Page</h3>
  68. <ul class="this-page-menu">
  69. <li><a href="_sources/server_side_script.txt"
  70. rel="nofollow">Show Source</a></li>
  71. </ul>
  72. <div id="searchbox" style="display: none">
  73. <h3>Quick search</h3>
  74. <form class="search" action="search.html" method="get">
  75. <input type="text" name="q" size="18" />
  76. <input type="submit" value="Go" />
  77. <input type="hidden" name="check_keywords" value="yes" />
  78. <input type="hidden" name="area" value="default" />
  79. </form>
  80. <p class="searchtip" style="font-size: 90%">
  81. Enter search terms or a module, class or function name.
  82. </p>
  83. </div>
  84. <script type="text/javascript">$('#searchbox').show(0);</script>
  85. </div>
  86. </div>
  87. <div class="document">
  88. <div class="documentwrapper">
  89. <div class="bodywrapper">
  90. <div class="body">
  91. <div class="section" id="server-side-scripts">
  92. <h1>Server Side Scripts<a class="headerlink" href="#server-side-scripts" title="Permalink to this headline">¶</a></h1>
  93. <div class="section" id="introduction">
  94. <h2>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
  95. <p>On the server-side, scripts are embedded in DocTypes. All scripts have to reside in classes in the DocTypes.</p>
  96. <p>To add a server script, open a DocType you want to attach the script to and open the &#8220;Server Script&#8221; tab.</p>
  97. <div class="admonition note">
  98. <p class="first admonition-title">Note</p>
  99. <p class="last">If you do not want the server script to be attached to any particular DocType, or call it from many
  100. DocTypes, you can create a &#8220;Single&#8221; DocType. Then using the get_obj method, you can call it from
  101. anywhere. More about it later.</p>
  102. </div>
  103. </div>
  104. <div class="section" id="declaring-a-class">
  105. <h2>Declaring a Class<a class="headerlink" href="#declaring-a-class" title="Permalink to this headline">¶</a></h2>
  106. <p>Server Side methods (functions) always reside in a &#8220;DocType&#8221; class, hence all your DocType classes will
  107. be declared in the following manner:</p>
  108. <div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">DocType</span><span class="p">:</span>
  109. <span class="c"># standard constructor</span>
  110. <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">doc</span><span class="p">,</span> <span class="n">doclist</span><span class="p">):</span>
  111. <span class="bp">self</span><span class="o">.</span><span class="n">doc</span> <span class="o">=</span> <span class="n">doc</span>
  112. <span class="bp">self</span><span class="o">.</span><span class="n">doclist</span> <span class="o">=</span> <span class="n">doclist</span>
  113. </pre></div>
  114. </div>
  115. <p>Let us see this constructor line by line</p>
  116. <ol class="arabic simple">
  117. <li><strong>class DocType</strong> - This is the standard declaration of a class (for any DocType, the class will be labeled DocType)</li>
  118. <li><strong>def __init__(self, doc, doclist):</strong> - This is the constructor. The object will be constructed by the framework
  119. and the framework will supply the data record &#8220;doc&#8221; and a bundle of data-records including child records
  120. of this object in &#8220;doclist&#8221;</li>
  121. <li><strong>self.doc = doc</strong> - Set class property &#8220;doc&#8221; as the data object</li>
  122. <li><strong>self.doclist = doclist</strong> - Set the class property &#8220;doclist&#8221; as the list of child records</li>
  123. </ol>
  124. </div>
  125. <div class="section" id="validate-method">
  126. <h2>validate method<a class="headerlink" href="#validate-method" title="Permalink to this headline">¶</a></h2>
  127. <p>The validate method is called just before the user saves a record using the &#8220;Save&#8221; button. To stop the user
  128. from saving, raise an Exception</p>
  129. <p>Example:</p>
  130. <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">validate</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  131. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">doc</span><span class="o">.</span><span class="n">start_date</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">doc</span><span class="o">.</span><span class="n">finish_date</span><span class="p">:</span>
  132. <span class="n">msgprint</span><span class="p">(</span><span class="s">&#39;Start date must be before finish date&#39;</span><span class="p">)</span>
  133. <span class="k">raise</span> <span class="ne">Exception</span>
  134. </pre></div>
  135. </div>
  136. </div>
  137. <div class="section" id="on-update-on-submit-on-cancel-methods">
  138. <h2>on_update, on_submit, on_cancel methods<a class="headerlink" href="#on-update-on-submit-on-cancel-methods" title="Permalink to this headline">¶</a></h2>
  139. <p>These methods are called at various stages of saving a document, as defined in <a class="reference external" href="save_submit.html"><em>Save-Submit-Cancel Pattern</em></a></p>
  140. <p>The on_update method is called after the document values are saved in the database. If you raise an
  141. Exception in any of these methods, the entire transaction will be rolled back.</p>
  142. </div>
  143. <div class="section" id="adding-child-records">
  144. <h2>Adding Child Records<a class="headerlink" href="#adding-child-records" title="Permalink to this headline">¶</a></h2>
  145. <p>Child records can be added on the server side by the addchild method:</p>
  146. <div class="highlight-python"><div class="highlight"><pre><span class="n">addchild</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">fieldname</span><span class="p">,</span> <span class="n">childtype</span> <span class="o">=</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="n">local</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">doclist</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
  147. </pre></div>
  148. </div>
  149. <p>here is an example:</p>
  150. <div class="highlight-python"><div class="highlight"><pre><span class="n">c</span> <span class="o">=</span> <span class="n">Document</span><span class="p">(</span><span class="s">&#39;Contact&#39;</span><span class="p">,</span><span class="s">&#39;ABC&#39;</span><span class="p">)</span>
  151. <span class="n">d</span> <span class="o">=</span> <span class="n">addchild</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="s">&#39;contact_updates&#39;</span><span class="p">,</span> <span class="s">&#39;Contact Update&#39;</span><span class="p">,</span> <span class="n">local</span> <span class="o">=</span> <span class="mi">1</span><span class="p">)</span>
  152. <span class="n">d</span><span class="o">.</span><span class="n">last_updated</span> <span class="o">=</span> <span class="s">&#39;Phone call&#39;</span>
  153. <span class="n">d</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
  154. </pre></div>
  155. </div>
  156. </div>
  157. <div class="section" id="debugging">
  158. <h2>Debugging<a class="headerlink" href="#debugging" title="Permalink to this headline">¶</a></h2>
  159. <p>For de-bugging on the server side, you can</p>
  160. <ol class="arabic simple">
  161. <li>Print messages via msgprint(message)</li>
  162. <li>Print error messages via errprint(message)</li>
  163. </ol>
  164. <p>The full traceback of your error can be seen in <strong>Tools -&gt; Error Console</strong></p>
  165. </div>
  166. </div>
  167. </div>
  168. </div>
  169. </div>
  170. <div class="clearer"></div>
  171. </div>
  172. <div class="related">
  173. <h3>Navigation</h3>
  174. <ul>
  175. <li class="right" style="margin-right: 10px">
  176. <a href="genindex.html" title="General Index"
  177. >index</a></li>
  178. <li class="right" >
  179. <a href="modindex.html" title="Global Module Index"
  180. >modules</a> |</li>
  181. <li class="right" >
  182. <a href="creating_pages.html" title="Creating Pages"
  183. >next</a> |</li>
  184. <li class="right" >
  185. <a href="writing_scripts.html" title="Writing Scripts"
  186. >previous</a> |</li>
  187. <li><a href="index.html">Documentation</a> &raquo;</li>
  188. <li><a href="build_app.html" >2. Building an Application</a> &raquo;</li>
  189. <li><a href="writing_scripts.html" >Writing Scripts</a> &raquo;</li>
  190. </ul>
  191. </div>
  192. <div class="footer">
  193. &copy; Copyright 2010, Rushabh Mehta.
  194. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.3.
  195. </div>
  196. </body>
  197. </html>