Corrected a race condition of strings in views.py and the related parts of exchange_rates.py
This commit is contained in:
parent
aca21b4353
commit
6cf9d77cae
|
@ -22,7 +22,6 @@ 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)
|
||||
|
@ -53,12 +52,16 @@ def get_exchange_rate():
|
|||
observation_path = 'cb:statistics/cb:exchangeRate/cb:observation/'
|
||||
exchange_rates = {}
|
||||
for item in root.findall('none:item', ns):
|
||||
# for eatch item n the list we grab the release date
|
||||
# to evaluate if its fresh data or old:
|
||||
|
||||
# THE CURRENCY DATE:
|
||||
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 is with milliseconds:
|
||||
# seams like snb striked the microsecond somewhere
|
||||
# between Nov. and Dez. 2017 so maybe first check
|
||||
# time type is with milliseconds:
|
||||
try:
|
||||
date = datetime.strptime(''.join(
|
||||
datetime_str.rsplit(':', 1)),
|
||||
|
@ -79,15 +82,18 @@ def get_exchange_rate():
|
|||
# print("date:", date, "today:", today)
|
||||
# only the values of today are used so check for date in XML:
|
||||
if date == today:
|
||||
# now search for the currency exchange rate:
|
||||
target_currency = item.find(rate_path +
|
||||
'cb:targetCurrency', ns).text
|
||||
value = float(item.find(observation_path +
|
||||
'cb:value', ns).text)
|
||||
value = float(value) # convert to float
|
||||
foreign_value = value # copy to new value to have both.
|
||||
# because it's dangerous to check for present, i check for none
|
||||
# here and have to do something in there so i set the target to 1.
|
||||
|
||||
if item.find(observation_path + 'cb:unit_mult', ns) is None:
|
||||
# because it's dangerous to check for present,
|
||||
# i check for none here and have to set the target
|
||||
# to 1. as im multiplying it later.
|
||||
unit_mult = float("1.0")
|
||||
else:
|
||||
# shift left by 2 digits with "/"
|
||||
|
@ -121,7 +127,7 @@ def get_exchange_rate():
|
|||
value = value / unit_mult
|
||||
# truncate it to decimal values provided by the xml:
|
||||
foreign_value_round = round(foreign_value, 5)
|
||||
# Print nice setup of all calculated currencys for Dev:
|
||||
# Print nice setup of all calculated currencys for development:
|
||||
# print("date:", date, " 1 ", target_currency, " costs: ",
|
||||
# CHFvalue, "CHF and 1 ", base_currency, " costs: ",
|
||||
# FOREIGNvalue_round, target_currency)
|
||||
|
@ -130,9 +136,5 @@ def get_exchange_rate():
|
|||
# Print the Dictionary:
|
||||
# print(exchange_rates)
|
||||
else:
|
||||
exchange_rates = error
|
||||
assert False
|
||||
break
|
||||
return(exchange_rates, today)
|
||||
|
||||
# for development its preferable to see that the for loop is done:
|
||||
# print('no more fresh data!')
|
||||
|
|
|
@ -29,33 +29,53 @@
|
|||
{% endif %}
|
||||
<br>
|
||||
<h3> EURO: </h3>
|
||||
{% if currency_EUR_list %}
|
||||
<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>
|
||||
{% if currency_EUR_list %}
|
||||
<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 %}
|
||||
<tr>
|
||||
</table>
|
||||
{% else %}
|
||||
<p class="alert">
|
||||
currency_EUR_list missing.
|
||||
</p>
|
||||
{% endif %}
|
||||
<br>
|
||||
{% endfor %}
|
||||
<tr>
|
||||
</table>
|
||||
{% else %}
|
||||
<p class="alert">
|
||||
currency_EUR_list missing.
|
||||
</p>
|
||||
{% endif %}
|
||||
<br>
|
||||
<h3> Japanese Yenn: </h3>
|
||||
{% if currency_JPY_list %}
|
||||
{% if currency_JPY_list %}
|
||||
<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 %}
|
||||
<tr>
|
||||
</table>
|
||||
{% else %}
|
||||
<p class="alert">
|
||||
currency_JPY_list missing.
|
||||
</p>
|
||||
{% endif %}
|
||||
<br>
|
||||
<h3> Great Britain Pounds: </h3>
|
||||
{% if currency_GBP_list %}
|
||||
<table>
|
||||
<tr>
|
||||
<th scope="col">DATE</th>
|
||||
<th scope="col">RATE</th>
|
||||
<tr>
|
||||
{% for currency in currency_JPY_list %}
|
||||
{% for currency in currency_GBP_list %}
|
||||
<td scope="col">{{ currency.date.date }}</td>
|
||||
<td scope="col">{{ currency.exchange_rate_to_chf }}</td>
|
||||
|
||||
|
@ -64,44 +84,9 @@
|
|||
</table>
|
||||
{% else %}
|
||||
<p class="alert">
|
||||
currency_JPY_list missing.
|
||||
currency_GBP_list missing.
|
||||
</p>
|
||||
{% endif %}
|
||||
<br>
|
||||
<h3> Great Britain Pounds: </h3>
|
||||
{% if currency_GBP_list %}
|
||||
<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 %}
|
||||
<tr>
|
||||
</table>
|
||||
{% else %}
|
||||
<p class="alert">
|
||||
currency_GBP_list missing.
|
||||
</p>
|
||||
{% endif %}
|
||||
<!-- <br>
|
||||
<br>
|
||||
<h2> The full List of Currencies </h2>
|
||||
<ul>
|
||||
{% for currency in currency_list %}
|
||||
<li>
|
||||
{{ currency.date.date }} :
|
||||
{{ currency.name.name }} :
|
||||
{{ currency.exchange_rate_to_chf }}
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
count_raw_data
|
||||
<br>
|
||||
{{ count_raw_data }} -->
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -11,110 +11,100 @@ def currencies(request):
|
|||
# evaluates if the values are already stored and
|
||||
# prepares the view all dynamicaly.
|
||||
# It can grow in terms of more Currencies over time automaticaly.
|
||||
# try:
|
||||
today = ''
|
||||
raw_data = ''
|
||||
raw_data, today = exchange_rates.get_exchange_rate()
|
||||
# except Exception as e:
|
||||
# print('get_exchange_rate() %s (%s) on %s'
|
||||
# % (e, type(e), today))
|
||||
print('views raw_data: ', raw_data) # assert False
|
||||
message_no = "Already querried today: "
|
||||
message_yes = " Updated successfully: "
|
||||
count_raw_data = 0
|
||||
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(
|
||||
date__date=today,
|
||||
name__name=currency):
|
||||
message_no += currency + ", "
|
||||
# A: https://stackoverflow.com/a/27802801/4061870
|
||||
else:
|
||||
if ExchangeRate_date.objects.filter(date=today)[:1]:
|
||||
try:
|
||||
# lustigerweise gibt .values() den value und den id
|
||||
# zurück. Ohne .values() gibts nur den "value"
|
||||
date_dict = ExchangeRate_date.objects.filter(
|
||||
date=today).values()
|
||||
date = date_dict[0]['date']
|
||||
date_id = date_dict[0]['id']
|
||||
except Exception as e:
|
||||
print('exdate_exists %s (%s) on %s'
|
||||
% (e, type(e), today))
|
||||
else:
|
||||
try:
|
||||
exdate = ExchangeRate_date.objects.create(
|
||||
date=today)
|
||||
exdate.save()
|
||||
except Exception as e:
|
||||
print('exdate_not_exists %s (%s) for %s'
|
||||
% (e, type(e), today))
|
||||
if ExchangeRate_name.objects.filter(
|
||||
name=currency)[:1]:
|
||||
try:
|
||||
name_dict = ExchangeRate_name.objects.filter(
|
||||
name=currency).values()
|
||||
name = name_dict[0]['name']
|
||||
name_id = name_dict[0]['id']
|
||||
except Exception as e:
|
||||
print('exname_exists %s (%s) on %s'
|
||||
% (e, type(e), currency))
|
||||
else:
|
||||
try:
|
||||
exname = ExchangeRate_name.objects.create(
|
||||
name=currency)
|
||||
exname.save()
|
||||
except Exception as e:
|
||||
print('exname_not_exists %s (%s) on %s'
|
||||
% (e, type(e), currency))
|
||||
try:
|
||||
exrate = ExchangeRate.objects.create(
|
||||
# name_id=name_id,
|
||||
name_id=ExchangeRate_name.objects.get(
|
||||
name=currency).id,
|
||||
# date_id=date_id,
|
||||
date_id=ExchangeRate_date.objects.get(
|
||||
date=today).id,
|
||||
exchange_rate_to_chf=rate,
|
||||
)
|
||||
exrate.save()
|
||||
message_yes += currency + ", "
|
||||
|
||||
except Exception as e:
|
||||
print('exrate_create %s (%s) on %s for %s'
|
||||
% (e, type(e), currency, today))
|
||||
|
||||
# prepare messages:
|
||||
message_no = message_no[::-1] # invert the string
|
||||
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 f. , with !
|
||||
message_yes = message_yes[::-1] # invert the string back
|
||||
|
||||
if len(message_no) > 24 and len(message_yes) > 23:
|
||||
message = message_no + message_yes
|
||||
elif len(message_no) > 24:
|
||||
message = message_no
|
||||
elif len(message_yes) > 23:
|
||||
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...
|
||||
"""
|
||||
elif datetime.datetime.today().isoweekday() == 7:
|
||||
message = """Die Abfrage wurde ohne ergebniss beendet.
|
||||
Es ist Sonntag, die SNB publiziert nur an Arbeitstagen
|
||||
neue Kurse...
|
||||
"""
|
||||
for currency, rate in raw_data.items():
|
||||
if ExchangeRate.objects.filter(
|
||||
date__date=today,
|
||||
name__name=currency):
|
||||
message_no += currency + ", "
|
||||
# A: https://stackoverflow.com/a/27802801/4061870
|
||||
else:
|
||||
message = """Die Abfrage wurde ohne ergebniss beendet.
|
||||
Kann es sein dass die SNB aufgrund eines Feiertages
|
||||
geschlossen ist?
|
||||
"""
|
||||
if ExchangeRate_date.objects.filter(date=today)[:1]:
|
||||
try:
|
||||
# lustigerweise gibt .values() den value und die id
|
||||
# zurück. Ohne .values() gibts nur den "value"
|
||||
date_dict = ExchangeRate_date.objects.filter(
|
||||
date=today).values()
|
||||
except Exception as e:
|
||||
print('exdate_exists %s (%s) on %s'
|
||||
% (e, type(e), today))
|
||||
else:
|
||||
try:
|
||||
exdate = ExchangeRate_date.objects.create(
|
||||
date=today)
|
||||
exdate.save()
|
||||
except Exception as e:
|
||||
print('exdate_not_exists %s (%s) for %s'
|
||||
% (e, type(e), today))
|
||||
if ExchangeRate_name.objects.filter(
|
||||
name=currency)[:1]:
|
||||
try:
|
||||
name_dict = ExchangeRate_name.objects.filter(
|
||||
name=currency).values()
|
||||
except Exception as e:
|
||||
print('exname_exists %s (%s) on %s'
|
||||
% (e, type(e), currency))
|
||||
else:
|
||||
try:
|
||||
exname = ExchangeRate_name.objects.create(
|
||||
name=currency)
|
||||
exname.save()
|
||||
except Exception as e:
|
||||
print('exname_not_exists %s (%s) on %s'
|
||||
% (e, type(e), currency))
|
||||
try:
|
||||
exrate = ExchangeRate.objects.create(
|
||||
# name_id=name_id,
|
||||
name_id=ExchangeRate_name.objects.get(
|
||||
name=currency).id,
|
||||
# date_id=date_id,
|
||||
date_id=ExchangeRate_date.objects.get(
|
||||
date=today).id,
|
||||
exchange_rate_to_chf=rate,
|
||||
)
|
||||
exrate.save()
|
||||
message_yes += currency + ", "
|
||||
|
||||
except Exception as e:
|
||||
print('exrate_create %s (%s) on %s for %s'
|
||||
% (e, type(e), currency, today))
|
||||
|
||||
# prepare messages:
|
||||
# python can not swap a char insinde a sting so i have
|
||||
# to invert and swap and then invert back:
|
||||
message_no = message_no[::-1] # invert the string
|
||||
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 f. , with !
|
||||
message_yes = message_yes[::-1] # invert the string back
|
||||
|
||||
if len(message_no) > 24 and len(message_yes) > 23:
|
||||
message = message_no + message_yes
|
||||
elif len(message_no) > 24:
|
||||
message = message_no
|
||||
elif len(message_yes) > 23:
|
||||
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...
|
||||
"""
|
||||
elif datetime.datetime.today().isoweekday() == 7:
|
||||
message = """Die Abfrage wurde ohne ergebniss beendet.
|
||||
Es ist Sonntag, die SNB publiziert nur an Arbeitstagen
|
||||
neue Kurse...
|
||||
"""
|
||||
else:
|
||||
message = "Die SNB hat die Währungsliste noch nicht aktualisiert."
|
||||
message = """Die Abfrage wurde ohne ergebniss beendet.
|
||||
Kann es sein dass die SNB aufgrund eines Feiertages
|
||||
geschlossen ist?
|
||||
"""
|
||||
currency_list = ExchangeRate.objects.all()
|
||||
currency_USD_list = ExchangeRate.objects.filter(name__name='USD')
|
||||
currency_EUR_list = ExchangeRate.objects.filter(name__name='EUR')
|
||||
|
@ -174,5 +164,4 @@ def currencies(request):
|
|||
'currency_EUR_list': currency_EUR_list,
|
||||
'currency_JPY_list': currency_JPY_list,
|
||||
'currency_GBP_list': currency_GBP_list,
|
||||
'count_raw_data': count_raw_data,
|
||||
'message': message})
|
||||
|
|
Loading…
Reference in New Issue