diff --git a/django/didgeridoo/currencies/exchange_rates.py b/django/didgeridoo/currencies/exchange_rates.py index a68ca2f..24f7c46 100644 --- a/django/didgeridoo/currencies/exchange_rates.py +++ b/django/didgeridoo/currencies/exchange_rates.py @@ -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!') diff --git a/django/didgeridoo/currencies/templates/currencies/index.html b/django/didgeridoo/currencies/templates/currencies/index.html index 9f228a4..de264d1 100644 --- a/django/didgeridoo/currencies/templates/currencies/index.html +++ b/django/didgeridoo/currencies/templates/currencies/index.html @@ -29,33 +29,53 @@ {% endif %}

EURO:

- {% if currency_EUR_list %} - - - - - - {% for currency in currency_EUR_list %} - - + {% if currency_EUR_list %} +
DATERATE
{{ currency.date.date }}{{ currency.exchange_rate_to_chf }}
+ + + + + {% for currency in currency_EUR_list %} + + - {% endfor %} - -
DATERATE
{{ currency.date.date }}{{ currency.exchange_rate_to_chf }}
- {% else %} -

- currency_EUR_list missing. -

- {% endif %} -
+ {% endfor %} + + + {% else %} +

+ currency_EUR_list missing. +

+ {% endif %} +

Japanese Yenn:

- {% if currency_JPY_list %} + {% if currency_JPY_list %} + + + + + + {% for currency in currency_JPY_list %} + + + + {% endfor %} + +
DATERATE
{{ currency.date.date }}{{ currency.exchange_rate_to_chf }}
+ {% else %} +

+ currency_JPY_list missing. +

+ {% endif %} +
+

Great Britain Pounds:

+ {% if currency_GBP_list %} - {% for currency in currency_JPY_list %} + {% for currency in currency_GBP_list %} @@ -64,44 +84,9 @@
DATE RATE
{{ currency.date.date }} {{ currency.exchange_rate_to_chf }}
{% else %}

- currency_JPY_list missing. + currency_GBP_list missing.

{% endif %} -
-

Great Britain Pounds:

- {% if currency_GBP_list %} - - - - - - {% for currency in currency_GBP_list %} - - - - {% endfor %} - -
DATERATE
{{ currency.date.date }}{{ currency.exchange_rate_to_chf }}
- {% else %} -

- currency_GBP_list missing. -

- {% endif %} - diff --git a/django/didgeridoo/currencies/views.py b/django/didgeridoo/currencies/views.py index cdb4e7c..b4a6fe9 100644 --- a/django/didgeridoo/currencies/views.py +++ b/django/didgeridoo/currencies/views.py @@ -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})