Robert Brewer avatar Robert Brewer committed 8416f3a

Moved chart stuff into its own .js and fixed imports

Comments (0)

Files changed (8)

flowrate/__init__.py

     filename=os.path.join(uidir, "common.js"))
 common_js.GET = common_js
 
+chart_js = cherrypy.tools.staticfile.handler(
+    filename=os.path.join(uidir, "chart.js"))
+chart_js.GET = chart_js
+
 class Root(object):
     
     exposed = True
     transactions = Transactions()
     json2_js = json2_js
     common_js = common_js
+    chart_js = chart_js
 
     def __init__(self):
         # This triggers tools.trailing_slash to force a redirect if missing

flowrate/csvutil.py

 import sqlalchemy
 
 import flowrate
-from flowrate import flows
+from flowrate import flows, ledger
 
 
 class Peekable(object):
             "AND amount = %s AND postdate = %s;",
             (credit['id'], debit['id'], amount, postdate)).fetchone()
         if exists is None:
-            newrow = flowrate.db.execute("INSERT INTO transactions "
-                "(amount, credit_account, debit_account, postdate,"
-                " description, credit_mult, debit_mult) "
-                "VALUES (%s, %s, %s, %s, %s, %s, %s) RETURNING *",
-                (amount, credit['id'], debit['id'], postdate, description,
-                 -1 if credit['type'] in ('asset', 'expense') else 1,
-                 1 if debit['type'] in ('asset', 'expense') else -1,
-                 )).fetchone()
-            flows.fulfill(newrow)
-            return newrow.id
+            credit_type = ledger.accounts[credit['id']]['type']
+            debit_type = ledger.accounts[debit['id']]['type']
+
+            tx = ledger.Transaction(id=None,
+                credit=credit['id'], debit=debit['id'],
+                credit_mult=-1 if credit_type in ('asset', 'expense') else 1,
+                debit_mult=1 if debit_type in ('asset', 'expense') else -1,
+                postdate=postdate, amount=amount, description=description)
+            tx.save()
+            tx.activate()
+
+            flows.fulfill(tx)
+
+            return tx.id
         else:
             return None
 

flowrate/ui/accounts.html

 <script type="text/javascript" src="https://www.google.com/jsapi"></script>
 <script type="text/javascript" src="/json2.js"></script>
 <script type="text/javascript" src="/common.js"></script>
+<script type="text/javascript" src="/chart.js"></script>
 <script type="text/javascript">
 
 google.load('visualization', '1.0', {'packages':['corechart']});

flowrate/ui/chart.js

+//                                   CHART                                   //
+
+var chart = null;
+var amount_formatter = null;
+function createAccountChart() {
+    chart = new google.visualization.ComboChart($('chart_div'));
+    amount_formatter = new google.visualization.NumberFormat(
+        {prefix: '$', negativeColor: 'red', negativeParens: true});
+}
+google.setOnLoadCallback(createAccountChart);
+
+function make_table(data) {
+    // Return a google.visualization.DataTable from the given data.
+    // 
+    // The 'data' argument MUST be an array of
+    // (account set, dataset[, titlesuffix]) tuples.
+    // 
+    // Each account set must an array of account id's (integers), which will
+    // be used as columns in the DataTable, which then are drawn as individual
+    // lines/bars in the resulting chart.
+    // 
+    // Each dataset is an object. Each key MUST be a string that describes a
+    // date group, such as '2012-01' for the month of January, 2012. These will
+    // be used to define rows in the DataTable and drawn as individual points
+    // on the horizontal axis. Each dataset value is then drawn as a coordinate
+    // in the chart.
+    // 
+    // If a titlesuffix is provided, it MUST be a string. It will be appended
+    // to each column title.
+    if (data.length <= 0) return;
+
+    // Create the data table.
+    var table = new google.visualization.DataTable();
+    table.addColumn('string', 'Account');
+
+    // Add columns from our data
+    for (var i = 0; i < data.length; i++) {
+        var col = data[i];
+
+        // Form the title of the column
+        var accountset = col[0];
+        if (accountset.length > 1) {
+            // Draw a line, label it with ids only
+            var name = accountset.join(" + ");
+        } else {
+            // Draw bars, label it with id and name
+            var a = accountset[0];
+            var name = a + ': ' + (accounts[a] ? accounts[a].name : '');
+        }
+
+        // Append the title suffix (like ' Budget'), if provided
+        if (col[2] != undefined) name = name + col[2];
+
+        // Add the column
+        table.addColumn('number', name);
+    }
+
+    // Grab the row keys and give them a stable sort order
+    var dategroups = keys(data[0][1]);
+    dategroups.sort();
+
+    // Add rows from our data
+    for (var i = 0; i < dategroups.length; i++) {
+        var dg = dategroups[i];
+        var row = [dg];
+        for (var a = 0; a < data.length; a++) {
+            row.push(data[a][1][dg] || 0);
+        }
+        table.addRow(row);
+    }
+
+    // For some reason, we have to do this after we add all the rows
+    for (var i = 0; i < data.length; i++) {
+        amount_formatter.format(table, i + 1);
+    }
+
+    return table;
+}
+
+function draw_table(table, title) {
+    // Draw the given table on the chart div.
+    var width = table.getNumberOfRows() * 75;
+    if (width < 600) { width = 600; }
+    if (width > 1200) { width = 1200; }
+
+    var series = {};
+    for (i=0; i < table.getNumberOfColumns(); i++) {
+        if (table.getColumnLabel(i).indexOf('+') != -1) {
+            series[i - 1] = {"type": "line", "curveType": "function"};
+        }
+    }
+
+    var options = {'title': title,
+                   'width': width, 'height': 400,
+                   'vAxis': {'minValue': 0},
+                   'seriesType': "bars",
+                   'series': series,
+                   'animation': {'duration': 1000, 'easing': 'out'},
+                   };
+
+    // Draw our chart, passing in some options.
+    chart.draw(table, options);
+}
+

flowrate/ui/common.js

     if (msgelem.parentNode) msgelem.parentNode.removeChild(msgelem);
 }
 
-//                                   CHART                                   //
-
-var chart = null;
-var amount_formatter = null;
-function createAccountChart() {
-    chart = new google.visualization.ComboChart($('chart_div'));
-    amount_formatter = new google.visualization.NumberFormat(
-        {prefix: '$', negativeColor: 'red', negativeParens: true});
-}
-google.setOnLoadCallback(createAccountChart);
-
-function make_table(data) {
-    // Return a google.visualization.DataTable from the given data.
-    // 
-    // The 'data' argument MUST be an array of
-    // (account set, dataset[, titlesuffix]) tuples.
-    // 
-    // Each account set must an array of account id's (integers), which will
-    // be used as columns in the DataTable, which then are drawn as individual
-    // lines/bars in the resulting chart.
-    // 
-    // Each dataset is an object. Each key MUST be a string that describes a
-    // date group, such as '2012-01' for the month of January, 2012. These will
-    // be used to define rows in the DataTable and drawn as individual points
-    // on the horizontal axis. Each dataset value is then drawn as a coordinate
-    // in the chart.
-    // 
-    // If a titlesuffix is provided, it MUST be a string. It will be appended
-    // to each column title.
-    if (data.length <= 0) return;
-
-    // Create the data table.
-    var table = new google.visualization.DataTable();
-    table.addColumn('string', 'Account');
-
-    // Add columns from our data
-    for (var i = 0; i < data.length; i++) {
-        var col = data[i];
-
-        // Form the title of the column
-        var accountset = col[0];
-        if (accountset.length > 1) {
-            // Draw a line, label it with ids only
-            var name = accountset.join(" + ");
-        } else {
-            // Draw bars, label it with id and name
-            var a = accountset[0];
-            var name = a + ': ' + (accounts[a] ? accounts[a].name : '');
-        }
-
-        // Append the title suffix (like ' Budget'), if provided
-        if (col[2] != undefined) name = name + col[2];
-
-        // Add the column
-        table.addColumn('number', name);
-    }
-
-    // Grab the row keys and give them a stable sort order
-    var dategroups = keys(data[0][1]);
-    dategroups.sort();
-
-    // Add rows from our data
-    for (var i = 0; i < dategroups.length; i++) {
-        var dg = dategroups[i];
-        var row = [dg];
-        for (var a = 0; a < data.length; a++) {
-            row.push(data[a][1][dg] || 0);
-        }
-        table.addRow(row);
-    }
-
-    // For some reason, we have to do this after we add all the rows
-    for (var i = 0; i < data.length; i++) {
-        amount_formatter.format(table, i + 1);
-    }
-
-    return table;
-}
-
-function draw_table(table, title) {
-    // Draw the given table on the chart div.
-    var width = table.getNumberOfRows() * 75;
-    if (width < 600) { width = 600; }
-    if (width > 1200) { width = 1200; }
-
-    var series = {};
-    for (i=0; i < table.getNumberOfColumns(); i++) {
-        if (table.getColumnLabel(i).indexOf('+') != -1) {
-            series[i - 1] = {"type": "line", "curveType": "function"};
-        }
-    }
-
-    var options = {'title': title,
-                   'width': width, 'height': 400,
-                   'vAxis': {'minValue': 0},
-                   'seriesType': "bars",
-                   'series': series,
-                   'animation': {'duration': 1000, 'easing': 'out'},
-                   };
-
-    // Draw our chart, passing in some options.
-    chart.draw(table, options);
-}
-

flowrate/ui/flows.html

 <script type="text/javascript" src="https://www.google.com/jsapi"></script>
 <script type="text/javascript" src="/json2.js"></script>
 <script type="text/javascript" src="/common.js"></script>
+<script type="text/javascript" src="/chart.js"></script>
 <script type="text/javascript">
 
 google.load('visualization', '1.0', {'packages':['corechart']});

flowrate/ui/transactions.html

 <script type="text/javascript" src="https://www.google.com/jsapi"></script>
 <script type="text/javascript" src="/json2.js"></script>
 <script type="text/javascript" src="/common.js"></script>
+<script type="text/javascript" src="/chart.js"></script>
 <script type="text/javascript">
 
 google.load('visualization', '1.0', {'packages':['corechart']});

flowrate/ui/variables.html

         var h = http("PUT", '/variables/' + varname_being_edited, false,
                      "Updating variable " + varname_being_edited);
         // TODO: update cell data
-        h[200] = function(h) {};
-        h[204] = function(h) {};
+        h[204] = function(h) {
+            // Remove and re-add the row.
+            var varrow = $('varrow' + varname_being_edited);
+            varrow.parentNode.removeChild(varrow);
+            v.id = varname_being_edited;
+            varrow = add_var_row(v);
+
+            // Also move the varedit row before it
+            var varedit = $('varedit');
+            varedit.parentNode.removeChild(varedit);
+            varrow.parentNode.insertBefore(varedit, varrow);
+            varrow.style.display = 'none';
+        };
     }
     h.setRequestHeader("Content-Type", "application/json");
     h.send(JSON.stringify({"body": v}));
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.