From 1d734c5e0a8d3c9f68d1767ad13e558163d6a59d Mon Sep 17 00:00:00 2001 From: Timotheus Pokorra Date: Fri, 10 Dec 2021 18:00:37 +0100 Subject: [PATCH] export transactions as excel file fixes #3 --- apps/transactions/templates/show.html | 2 + apps/transactions/views.py | 58 +++++++++++++++++++++++++++ requirements.txt | 1 + walletmonitor/urls.py | 1 + 4 files changed, 62 insertions(+) diff --git a/apps/transactions/templates/show.html b/apps/transactions/templates/show.html index f62a142..71566fc 100644 --- a/apps/transactions/templates/show.html +++ b/apps/transactions/templates/show.html @@ -8,6 +8,8 @@
Import from Bitcoin.de

+
Export Transactions
+
diff --git a/apps/transactions/views.py b/apps/transactions/views.py index 42ce6a3..421f11b 100644 --- a/apps/transactions/views.py +++ b/apps/transactions/views.py @@ -6,6 +6,9 @@ from apps.transactions.forms import ImportForm from apps.transactions.models import Transaction from apps.transactions.importbtcde import ImportBtcDe import socket +import xlwt +from django.http import HttpResponse + @login_required def add(request): @@ -60,6 +63,61 @@ def show(request): tr.fiat_total += float(tr.fiat_fee) return render(request,"show.html",{'transactions':transactions}) + +@login_required +def export(request): + if 'crypto' in request.GET: + filename="transactions-"+request.GET['crypto']+".xls" + transactions = Transaction.objects.filter(owner=request.user, crypto_currency=request.GET['crypto']).order_by('-date_valid') + else: + transactions = Transaction.objects.filter(owner=request.user).order_by('-date_valid') + filename="transactions.xls" + + response = HttpResponse(content_type='application/ms-excel') + response['Content-Disposition'] = 'attachment; filename="'+filename+'"' + + wb = xlwt.Workbook(encoding='utf-8') + ws = wb.add_sheet('Transactions') + + # header + row_num = 0 + font_style = xlwt.XFStyle() + font_style.font.bold = True + + columns = ['Date', 'Type', 'Crypto Amount', 'Crypto Fee', 'Crypto Currency', 'Fiat Amount', 'Fiat Fee', 'Fiat Currency', 'Rate', 'Description', ] + for col_num in range(len(columns)): + ws.write(row_num, col_num, columns[col_num], font_style) + + # body + font_style = xlwt.XFStyle() + date_format = xlwt.XFStyle() + date_format.num_format_str = 'dd/mm/yyyy' + + for row in transactions: + row_num += 1 + ws.write(row_num, 0, row.date_valid.replace(tzinfo=None), date_format) + ws.write(row_num, 1, row.transaction_type, font_style) + if row.transaction_type == 'B': + ws.write(row_num, 2, row.crypto_amount, font_style) + elif row.crypto_amount: + ws.write(row_num, 2, -1*row.crypto_amount, font_style) + if row.crypto_fee: + ws.write(row_num, 3, -1*row.crypto_fee, font_style) + ws.write(row_num, 4, row.crypto_currency, font_style) + if row.transaction_type == 'B' and row.fiat_amount: + ws.write(row_num, 5, -1*row.fiat_amount, font_style) + else: + ws.write(row_num, 5, row.fiat_amount, font_style) + if row.fiat_fee: + ws.write(row_num, 6, -1*row.fiat_fee, font_style) + ws.write(row_num, 7, row.fiat_currency, font_style) + ws.write(row_num, 8, row.exchange_rate, font_style) + ws.write(row_num, 9, row.description, font_style) + + wb.save(response) + return response + + @login_required def edit(request, id): transaction = Transaction.objects.get(id=id, owner=request.user) diff --git a/requirements.txt b/requirements.txt index 65d1ddc..0ddede0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ django-registration pytz matplotlib btcde +xlwt diff --git a/walletmonitor/urls.py b/walletmonitor/urls.py index 6114574..c1da1f2 100644 --- a/walletmonitor/urls.py +++ b/walletmonitor/urls.py @@ -30,6 +30,7 @@ urlpatterns = [ path('graph/', monitor_views.graph), path('transactions/add', tr_views.add), path('transactions/import', tr_views.importbtcde), + path('transactions/export', tr_views.export), path('transactions/show', tr_views.show), path('transactions/edit/', tr_views.edit), path('transactions/update/', tr_views.update),