Corrected a race condition of strings in views.py and the related parts of exchange_rates.py

This commit is contained in:
Ivan Hörler 2018-01-11 07:54:51 +01:00
parent aca21b4353
commit 6cf9d77cae
3 changed files with 141 additions and 165 deletions

View File

@ -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!')

View File

@ -87,21 +87,6 @@
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>

View File

@ -11,19 +11,13 @@ 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):
@ -32,12 +26,10 @@ def currencies(request):
else:
if ExchangeRate_date.objects.filter(date=today)[:1]:
try:
# lustigerweise gibt .values() den value und den id
# 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()
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))
@ -54,8 +46,6 @@ def currencies(request):
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))
@ -85,6 +75,8 @@ def currencies(request):
% (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
@ -113,8 +105,6 @@ def currencies(request):
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')
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})