292 lines
No EOL
21 KiB
HTML
292 lines
No EOL
21 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
|
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
|
|
<title>accounting.web — Accounting API 0.1-beta documentation</title>
|
|
|
|
<link rel="stylesheet" href="../../_static/pydoctheme.css" type="text/css" />
|
|
<link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
|
|
|
|
<script type="text/javascript">
|
|
var DOCUMENTATION_OPTIONS = {
|
|
URL_ROOT: '../../',
|
|
VERSION: '0.1-beta',
|
|
COLLAPSE_INDEX: false,
|
|
FILE_SUFFIX: '.html',
|
|
HAS_SOURCE: true
|
|
};
|
|
</script>
|
|
<script type="text/javascript" src="../../_static/jquery.js"></script>
|
|
<script type="text/javascript" src="../../_static/underscore.js"></script>
|
|
<script type="text/javascript" src="../../_static/doctools.js"></script>
|
|
<link rel="top" title="Accounting API 0.1-beta documentation" href="../../index.html" />
|
|
<link rel="up" title="accounting" href="../accounting.html" />
|
|
</head>
|
|
<body>
|
|
<div class="related">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../../genindex.html" title="General Index"
|
|
accesskey="I">index</a></li>
|
|
<li class="right" >
|
|
<a href="../../py-modindex.html" title="Python Module Index"
|
|
>modules</a> |</li>
|
|
<li><a href="../../index.html">Accounting API 0.1-beta documentation</a> »</li>
|
|
<li><a href="../index.html" >Module code</a> »</li>
|
|
<li><a href="../accounting.html" accesskey="U">accounting</a> »</li>
|
|
</ul>
|
|
</div>
|
|
|
|
<div class="document">
|
|
<div class="documentwrapper">
|
|
<div class="bodywrapper">
|
|
<div class="body">
|
|
|
|
<h1>Source code for accounting.web</h1><div class="highlight"><pre>
|
|
<span class="sd">'''</span>
|
|
<span class="sd">This module contains the high-level webservice logic such as the Flask setup</span>
|
|
<span class="sd">and the Flask endpoints.</span>
|
|
<span class="sd">'''</span>
|
|
<span class="kn">import</span> <span class="nn">sys</span>
|
|
<span class="kn">import</span> <span class="nn">logging</span>
|
|
<span class="kn">import</span> <span class="nn">argparse</span>
|
|
|
|
<span class="kn">from</span> <span class="nn">flask</span> <span class="kn">import</span> <span class="n">Flask</span><span class="p">,</span> <span class="n">jsonify</span><span class="p">,</span> <span class="n">request</span>
|
|
|
|
<span class="kn">from</span> <span class="nn">accounting</span> <span class="kn">import</span> <span class="n">Ledger</span>
|
|
<span class="kn">from</span> <span class="nn">accounting.transport</span> <span class="kn">import</span> <span class="n">AccountingEncoder</span><span class="p">,</span> <span class="n">AccountingDecoder</span>
|
|
<span class="kn">from</span> <span class="nn">accounting.exceptions</span> <span class="kn">import</span> <span class="n">AccountingException</span>
|
|
<span class="kn">from</span> <span class="nn">accounting.decorators</span> <span class="kn">import</span> <span class="n">jsonify_exceptions</span>
|
|
|
|
|
|
<span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="s">'accounting'</span><span class="p">)</span>
|
|
<span class="n">app</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">from_pyfile</span><span class="p">(</span><span class="s">'config.py'</span><span class="p">)</span>
|
|
|
|
<span class="n">ledger</span> <span class="o">=</span> <span class="bp">None</span>
|
|
|
|
<span class="nd">@app.before_request</span>
|
|
<div class="viewcode-block" id="init_ledger"><a class="viewcode-back" href="../../api/accounting.html#accounting.web.init_ledger">[docs]</a><span class="k">def</span> <span class="nf">init_ledger</span><span class="p">():</span>
|
|
<span class="sd">'''</span>
|
|
<span class="sd"> :py:meth:`flask.Flask.before_request`-decorated method that initializes an</span>
|
|
<span class="sd"> :py:class:`accounting.Ledger` object.</span>
|
|
<span class="sd"> '''</span>
|
|
<span class="k">global</span> <span class="n">ledger</span>
|
|
<span class="n">ledger</span> <span class="o">=</span> <span class="n">Ledger</span><span class="p">(</span><span class="n">ledger_file</span><span class="o">=</span><span class="n">app</span><span class="o">.</span><span class="n">config</span><span class="p">[</span><span class="s">'LEDGER_FILE'</span><span class="p">])</span>
|
|
|
|
|
|
<span class="c"># These will convert output from our internal classes to JSON and back</span></div>
|
|
<span class="n">app</span><span class="o">.</span><span class="n">json_encoder</span> <span class="o">=</span> <span class="n">AccountingEncoder</span>
|
|
<span class="n">app</span><span class="o">.</span><span class="n">json_decoder</span> <span class="o">=</span> <span class="n">AccountingDecoder</span>
|
|
|
|
|
|
<span class="nd">@app.route</span><span class="p">(</span><span class="s">'/'</span><span class="p">)</span>
|
|
<div class="viewcode-block" id="index"><a class="viewcode-back" href="../../api/accounting.html#accounting.web.index">[docs]</a><span class="k">def</span> <span class="nf">index</span><span class="p">():</span>
|
|
<span class="sd">''' Hello World! '''</span>
|
|
<span class="k">return</span> <span class="s">'Hello World!'</span>
|
|
|
|
</div>
|
|
<span class="nd">@app.route</span><span class="p">(</span><span class="s">'/balance'</span><span class="p">)</span>
|
|
<div class="viewcode-block" id="balance_report"><a class="viewcode-back" href="../../api/accounting.html#accounting.web.balance_report">[docs]</a><span class="k">def</span> <span class="nf">balance_report</span><span class="p">():</span>
|
|
<span class="sd">'''</span>
|
|
<span class="sd"> Returns the JSON-serialized result of :meth:`accounting.Ledger.bal`</span>
|
|
<span class="sd"> '''</span>
|
|
<span class="n">report_data</span> <span class="o">=</span> <span class="n">ledger</span><span class="o">.</span><span class="n">bal</span><span class="p">()</span>
|
|
|
|
<span class="k">return</span> <span class="n">jsonify</span><span class="p">(</span><span class="n">balance_report</span><span class="o">=</span><span class="n">report_data</span><span class="p">)</span>
|
|
</div>
|
|
<span class="nd">@app.route</span><span class="p">(</span><span class="s">'/transaction'</span><span class="p">,</span> <span class="n">methods</span><span class="o">=</span><span class="p">[</span><span class="s">'GET'</span><span class="p">])</span>
|
|
<div class="viewcode-block" id="transaction_get"><a class="viewcode-back" href="../../api/accounting.html#accounting.web.transaction_get">[docs]</a><span class="k">def</span> <span class="nf">transaction_get</span><span class="p">():</span>
|
|
<span class="sd">'''</span>
|
|
<span class="sd"> Returns the JSON-serialized output of :meth:`accounting.Ledger.reg`</span>
|
|
<span class="sd"> '''</span>
|
|
<span class="k">return</span> <span class="n">jsonify</span><span class="p">(</span><span class="n">transactions</span><span class="o">=</span><span class="n">ledger</span><span class="o">.</span><span class="n">reg</span><span class="p">())</span>
|
|
</div>
|
|
<span class="nd">@app.route</span><span class="p">(</span><span class="s">'/transaction'</span><span class="p">,</span> <span class="n">methods</span><span class="o">=</span><span class="p">[</span><span class="s">'POST'</span><span class="p">])</span>
|
|
<span class="nd">@jsonify_exceptions</span>
|
|
<div class="viewcode-block" id="transaction_post"><a class="viewcode-back" href="../../api/accounting.html#accounting.web.transaction_post">[docs]</a><span class="k">def</span> <span class="nf">transaction_post</span><span class="p">():</span>
|
|
<span class="sd">'''</span>
|
|
<span class="sd"> REST/JSON endpoint for transactions.</span>
|
|
|
|
<span class="sd"> Current state:</span>
|
|
|
|
<span class="sd"> Takes a POST request with a ``transactions`` JSON payload and writes it to</span>
|
|
<span class="sd"> the ledger file.</span>
|
|
|
|
<span class="sd"> Requires the ``transactions`` payload to be __type__-annotated:</span>
|
|
|
|
<span class="sd"> .. code-block:: json</span>
|
|
|
|
<span class="sd"> {</span>
|
|
<span class="sd"> "transactions": [</span>
|
|
<span class="sd"> {</span>
|
|
<span class="sd"> "__type__": "Transaction",</span>
|
|
<span class="sd"> "date": "2013-01-01",</span>
|
|
<span class="sd"> "payee": "Kindly T. Donor",</span>
|
|
<span class="sd"> "postings": [</span>
|
|
<span class="sd"> {</span>
|
|
<span class="sd"> "__type__": "Posting",</span>
|
|
<span class="sd"> "account": "Income:Foo:Donation",</span>
|
|
<span class="sd"> "amount": {</span>
|
|
<span class="sd"> "__type__": "Amount",</span>
|
|
<span class="sd"> "amount": "-100",</span>
|
|
<span class="sd"> "symbol": "$"</span>
|
|
<span class="sd"> }</span>
|
|
<span class="sd"> },</span>
|
|
<span class="sd"> {</span>
|
|
<span class="sd"> "__type__": "Posting",</span>
|
|
<span class="sd"> "account": "Assets:Checking",</span>
|
|
<span class="sd"> "amount": {</span>
|
|
<span class="sd"> "__type__": "Amount",</span>
|
|
<span class="sd"> "amount": "100",</span>
|
|
<span class="sd"> "symbol": "$"</span>
|
|
<span class="sd"> }</span>
|
|
<span class="sd"> }</span>
|
|
<span class="sd"> ]</span>
|
|
<span class="sd"> }</span>
|
|
<span class="sd"> }</span>
|
|
|
|
<span class="sd"> becomes::</span>
|
|
|
|
<span class="sd"> 2013-01-01 Kindly T. Donor</span>
|
|
<span class="sd"> Income:Foo:Donation $ -100</span>
|
|
<span class="sd"> Assets:Checking $ 100</span>
|
|
<span class="sd"> '''</span>
|
|
<span class="n">transactions</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">json</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'transactions'</span><span class="p">)</span>
|
|
|
|
<span class="k">if</span> <span class="ow">not</span> <span class="n">transactions</span><span class="p">:</span>
|
|
<span class="k">raise</span> <span class="n">AccountingException</span><span class="p">(</span><span class="s">'No transaction data provided'</span><span class="p">)</span>
|
|
|
|
<span class="k">for</span> <span class="n">transaction</span> <span class="ow">in</span> <span class="n">transactions</span><span class="p">:</span>
|
|
<span class="n">ledger</span><span class="o">.</span><span class="n">add_transaction</span><span class="p">(</span><span class="n">transaction</span><span class="p">)</span>
|
|
|
|
<span class="k">return</span> <span class="n">jsonify</span><span class="p">(</span><span class="n">foo</span><span class="o">=</span><span class="s">'bar'</span><span class="p">)</span>
|
|
|
|
</div>
|
|
<span class="nd">@app.route</span><span class="p">(</span><span class="s">'/parse-json'</span><span class="p">,</span> <span class="n">methods</span><span class="o">=</span><span class="p">[</span><span class="s">'POST'</span><span class="p">])</span>
|
|
<div class="viewcode-block" id="parse_json"><a class="viewcode-back" href="../../api/accounting.html#accounting.web.parse_json">[docs]</a><span class="k">def</span> <span class="nf">parse_json</span><span class="p">():</span>
|
|
<span class="sd">r'''</span>
|
|
<span class="sd"> Parses a __type__-annotated JSON payload and debug-logs the decoded version</span>
|
|
<span class="sd"> of it.</span>
|
|
|
|
<span class="sd"> Example:</span>
|
|
|
|
<span class="sd"> .. code-block:: bash</span>
|
|
|
|
<span class="sd"> wget http://127.0.0.1:5000/balance -O balance.json</span>
|
|
<span class="sd"> curl -X POST -H 'Content-Type: application/json' -d @balance.json \</span>
|
|
<span class="sd"> http://127.0.0.1/parse-json</span>
|
|
<span class="sd"> # Logging output (linebreaks added for clarity)</span>
|
|
<span class="sd"> # DEBUG:accounting:json data: {'balance_report':</span>
|
|
<span class="sd"> # [<Account "None" [</span>
|
|
<span class="sd"> # <Amount $ 0>, <Amount KARMA 0>]</span>
|
|
<span class="sd"> # [<Account "Assets" [</span>
|
|
<span class="sd"> # <Amount $ 50>, <Amount KARMA 10>]</span>
|
|
<span class="sd"> # [<Account "Assets:Checking" [</span>
|
|
<span class="sd"> # <Amount $ 50>] []>,</span>
|
|
<span class="sd"> # <Account "Assets:Karma Account" [</span>
|
|
<span class="sd"> # <Amount KARMA 10>] []>]>,</span>
|
|
<span class="sd"> # <Account "Expenses" [</span>
|
|
<span class="sd"> # <Amount $ 500>]</span>
|
|
<span class="sd"> # [<Account "Expenses:Blah" [</span>
|
|
<span class="sd"> # <Amount $ 250>]</span>
|
|
<span class="sd"> # [<Account "Expenses:Blah:Hosting" [</span>
|
|
<span class="sd"> # <Amount $ 250>] []>]>,</span>
|
|
<span class="sd"> # <Account "Expenses:Foo" [</span>
|
|
<span class="sd"> # <Amount $ 250>] [</span>
|
|
<span class="sd"> # <Account "Expenses:Foo:Hosting" [</span>
|
|
<span class="sd"> # <Amount $ 250>] []>]>]>,</span>
|
|
<span class="sd"> # <Account "Income" [</span>
|
|
<span class="sd"> # <Amount $ -550>,</span>
|
|
<span class="sd"> # <Amount KARMA -10>]</span>
|
|
<span class="sd"> # [<Account "Income:Donation" [</span>
|
|
<span class="sd"> # <Amount $ -50>] []>,</span>
|
|
<span class="sd"> # <Account "Income:Foo" [</span>
|
|
<span class="sd"> # <Amount $ -500>]</span>
|
|
<span class="sd"> # [<Account "Income:Foo:Donation" [</span>
|
|
<span class="sd"> # <Amount $ -500>] []>]>,</span>
|
|
<span class="sd"> # <Account "Income:Karma" [</span>
|
|
<span class="sd"> # <Amount KARMA -10>] []>]>]>]}</span>
|
|
<span class="sd"> '''</span>
|
|
<span class="n">app</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">'json data: </span><span class="si">%s</span><span class="s">'</span><span class="p">,</span> <span class="n">request</span><span class="o">.</span><span class="n">json</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="n">jsonify</span><span class="p">(</span><span class="n">foo</span><span class="o">=</span><span class="s">'bar'</span><span class="p">)</span>
|
|
|
|
</div>
|
|
<span class="nd">@app.route</span><span class="p">(</span><span class="s">'/register'</span><span class="p">)</span>
|
|
<div class="viewcode-block" id="register_report"><a class="viewcode-back" href="../../api/accounting.html#accounting.web.register_report">[docs]</a><span class="k">def</span> <span class="nf">register_report</span><span class="p">():</span>
|
|
<span class="sd">'''</span>
|
|
<span class="sd"> Returns the JSON-serialized output of :py:meth:`accounting.Ledger.reg`</span>
|
|
<span class="sd"> '''</span>
|
|
<span class="n">report_data</span> <span class="o">=</span> <span class="n">ledger</span><span class="o">.</span><span class="n">reg</span><span class="p">()</span>
|
|
|
|
<span class="k">return</span> <span class="n">jsonify</span><span class="p">(</span><span class="n">register_report</span><span class="o">=</span><span class="n">report_data</span><span class="p">)</span>
|
|
|
|
</div>
|
|
<div class="viewcode-block" id="main"><a class="viewcode-back" href="../../api/accounting.html#accounting.web.main">[docs]</a><span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">argv</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
|
|
<span class="n">prog</span> <span class="o">=</span> <span class="n">__name__</span>
|
|
<span class="k">if</span> <span class="n">argv</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
|
|
<span class="n">prog</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
|
|
<span class="n">argv</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
|
|
|
|
<span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="n">prog</span><span class="p">)</span>
|
|
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">'-v'</span><span class="p">,</span> <span class="s">'--verbosity'</span><span class="p">,</span>
|
|
<span class="n">default</span><span class="o">=</span><span class="s">'INFO'</span><span class="p">,</span>
|
|
<span class="n">help</span><span class="o">=</span><span class="p">(</span><span class="s">'Filter logging output. Possible values:'</span> <span class="o">+</span>
|
|
<span class="s">' CRITICAL, ERROR, WARNING, INFO, DEBUG'</span><span class="p">))</span>
|
|
|
|
<span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="n">argv</span><span class="p">)</span>
|
|
|
|
<span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="n">level</span><span class="o">=</span><span class="nb">getattr</span><span class="p">(</span><span class="n">logging</span><span class="p">,</span> <span class="n">args</span><span class="o">.</span><span class="n">verbosity</span><span class="p">,</span> <span class="s">'INFO'</span><span class="p">))</span>
|
|
|
|
<span class="n">app</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">host</span><span class="o">=</span><span class="n">app</span><span class="o">.</span><span class="n">config</span><span class="p">[</span><span class="s">'HOST'</span><span class="p">],</span> <span class="n">port</span><span class="o">=</span><span class="n">app</span><span class="o">.</span><span class="n">config</span><span class="p">[</span><span class="s">'PORT'</span><span class="p">])</span>
|
|
</div>
|
|
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span>
|
|
<span class="n">main</span><span class="p">()</span>
|
|
</pre></div>
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sphinxsidebar">
|
|
<div class="sphinxsidebarwrapper">
|
|
<div id="searchbox" style="display: none">
|
|
<h3>Quick search</h3>
|
|
<form class="search" action="../../search.html" method="get">
|
|
<input type="text" name="q" />
|
|
<input type="submit" value="Go" />
|
|
<input type="hidden" name="check_keywords" value="yes" />
|
|
<input type="hidden" name="area" value="default" />
|
|
</form>
|
|
<p class="searchtip" style="font-size: 90%">
|
|
Enter search terms or a module, class or function name.
|
|
</p>
|
|
</div>
|
|
<script type="text/javascript">$('#searchbox').show(0);</script>
|
|
</div>
|
|
</div>
|
|
<div class="clearer"></div>
|
|
</div>
|
|
<div class="related">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../../genindex.html" title="General Index"
|
|
>index</a></li>
|
|
<li class="right" >
|
|
<a href="../../py-modindex.html" title="Python Module Index"
|
|
>modules</a> |</li>
|
|
<li><a href="../../index.html">Accounting API 0.1-beta documentation</a> »</li>
|
|
<li><a href="../index.html" >Module code</a> »</li>
|
|
<li><a href="../accounting.html" >accounting</a> »</li>
|
|
</ul>
|
|
</div>
|
|
<div class="footer">
|
|
© Copyright 2013, Joar Wandborg.
|
|
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.
|
|
</div>
|
|
</body>
|
|
</html> |