From 8fde1dde2c0c64ed29ffb1b20c953c97aad7b0f8 Mon Sep 17 00:00:00 2001 From: Debian Guru Date: Fri, 5 Oct 2018 07:10:16 +0200 Subject: [PATCH] cvs-export --- index.py | 29 ++++++++++++++++++++++++++++- kontomodel.py | 1 - static/categorize.js | 39 +++++++++++++++++++++++++++++++++++++++ static/download.png | Bin 0 -> 404 bytes views/categorize.tpl | 4 ++-- views/index.tpl | 4 +++- 6 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 static/download.png diff --git a/index.py b/index.py index bd83bb2..c619f70 100755 --- a/index.py +++ b/index.py @@ -6,6 +6,8 @@ import json import sys import re +import io +import csv import kontomodel @@ -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: @@ -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': diff --git a/kontomodel.py b/kontomodel.py index 02b2dac..deda367 100644 --- a/kontomodel.py +++ b/kontomodel.py @@ -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) diff --git a/static/categorize.js b/static/categorize.js index 4b280ac..feed411 100644 --- a/static/categorize.js +++ b/static/categorize.js @@ -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" + }); } diff --git a/static/download.png b/static/download.png new file mode 100644 index 0000000000000000000000000000000000000000..2f8906b073871dfc45f3fc677e348a8824d1cb47 GIT binary patch literal 404 zcmeAS@N?(olHy`uVBq!ia0vp^5+KaM3?#3wJbMaAO$+b|aRt)zgoP!o-Oar%ahLfByXa`}dzYbLQ;X zv**s8yMO=w)2C10y?giR)2AN4GqFNj@+B>-hFHN8s0ThV4K9w0(bY>c&_3 zh5-M@GyhN4{nY%SA1(eS^0nmQ%}2jocz7hD`0$Ng2X92O7q#hRKbi2c)7ta<%ItRz zTEgN6cJ(n2`X>LaX#W>b&bR4#@oa@rud&_5aY~$y3mv746 vYh+$@kNKWF&#yg9w_C4mV`&n+YNyT8=qa_UN@%M*(A5l{u6{1-oD!M{{title}} +

{{title}} Umsätze exportieren

% import json % import datetime @@ -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] % diff --git a/views/index.tpl b/views/index.tpl index 88b8594..dd6547e 100644 --- a/views/index.tpl +++ b/views/index.tpl @@ -147,6 +147,7 @@ function submitSettingsForm() { storeDates(); doPlot(); showDetails(detailsParams); + return false; } @@ -189,7 +190,8 @@ $(document).ready(function() { % end Suche: - + +