Skip to content

Commit

Permalink
cvs-export
Browse files Browse the repository at this point in the history
  • Loading branch information
mtill committed Oct 5, 2018
1 parent a6e100b commit 8fde1dd
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 5 deletions.
29 changes: 28 additions & 1 deletion index.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import json
import sys
import re
import io
import csv

import kontomodel

Expand Down Expand Up @@ -158,6 +160,8 @@ def getDetails():
sortScatterBy = request.json.get('sortScatterBy')
sortScatterByReverse = request.json.get('sortScatterByReverse')

csvexport = request.json.get('csvexport', False)

title = 'Umsätze'
if categorySelection is not None:
if len(categorySelection) == 1:
Expand All @@ -170,7 +174,30 @@ def getDetails():

legendonlyTraces = ["Umbuchung", "Gehalt"]
consolidated = k.getConsolidated(byCategory=byCategory, traceNames=['scatter'], fromDate=fromDate, categories=categories, toDate=toDate, accounts=accounts, thepattern=thepattern, categorySelection=categorySelection, sortScatterBy=sortScatterBy, legendonlyTraces=legendonlyTraces)
return template('categorize.tpl', title=title, theX=theX, allcategoriesNames=consolidated['allcategoriesNames'], scatter=consolidated['scatter'], reverseSort=sortScatterByReverse)

if csvexport:
cwriterresult = io.StringIO()
cwriter = csv.DictWriter(cwriterresult, fieldnames=['id', 'date', 'x', 'account', 'amount', 'currency', 'name', 'description', 'category', 'note'], delimiter=',')
cwriter.writeheader()
thescatter = consolidated['scatter']
for i in range(0, len(thescatter['timestamp'])):
mydate = datetime.datetime.fromtimestamp(thescatter['timestamp'][i]).strftime('%Y-%m-%d')
cwriter.writerow({'date': mydate,
'account': thescatter['account'][i],
'id': thescatter['id'][i],
'amount': thescatter['amount'][i],
'currency': thescatter['currency'][i],
'name': thescatter['name'][i],
'description': thescatter['description'][i],
'category': thescatter['category'][i],
'note': thescatter['note'][i],
'x': thescatter['theX'][i]})
cwriterresultstr = cwriterresult.getvalue()
cwriterresult.close()
return cwriterresultstr
else:
return template('categorize.tpl', title=title, theX=theX, allcategoriesNames=consolidated['allcategoriesNames'], scatter=consolidated['scatter'], reverseSort=sortScatterByReverse)


if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == 'dev':
Expand Down
1 change: 0 additions & 1 deletion kontomodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ def getTransactions(self, accounts, fromDate, toDate):
sqlparam.append(toDateTimestamp)
hasParams = True

print()
accountsql = ''
if accounts is not None:
sqlparam.extend(accounts)
Expand Down
39 changes: 39 additions & 0 deletions static/categorize.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,43 @@ function showDetails(params) {
},
contentType: "application/json; charset=utf-8"
});

}

function doDownload(params) {
$.ajax({
type: "POST",
url: "/getDetails",
data: JSON.stringify({theX: params["theX"],
byCategory: params["byCategory"],
fromDate: params["fromDate"],
toDate: params["toDate"],
accounts: params["accounts"],
patternInput: params["patternInput"],
categorySelection: params["categorySelection"],
sortScatterBy: params["sortScatterBy"],
sortScatterByReverse: params["sortScatterByReverse"],
csvexport: true}),
success: function(thedata) {
//var w = window.open("about:blank", "csvexport");
//w.document.open();
//w.document.write(thedata);
//w.document.close();

var newBlob = new Blob([thedata], {type: "text/csv; charset=utf-8"});
var blobdata = window.URL.createObjectURL(newBlob);
var thelink = document.createElement('a');
thelink.href = blobdata;
thelink.style.display = "none";
thelink.download = "konto_" + params["fromDate"] + "_" + params["toDate"] + ".csv";
$("body").append(thelink);
thelink.click();
setTimeout(function() {
window.URL.revokeObjectURL(blobdata);
$(thelink).remove();
}, 5000);

},
contentType: "application/json; charset=utf-8"
});
}
Binary file added static/download.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions views/categorize.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<h2>{{title}}</h2>
<h2>{{title}} <img src="/static/download.png" class="clickable" onclick="doDownload(detailsParams)" alt="Umsätze exportieren"></h2>

% import json
% import datetime
Expand Down Expand Up @@ -45,7 +45,7 @@
% shortdescription = scatter['description'][i][0:40]
% account = scatter['account'][i]
% theid = scatter['id'][i]
% amountcurrency = str(scatter['amount'][i]) + scatter['currency'][i]
% amountcurrency = '{:.2f}'.format(scatter['amount'][i]) + scatter['currency'][i]
% thecategory = '' if scatter['category'][i] == 'nicht kategorisiert' else scatter['category'][i]
% thenote = scatter['note'][i]
%
Expand Down
4 changes: 3 additions & 1 deletion views/index.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ function submitSettingsForm() {
storeDates();
doPlot();
showDetails(detailsParams);
return false;
}
Expand Down Expand Up @@ -189,7 +190,8 @@ $(document).ready(function() {
% end

<span style="margin-left: 2em">Suche: <input type="text" id="patternInput" placeholder="Filter" value=""></span>
<input type="submit" value="Umsätze anzeigen / aktualisieren" style="margin-right:2em">

<input type="submit" value="Umsätze anzeigen / aktualisieren" style="margin-left:2em">
</form>
</fieldset>

Expand Down

0 comments on commit 8fde1dd

Please sign in to comment.