changed view and catched a bug in currencies.views.py
This commit is contained in:
parent
12ba3262ec
commit
aca21b4353
|
@ -1,8 +1,6 @@
|
|||
from datetime import datetime
|
||||
import urllib.request
|
||||
import xml.etree.ElementTree as ET
|
||||
import datetime as dt
|
||||
|
||||
|
||||
"""
|
||||
this method calls a rss/XML Resource
|
||||
|
@ -14,6 +12,8 @@ Key:Value pairs of new currencys.
|
|||
|
||||
|
||||
def get_exchange_rate():
|
||||
# zweitweise kann die resource nicht geladen werden.
|
||||
# https://stackoverflow.com/a/43523497/4061870
|
||||
# During weekends there are no updates.
|
||||
# To develop i need a testresource.
|
||||
# In that case i comment the Online Resource block and uncomment the
|
||||
|
@ -22,6 +22,7 @@ def get_exchange_rate():
|
|||
# ~~~~~~~~~~~~~~~~~~~~~
|
||||
# Online Resource block:
|
||||
# ~~~~~~~~~~~~~~~~~~~~~
|
||||
error = "SNB did not update the currencies for today."
|
||||
today = datetime.now().strftime("%Y-%m-%d")
|
||||
SNB_URL = 'https://www.snb.ch/selector/de/mmr/exfeed/rss'
|
||||
urlsocket = urllib.request.urlopen(SNB_URL)
|
||||
|
@ -63,14 +64,17 @@ def get_exchange_rate():
|
|||
datetime_str.rsplit(':', 1)),
|
||||
"%Y-%m-%dT%H:%M:%S%z").strftime(
|
||||
"%Y-%m-%d")
|
||||
except:
|
||||
except Exception as e:
|
||||
print('%s (%s)' % (e, type(e)))
|
||||
try:
|
||||
date = datetime.strptime(''.join(
|
||||
datetime_str.rsplit(':', 1)),
|
||||
"%Y-%m-%dT%H:%M:%S.%f%z").strftime(
|
||||
"%Y-%m-%d")
|
||||
continue
|
||||
except Exception as e:
|
||||
print('%s (%s)' % (e, type(e)))
|
||||
continue
|
||||
# Print dates for development:
|
||||
# print("date:", date, "today:", today)
|
||||
# only the values of today are used so check for date in XML:
|
||||
|
@ -126,7 +130,8 @@ def get_exchange_rate():
|
|||
# Print the Dictionary:
|
||||
# print(exchange_rates)
|
||||
else:
|
||||
exchange_rates = "SNB did not update the currencies for today."
|
||||
exchange_rates = error
|
||||
assert False
|
||||
return(exchange_rates, today)
|
||||
|
||||
# for development its preferable to see that the for loop is done:
|
||||
|
|
|
@ -12,19 +12,15 @@
|
|||
{% if currency_USD_list %}
|
||||
<table>
|
||||
<tr>
|
||||
{% for element in currency_USD_list %}
|
||||
{% for key, value in element.items %}
|
||||
<th scope="col">{{ key }}</th>
|
||||
{% endfor %}
|
||||
<th scope="col">DATE</th>
|
||||
<th scope="col">RATE</th>
|
||||
<tr>
|
||||
{% for currency in currency_USD_list %}
|
||||
<td scope="col">{{ currency.date.date }}</td>
|
||||
<td scope="col">{{ currency.exchange_rate_to_chf }}</td>
|
||||
|
||||
{% endfor %}
|
||||
<tr>
|
||||
{% for element in currency_USD_list %}
|
||||
<tr>
|
||||
{% for key, value in element.items %}
|
||||
<td scope="col">{{ value }}</td>
|
||||
{% endfor %}
|
||||
<tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
{% else %}
|
||||
<p class="alert">
|
||||
|
@ -34,22 +30,18 @@
|
|||
<br>
|
||||
<h3> EURO: </h3>
|
||||
{% if currency_EUR_list %}
|
||||
<table>
|
||||
<tr>
|
||||
{% for element in currency_EUR_list %}
|
||||
{% for key, value in element.items %}
|
||||
<th scope="col">{{ key }}</th>
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
<tr>
|
||||
{% for element in currency_EUR_list %}
|
||||
<tr>
|
||||
{% for key, value in element.items %}
|
||||
<td scope="col">{{ value }}</td>
|
||||
{% endfor %}
|
||||
<tr>
|
||||
<table>
|
||||
<tr>
|
||||
<th scope="col">DATE</th>
|
||||
<th scope="col">RATE</th>
|
||||
<tr>
|
||||
{% for currency in currency_EUR_list %}
|
||||
<td scope="col">{{ currency.date.date }}</td>
|
||||
<td scope="col">{{ currency.exchange_rate_to_chf }}</td>
|
||||
|
||||
{% endfor %}
|
||||
</table>
|
||||
<tr>
|
||||
</table>
|
||||
{% else %}
|
||||
<p class="alert">
|
||||
currency_EUR_list missing.
|
||||
|
@ -58,22 +50,18 @@
|
|||
<br>
|
||||
<h3> Japanese Yenn: </h3>
|
||||
{% if currency_JPY_list %}
|
||||
<table>
|
||||
<tr>
|
||||
{% for element in currency_JPY_list %}
|
||||
{% for key, value in element.items %}
|
||||
<th scope="col">{{ key }}</th>
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
<tr>
|
||||
{% for element in currency_JPY_list %}
|
||||
<tr>
|
||||
{% for key, value in element.items %}
|
||||
<td scope="col">{{ value }}</td>
|
||||
{% endfor %}
|
||||
<tr>
|
||||
<table>
|
||||
<tr>
|
||||
<th scope="col">DATE</th>
|
||||
<th scope="col">RATE</th>
|
||||
<tr>
|
||||
{% for currency in currency_JPY_list %}
|
||||
<td scope="col">{{ currency.date.date }}</td>
|
||||
<td scope="col">{{ currency.exchange_rate_to_chf }}</td>
|
||||
|
||||
{% endfor %}
|
||||
</table>
|
||||
<tr>
|
||||
</table>
|
||||
{% else %}
|
||||
<p class="alert">
|
||||
currency_JPY_list missing.
|
||||
|
@ -82,29 +70,25 @@
|
|||
<br>
|
||||
<h3> Great Britain Pounds: </h3>
|
||||
{% if currency_GBP_list %}
|
||||
<table>
|
||||
<tr>
|
||||
{% for element in currency_GBP_list %}
|
||||
{% for key, value in element.items %}
|
||||
<th scope="col">{{ key }}</th>
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
<tr>
|
||||
{% for element in currency_GBP_list %}
|
||||
<tr>
|
||||
{% for key, value in element.items %}
|
||||
<td scope="col">{{ value }}</td>
|
||||
{% endfor %}
|
||||
<tr>
|
||||
<table>
|
||||
<tr>
|
||||
<th scope="col">DATE</th>
|
||||
<th scope="col">RATE</th>
|
||||
<tr>
|
||||
{% for currency in currency_GBP_list %}
|
||||
<td scope="col">{{ currency.date.date }}</td>
|
||||
<td scope="col">{{ currency.exchange_rate_to_chf }}</td>
|
||||
|
||||
{% endfor %}
|
||||
</table>
|
||||
<tr>
|
||||
</table>
|
||||
{% else %}
|
||||
<p class="alert">
|
||||
currency_GBP_list missing.
|
||||
</p>
|
||||
{% endif %}
|
||||
<br>
|
||||
<!-- <br>
|
||||
<!-- <br>
|
||||
<br>
|
||||
<h2> The full List of Currencies </h2>
|
||||
<ul>
|
||||
{% for currency in currency_list %}
|
||||
|
|
|
@ -12,7 +12,7 @@ def currencies(request):
|
|||
# prepares the view all dynamicaly.
|
||||
# It can grow in terms of more Currencies over time automaticaly.
|
||||
# try:
|
||||
today = '1970-01-01'
|
||||
today = ''
|
||||
raw_data = ''
|
||||
raw_data, today = exchange_rates.get_exchange_rate()
|
||||
# except Exception as e:
|
||||
|
@ -21,9 +21,7 @@ def currencies(request):
|
|||
message_no = "Already querried today: "
|
||||
message_yes = " Updated successfully: "
|
||||
count_raw_data = 0
|
||||
if raw_data == "SNB did not update the currencies for today.":
|
||||
message = """Die SNB hat die Währungsliste noch nicht aktualisiert."""
|
||||
else:
|
||||
if raw_data != "SNB did not update the currencies for today.":
|
||||
for currency, rate in raw_data.items():
|
||||
count_raw_data += 1
|
||||
if ExchangeRate.objects.filter(
|
||||
|
@ -91,7 +89,7 @@ def currencies(request):
|
|||
message_no = message_no.replace(",", "!", 1) # replace first , with !
|
||||
message_no = message_no[::-1] # invert the string back
|
||||
message_yes = message_yes[::-1] # invert the string
|
||||
message_yes = message_yes.replace(",", "!", 1) # replace first , with !
|
||||
message_yes = message_yes.replace(",", "!", 1) # replace f. , with !
|
||||
message_yes = message_yes[::-1] # invert the string back
|
||||
|
||||
if len(message_no) > 24 and len(message_yes) > 23:
|
||||
|
@ -102,21 +100,26 @@ def currencies(request):
|
|||
message = message_yes
|
||||
elif datetime.datetime.today().isoweekday() == 6:
|
||||
message = """Die Abfrage wurde ohne ergebniss beendet.
|
||||
Es ist Samstag, die SNB publiziert nur an Arbeitstagen neue Kurse...
|
||||
Es ist Samstag, die SNB publiziert nur an Arbeitstagen
|
||||
neue Kurse...
|
||||
"""
|
||||
elif datetime.datetime.today().isoweekday() == 7:
|
||||
message = """Die Abfrage wurde ohne ergebniss beendet.
|
||||
Es ist Sonntag, die SNB publiziert nur an Arbeitstagen neue Kurse...
|
||||
Es ist Sonntag, die SNB publiziert nur an Arbeitstagen
|
||||
neue Kurse...
|
||||
"""
|
||||
else:
|
||||
message = """Die Abfrage wurde ohne ergebniss beendet.
|
||||
Kann es sein dass die SNB aufgrund eines Feiertages geschlossen ist?
|
||||
Kann es sein dass die SNB aufgrund eines Feiertages
|
||||
geschlossen ist?
|
||||
"""
|
||||
else:
|
||||
message = "Die SNB hat die Währungsliste noch nicht aktualisiert."
|
||||
currency_list = ExchangeRate.objects.all()
|
||||
currency_USD_list = ExchangeRate.objects.filter(name__name='USD').values()
|
||||
currency_EUR_list = ExchangeRate.objects.filter(name__name='EUR').values()
|
||||
currency_JPY_list = ExchangeRate.objects.filter(name__name='JPY').values()
|
||||
currency_GBP_list = ExchangeRate.objects.filter(name__name='GBP').values()
|
||||
currency_USD_list = ExchangeRate.objects.filter(name__name='USD')
|
||||
currency_EUR_list = ExchangeRate.objects.filter(name__name='EUR')
|
||||
currency_JPY_list = ExchangeRate.objects.filter(name__name='JPY')
|
||||
currency_GBP_list = ExchangeRate.objects.filter(name__name='GBP')
|
||||
# -------------------------------------------------------------------
|
||||
# -------------------------------------------------------------------
|
||||
# I leave this part in the document as history.
|
||||
|
|
Loading…
Reference in New Issue