add tableview in /currencies

This commit is contained in:
Ivan Hörler 2017-12-29 17:02:33 +01:00
parent 318d599a2f
commit 13805f790d
3 changed files with 32 additions and 13 deletions

View File

@ -53,8 +53,10 @@ def get_exchange_rate():
datetime_str = item.find('dc:date', ns).text
# convert string to date object:
# https://stackoverflow.com/a/12282040/4061870
# seams like snb striked the microsecond somewhere between Nov. and
# Dez. 2017 so maybe first check time type. "%Y-%m-%dT%H:%M:%S.%f%z"
date = datetime.strptime(''.join(datetime_str.rsplit(':', 1)),
"%Y-%m-%dT%H:%M:%S.%f%z").strftime("%Y-%m-%d")
"%Y-%m-%dT%H:%M:%S%z").strftime("%Y-%m-%d")
# only the values of today are used so check for date:
if date == today:
title = item.find('none:title', ns).text
@ -110,11 +112,11 @@ def get_exchange_rate():
# CHFvalue, "CHF and 1 ", base_currency, " costs: ",
# FOREIGNvalue_round, target_currency)
exchange_rates.update(
{target_currency: FOREIGNvalue_round, "date": date})
{target_currency: FOREIGNvalue_round})
# Print the Dictionary:
# print(exchange_rates)
else:
break
return(exchange_rates)
return(exchange_rates, date)
# for development its preferable to see that the for loop is done:
# print('no more fresh data!')

View File

@ -6,6 +6,22 @@
<div id="content" class="flex">
<h1>Currencies in CHF</h1>
{% if currency_list %}
<table>
<tr>
<th scope="col">DATE</th>
{% for currency in currency_list %}
<th scope="col">{{ currency.name }}</th>
{% endfor %}
</tr>
<tr>
<td>{{ date }}</td>
{% for currency in currency_list %}
<td>{{ currency.exchange_rate_to_chf }}</td>
{% endfor %}
</tr>
</table>
<br>
<br>
<ul>
{% for currency in currency_list %}
<li>

View File

@ -5,23 +5,24 @@ from currencies import exchange_rates
def currencies(request):
# return HttpResponse("exchange_rates")
raw_data = exchange_rates.get_exchange_rate()
for i, j, k, l in raw_data.items():
if ExchangeRate.objects.filter(name=i):
raw_data, date = exchange_rates.get_exchange_rate()
for currency, rate in raw_data.items():
if ExchangeRate.objects.filter(name=currency):
e = ExchangeRate.objects.filter(
name=i,
name=currency,
).update(
exchange_rate_to_chf=j,
date=l
exchange_rate_to_chf=rate,
date=date
)
else:
e = ExchangeRate.objects.create(
name=i,
exchange_rate_to_chf=j,
date=l
name=currency,
exchange_rate_to_chf=rate,
date=date
)
e.save()
currency_list = ExchangeRate.objects.all()
return render(request,
'currencies/index.html',
{'currency_list': currency_list})
{'currency_list': currency_list,
'date': date})