Permalink
Browse files

refactoring get_entries and related functions, in preparation for all…

…owing date-range selection
  • Loading branch information...
buggi22 committed Jan 26, 2012
1 parent f8f6d05 commit d5dc263feede7f95870139ab45efdfe690b7d57b
Showing with 118 additions and 21 deletions.
  1. +109 −12 finance.py
  2. +9 −9 templates/show_entries.html
View
@@ -28,16 +28,110 @@ def before_request():
def teardown_request(exception):
g.db.close()
-def get_entries():
- cur = g.db.execute('select description, amountcents, srcbucketname, srcbucketid, destbucketname, destbucketid, entryid from entries_labeled')
- entries = [dict(description=row[0], amountstring=cents_to_string(row[1]), srcbucket=str(row[2]), srcbucketid=row[3], destbucket=str(row[4]), destbucketid=row[5], entryid=row[6]) for row in cur.fetchall()]
+def update_balances(balances, changes):
+ if(len(balances) != len(changes)):
+ raise Exception("balances must be same length as changes")
+ result = []
+
+ for i, bucket_balance in enumerate(balances):
+ new_balance = changes[i]['amountcents']
+ if('initialbalancecents' in bucket_balance):
+ new_balance += bucket_balance['initialbalancecents']
+ else:
+ new_balance += bucket_balance['balancecents']
+
+ result.append( {
+ 'bucketname': bucket_balance['bucketname'],
+ 'balancecents': new_balance,
+ 'balancestring': cents_to_string( new_balance )
+ } )
+
+ return result
+
+
+def get_balances_at(datetime=None):
cur = g.db.execute('select bucketid, bucketname, initialbalancecents from buckets where buckettype = "internal"')
- internals = [dict(bucketname=row[1], initialbalancecents=row[2], initialbalancestring=cents_to_string(row[2])) for row in cur.fetchall()]
- numinternals = len(internals)
+ initial_balances = [
+ dict(
+ bucketname=row[1],
+ initialbalancecents=row[2],
+ initialbalancestring=cents_to_string(row[2])
+ ) for row in cur.fetchall() ]
+ if(datetime == None):
+ return initial_balances
+
+ balances = [] + initial_balances
+
+ list_of_changes = get_changes_by_entry_and_bucket(start=None, end=datetime)
+ for changes in list_of_changes:
+ balances = update_balances(balances, changes)
+
+ return balances
+
+def get_changes_by_entry_and_bucket(start=None, end=None):
cur = g.db.execute('select entryid, bucketid_for_change, amountcents from entries_with_bucket_changes')
+ rows = cur.fetchall()
+
+ prev_entryid = None
+ result = []
+
+ for row in rows:
+ if(row[0] != prev_entryid):
+ result.append( [] )
+ result[-1].append( dict(
+ amountcents = row[2],
+ amountstring = cents_to_string(int(row[2])) if row[2] != 0 else '-'
+ ) )
+ prev_entryid = row[0]
+
+ return result
+
+def get_ending_balances_by_entry_and_bucket(start=None, end=None):
+ balances = get_balances_at(start)
+ result = []
+ list_of_changes = get_changes_by_entry_and_bucket(start, end)
+
+ for changes in list_of_changes:
+ balances = update_balances(balances, changes)
+ result.append(balances)
+
+ return result
+
+def get_entries(start=None, end=None):
+ cur = g.db.execute('select description, amountcents, srcbucketname, srcbucketid, ' +
+ 'destbucketname, destbucketid, entryid from entries_labeled')
+ entries = [
+ dict(
+ description=row[0],
+ amountstring=cents_to_string(row[1]),
+ srcbucket=str(row[2]),
+ srcbucketid=row[3],
+ destbucket=str(row[4]),
+ destbucketid=row[5],
+ entryid=row[6]
+ ) for row in cur.fetchall() ]
+ return entries
+
+def get_entries_with_changes_and_balances(start=None, end=None):
+ entries = get_entries(start, end)
+ initial_balances = get_balances_at(start)
+ changes = get_changes_by_entry_and_bucket(start, end)
+ balances = get_ending_balances_by_entry_and_bucket(start, end)
+
+ for i in range(len(entries)):
+ entries[i]['balances'] = balances[i]
+ entries[i]['changes'] = changes[i]
+
+ return (entries, initial_balances)
+
+ '''
+ internals = get_balances_at(start)
+ numinternals = len(internals)
+
runningtotals = [b['initialbalancecents'] for b in internals]
+ cur = g.db.execute('select entryid, bucketid_for_change, amountcents from entries_with_bucket_changes')
for i, row in enumerate(cur.fetchall()):
if(i % numinternals == 0):
@@ -49,11 +143,13 @@ def get_entries():
entries[i / numinternals]['balances'] += [ cents_to_string( int(runningtotals[i % numinternals]) ) ]
return (entries, internals)
+ '''
@app.route('/')
def show_entries():
- entries, internals = get_entries()
- return render_template('show_entries.html', entries=entries, internals=internals)
+ entries, initial_balances = get_entries_with_changes_and_balances()
+ return render_template('show_entries.html', entries=entries,
+ initial_balances=initial_balances)
@app.route('/add_entry', methods=['POST'])
def add_entry():
@@ -69,7 +165,8 @@ def add_entry():
@app.route('/show_buckets')
def show_buckets():
- cur = g.db.execute('select bucketname, initialbalancecents, net_change, finalbalancecents from buckets_with_net_change where buckettype = "internal" order by bucketid asc')
+ cur = g.db.execute('select bucketname, initialbalancecents, net_change, finalbalancecents from ' +
+ 'buckets_with_net_change where buckettype = "internal" order by bucketid asc')
buckets = [dict(name=row[0], initialbalancestring=cents_to_string(row[1]),
netchangestring=cents_to_string(row[2]),
finalbalancestring=cents_to_string(row[3]) )
@@ -103,22 +200,22 @@ def add_bucket():
@app.route('/history.png')
def history_png():
- entries, internals = get_entries()
+ entries, initial_balances = get_entries_with_changes_and_balances()
xvalues = pylab.arange(0, len(entries)+1, 1)
- yvalues = [[internal['initialbalancecents'] / 100.0] for internal in internals]
+ yvalues = [[initial_balance['initialbalancecents'] / 100.0] for initial_balance in initial_balances]
+ seriesnames = [initial_balance['bucketname'] for initial_balance in initial_balances]
for e in entries:
for i, balance in enumerate(e['balances']):
- yvalues[i] += [string_to_cents(balance) / 100.0]
+ yvalues[i] += [ balance['balancecents'] / 100.0]
series = []
for yv in yvalues:
series += [xvalues, yv]
pylab.clf() # clear current figure
pylab.plot(*series)
- seriesnames = [internal['bucketname'] for internal in internals]
pylab.legend(seriesnames, 'lower right')
imgdata = StringIO.StringIO()
@@ -15,15 +15,15 @@
<td>From</td>
<td>To</td>
<td>&nbsp;</td>
- {% for internal in internals %}
- <td>{{ internal.bucketname }}</td>
+ {% for initial_balance in initial_balances %}
+ <td>{{ initial_balance.bucketname }}</td>
{% endfor %}
</tr>
<tr class=resultingbalance>
<td colspan=3>&nbsp;</td>
<td>Initial: </td>
- {% for internal in internals %}
- <td align="right">{{ internal.initialbalancestring }}</td>
+ {% for initial_balance in initial_balances %}
+ <td align="right">{{ initial_balance.initialbalancestring }}</td>
{% endfor %}
</tr>
{% for entry in entries %}
@@ -32,15 +32,15 @@
<td>{{ entry.srcbucket }}</td>
<td>{{ entry.destbucket }}</td>
<td>Change:</td>
- {% for internal in entry.internals %}
- <td align="right">{{ internal }}</td>
+ {% for change in entry.changes %}
+ <td align="right">{{ change.amountstring }}</td>
{% endfor %}
</tr>
<tr class=resultingbalance>
<td colspan=3>{{ entry.description }}&nbsp;</td>
<td>Result:</td>
{% for balance in entry.balances %}
- <td align="right">{{ balance }}</td>
+ <td align="right">{{ balance.balancestring }}</td>
{% endfor %}
</tr>
{% else %}
@@ -52,7 +52,7 @@
<td>From<br/><input type=text size=10 name=srcbucket></td>
<td>To<br/><input type=text size=10 name=destbucket></td>
<td>&nbsp;</td>
- {% for internal in internals %}
+ {% for initial_balance in initial_balances %}
<td>&nbsp;</td>
{% endfor %}
</tr>
@@ -63,7 +63,7 @@
<input type=submit value="Add Entry">
</td>
<td>&nbsp;</td>
- {% for internal in internals %}
+ {% for initial_balance in initial_balances %}
<td>&nbsp;</td>
{% endfor %}
</tr>

0 comments on commit d5dc263

Please sign in to comment.