Merge pull request #36 from tpokorra/TP-202112-export_transactions

export transactions as excel file
This commit was merged in pull request #36.
This commit is contained in:
2021-12-10 18:01:57 +01:00
committed by GitHub
4 changed files with 62 additions and 0 deletions
+2
View File
@@ -8,6 +8,8 @@
<br/> <br/>
<center><a href="/transactions/import" class="button button-primary">Import from Bitcoin.de</a></center> <center><a href="/transactions/import" class="button button-primary">Import from Bitcoin.de</a></center>
<br/> <br/>
<center><a href="/transactions/export" class="button button-primary">Export Transactions</a></center>
<br/>
<table class="table table-striped table-bordered table-sm"> <table class="table table-striped table-bordered table-sm">
<thead class="thead-dark"> <thead class="thead-dark">
<tr> <tr>
+58
View File
@@ -6,6 +6,9 @@ from apps.transactions.forms import ImportForm
from apps.transactions.models import Transaction from apps.transactions.models import Transaction
from apps.transactions.importbtcde import ImportBtcDe from apps.transactions.importbtcde import ImportBtcDe
import socket import socket
import xlwt
from django.http import HttpResponse
@login_required @login_required
def add(request): def add(request):
@@ -60,6 +63,61 @@ def show(request):
tr.fiat_total += float(tr.fiat_fee) tr.fiat_total += float(tr.fiat_fee)
return render(request,"show.html",{'transactions':transactions}) 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 @login_required
def edit(request, id): def edit(request, id):
transaction = Transaction.objects.get(id=id, owner=request.user) transaction = Transaction.objects.get(id=id, owner=request.user)
+1
View File
@@ -3,3 +3,4 @@ django-registration
pytz pytz
matplotlib matplotlib
btcde btcde
xlwt
+1
View File
@@ -30,6 +30,7 @@ urlpatterns = [
path('graph/', monitor_views.graph), path('graph/', monitor_views.graph),
path('transactions/add', tr_views.add), path('transactions/add', tr_views.add),
path('transactions/import', tr_views.importbtcde), path('transactions/import', tr_views.importbtcde),
path('transactions/export', tr_views.export),
path('transactions/show', tr_views.show), path('transactions/show', tr_views.show),
path('transactions/edit/<int:id>', tr_views.edit), path('transactions/edit/<int:id>', tr_views.edit),
path('transactions/update/<int:id>', tr_views.update), path('transactions/update/<int:id>', tr_views.update),