diff --git a/django/didgeridoo/currencies/exchange_rates.py b/django/didgeridoo/currencies/exchange_rates.py
index db1cc6b..5415301 100644
--- a/django/didgeridoo/currencies/exchange_rates.py
+++ b/django/didgeridoo/currencies/exchange_rates.py
@@ -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!')
diff --git a/django/didgeridoo/currencies/templates/currencies/index.html b/django/didgeridoo/currencies/templates/currencies/index.html
index 50d944a..8e67685 100644
--- a/django/didgeridoo/currencies/templates/currencies/index.html
+++ b/django/didgeridoo/currencies/templates/currencies/index.html
@@ -6,6 +6,22 @@
Currencies in CHF
{% if currency_list %}
+
+
+ DATE |
+ {% for currency in currency_list %}
+ {{ currency.name }} |
+ {% endfor %}
+
+
+ {{ date }} |
+ {% for currency in currency_list %}
+ {{ currency.exchange_rate_to_chf }} |
+ {% endfor %}
+
+
+
+
{% for currency in currency_list %}
-
diff --git a/django/didgeridoo/currencies/views.py b/django/didgeridoo/currencies/views.py
index 4a4450c..45c280b 100644
--- a/django/didgeridoo/currencies/views.py
+++ b/django/didgeridoo/currencies/views.py
@@ -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})