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