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 datetime_str = item.find('dc:date', ns).text
# convert string to date object: # convert string to date object:
# https://stackoverflow.com/a/12282040/4061870 # 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)), 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: # only the values of today are used so check for date:
if date == today: if date == today:
title = item.find('none:title', ns).text title = item.find('none:title', ns).text
@ -110,11 +112,11 @@ def get_exchange_rate():
# CHFvalue, "CHF and 1 ", base_currency, " costs: ", # CHFvalue, "CHF and 1 ", base_currency, " costs: ",
# FOREIGNvalue_round, target_currency) # FOREIGNvalue_round, target_currency)
exchange_rates.update( exchange_rates.update(
{target_currency: FOREIGNvalue_round, "date": date}) {target_currency: FOREIGNvalue_round})
# Print the Dictionary: # Print the Dictionary:
# print(exchange_rates) # print(exchange_rates)
else: else:
break break
return(exchange_rates) return(exchange_rates, date)
# for development its preferable to see that the for loop is done: # for development its preferable to see that the for loop is done:
# print('no more fresh data!') # print('no more fresh data!')

View File

@ -6,6 +6,22 @@
<div id="content" class="flex"> <div id="content" class="flex">
<h1>Currencies in CHF</h1> <h1>Currencies in CHF</h1>
{% if currency_list %} {% 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> <ul>
{% for currency in currency_list %} {% for currency in currency_list %}
<li> <li>

View File

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