Robert Brewer avatar Robert Brewer committed a19d254

Replace edit rows always, and write dategroupformat on flow POST/PUT

Comments (0)

Files changed (6)

flowrate/__init__.py

 
         db.execute(
             "DELETE FROM obligations WHERE flowid = %s;", (req.flowid,))
+        if vals['unit'] == 'years':
+            dategroupformat = 'YYYY'
+        elif vals['unit'] == 'days':
+            dategroupformat = 'YYYY-MM-DD'
+        else:
+            dategroupformat = 'YYYY-MM'
         for tx in flows.obligations(row):
             # TODO: this is slow. Can we change it to an INSERT INTO ... FROM"?
             db.execute(
                 "INSERT INTO obligations "
                 "(flowid, postdate, credit_account, debit_account,"
-                " description, amount) "
-                "VALUES (%s, %s, %s, %s, %s, %s);",
+                " description, amount, dategroupformat) "
+                "VALUES (%s, %s, %s, %s, %s, %s, %s);",
                 (req.flowid, tx['postdate'], tx['credit'], tx['debit'],
-                 tx['description'], tx['amount']))
+                 tx['description'], tx['amount'], dategroupformat))
 
         cherrypy.response.status = 204
 
         sql = "SELECT * FROM flows"
         if whereclause:
             sql += " WHERE " + " AND ".join(whereclause)
-        sql += " ORDER BY range_start DESC, debit_account;"
+        sql += " ORDER BY debit_account, credit_account, range_start DESC;"
         rows = db.execute(sql, args).fetchall()
         t['data'] = [
             {"id": cherrypy.url("/flows/%s" % row.id),
                     vals["description"],
                     )).fetchone()
 
+        if vals['unit'] == 'years':
+            dategroupformat = 'YYYY'
+        elif vals['unit'] == 'days':
+            dategroupformat = 'YYYY-MM-DD'
+        else:
+            dategroupformat = 'YYYY-MM'
         for tx in flows.obligations(newrow):
+            # TODO: this is slow. Can we change it to an INSERT INTO ... FROM"?
             db.execute(
                 "INSERT INTO obligations "
                 "(flowid, postdate, credit_account, debit_account,"
-                " description, amount) "
-                "VALUES (%s, %s, %s, %s, %s, %s);",
+                " description, amount, dategroupformat) "
+                "VALUES (%s, %s, %s, %s, %s, %s, %s);",
                 (newrow.id, tx['postdate'], tx['credit'], tx['debit'],
-                 tx['description'], tx['amount']))
+                 tx['description'], tx['amount'], dategroupformat))
 
         cherrypy.response.status = 201
         cherrypy.response.headers['Location'] = cherrypy.url("%s" % newrow.id)

flowrate/common.js

 
 //                                   CHART                                   //
 
-google.load('visualization', '1.0', {'packages':['corechart']});
-
 function drawAccountChart(title, data, element) {
     // Draw a Google Chart onto the $chart_div element.
     // The 'data' argument MUST be an array of (account set, dategroup) pairs.
     // as rows in the DataTable, which then are drawn as individual points
     // on the horizontal axis.
 
+    google.load('visualization', '1.0', {'packages':['corechart']});
+
     // Create the data table.
     var table = new google.visualization.DataTable();
     table.addColumn('string', 'Account');

flowrate/flowrate.sql

 
 
 -- View: fulfillments
-
+-- TODO: this doesn't take into account the period
 CREATE OR REPLACE VIEW fulfillments AS
     SELECT o.*,
         (SELECT COALESCE(SUM(t.amount), 0) FROM transactions t

flowrate/flows.html

     r = document.createElement("tr");
     r.className = 'flowrow';
     r.id = 'flowrow' + flowid;
+    r.flow = flow;
     r.onclick = function () { edit_flow(flowid) };
     
     // ID cell
     for (var i=0; i < rowset.childNodes.length; i++) {
         var existing = rowset.childNodes[i];
         if (existing.className == 'flowrow') {
-            if (flows[existing.id.substring('flowrow'.length)].start <= flow.start) {
+            if (existing.flow.debit_account <= flow.debit_account) {
                 rowset.insertBefore(r, existing);
                 break;
             }
         var h = http("PUT", '/flows/' + flowid_being_edited, false,
                      "Updating flow " + flowid_being_edited);
         h[204] = function(h) {
-            // Remove and re-add the row if it changed dates
-            if (flow.start != flows[flowid_being_edited].start) {
-                var flowrow = $('flowrow' + flowid_being_edited);
-                flowrow.parentNode.removeChild(flowrow);
-                flow.id = flowid_being_edited;
-                flowrow = add_flow_row(flow);
+            // Remove and re-add the row. This will also move it if dates change
+            var flowrow = $('flowrow' + flowid_being_edited);
+            flowrow.parentNode.removeChild(flowrow);
+            flow.id = flowid_being_edited;
+            flowrow = add_flow_row(flow);
 
-                // Also move the flowedit row before it
-                var flowedit = $('flowedit');
-                flowedit.parentNode.removeChild(flowedit);
-                flowrow.parentNode.insertBefore(flowedit, flowrow);
-                flowrow.style.display = 'none';
-            }
+            // Also move the flowedit row before it
+            var flowedit = $('flowedit');
+            flowedit.parentNode.removeChild(flowedit);
+            flowrow.parentNode.insertBefore(flowedit, flowrow);
+            flowrow.style.display = 'none';
         };
     }
     h.setRequestHeader("Content-Type", "application/json");

flowrate/flows.py

         unit, day = lambda d: d.year, lambda d: d.timetuple().tm_yday
         pgfmt = 'YYYY'
 
-    for d in range(0, (flow.range_end - flow.range_start).days):
+    for d in range(0, (flow.range_end - flow.range_start).days + 1):
         postdate = flow.range_start + datetime.timedelta(days=d)
         if day(postdate) != flow.day:
             continue
                 'amount': flow.amount,
                 }
 
+def isSubAccount(child, parents):
+    for p in parents:
+        for scale in (1000, 100, 10, 1):
+            if p % scale == 0 and p <= child < (p + scale):
+                return True
+    return False
 
 def transactions(accounts=None, credits=None, debits=None,
                  years=None, months=None, days=None,
                            " OR ARRAY[debit_account] <@ %(accounts)s)")
         all_accts = [row.id for row in flowrate.db.execute(
                      "SELECT id FROM accounts;").fetchall()]
-        def isSubAccount(child, parents):
-            for p in parents:
-                for scale in (1000, 100, 10, 1):
-                    if p % scale == 0 and p <= child < (p + scale):
-                        return True
-            return False
         args['accounts'] = [a for a in all_accts if isSubAccount(a, accounts)]
     if credits:
         whereclause.append("ARRAY[credit_account] <@ %(credits)s")

flowrate/transactions.html

         var h = http("PUT", '/transactions/' + txid_being_edited, false,
                      "Updating transaction " + txid_being_edited);
         h[204] = function(h) {
-            // Remove and re-add the row if it changed dates
-            if (tx.postdate != txs[txid_being_edited].postdate) {
-                var txrow = $('txrow' + txid_being_edited);
-                txrow.parentNode.removeChild(txrow);
-                tx.id = txid_being_edited;
-                txrow = add_tx_row(tx);
+            // Remove and re-add the row; this moves it if it changed dates
+            var txrow = $('txrow' + txid_being_edited);
+            txrow.parentNode.removeChild(txrow);
+            tx.id = txid_being_edited;
+            txrow = add_tx_row(tx);
 
-                // Also move the txedit row before it
-                var txedit = $('txedit');
-                txedit.parentNode.removeChild(txedit);
-                txrow.parentNode.insertBefore(txedit, txrow);
-                txrow.style.display = 'none';
-            }
+            // Also move the txedit row before it
+            var txedit = $('txedit');
+            txedit.parentNode.removeChild(txedit);
+            txrow.parentNode.insertBefore(txedit, txrow);
+            txrow.style.display = 'none';
         };
     }
     h.setRequestHeader("Content-Type", "application/json");
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.