2017-12-18 09:03:52 +01:00
|
|
|
from django.shortcuts import render
|
2018-01-10 00:58:34 +01:00
|
|
|
import datetime
|
|
|
|
from currencies.models import (ExchangeRate,
|
|
|
|
ExchangeRate_date,
|
|
|
|
ExchangeRate_name)
|
2017-12-19 22:33:00 +01:00
|
|
|
from currencies import exchange_rates
|
2017-12-18 09:03:52 +01:00
|
|
|
|
2017-12-18 19:19:08 +01:00
|
|
|
|
2017-12-19 21:28:05 +01:00
|
|
|
def currencies(request):
|
2018-01-02 14:51:06 +01:00
|
|
|
# this function fetches the data from exchange_rates.py
|
|
|
|
# evaluates if the values are already stored and
|
|
|
|
# prepares the view all dynamicaly.
|
|
|
|
# It can grow in terms of more Currencies over time automaticaly.
|
2018-01-10 22:43:22 +01:00
|
|
|
today = ''
|
2018-01-11 19:21:13 +01:00
|
|
|
raw_data = []
|
|
|
|
try:
|
|
|
|
raw_data, today = exchange_rates.get_exchange_rate()
|
|
|
|
except Exception as e:
|
|
|
|
print('views raw_data: ', raw_data, 'error:', e) # assert False
|
2018-01-02 14:51:06 +01:00
|
|
|
message_no = "Already querried today: "
|
|
|
|
message_yes = " Updated successfully: "
|
2018-01-11 19:21:13 +01:00
|
|
|
if raw_data:
|
|
|
|
print(raw_data)
|
|
|
|
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
|
2018-01-03 08:34:25 +01:00
|
|
|
else:
|
2018-01-11 19:21:13 +01:00
|
|
|
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))
|
2018-01-03 08:34:25 +01:00
|
|
|
try:
|
2018-01-11 19:21:13 +01:00
|
|
|
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 + ", "
|
2018-01-02 14:51:06 +01:00
|
|
|
|
2018-01-11 19:21:13 +01:00
|
|
|
except Exception as e:
|
|
|
|
print('exrate_create %s (%s) on %s for %s'
|
|
|
|
% (e, type(e), currency, today))
|
2018-01-02 14:51:06 +01:00
|
|
|
|
2018-01-11 07:54:51 +01:00
|
|
|
# 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...
|
|
|
|
"""
|
2018-01-10 22:43:22 +01:00
|
|
|
else:
|
2018-01-11 07:54:51 +01:00
|
|
|
message = """Die Abfrage wurde ohne ergebniss beendet.
|
|
|
|
Kann es sein dass die SNB aufgrund eines Feiertages
|
|
|
|
geschlossen ist?
|
|
|
|
"""
|
2018-01-03 08:34:25 +01:00
|
|
|
currency_list = ExchangeRate.objects.all()
|
2018-01-10 22:43:22 +01:00
|
|
|
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')
|
2018-01-10 00:58:34 +01:00
|
|
|
# -------------------------------------------------------------------
|
|
|
|
# -------------------------------------------------------------------
|
|
|
|
# I leave this part in the document as history.
|
|
|
|
# Problem is that i get the expected List with dictionaries like:
|
|
|
|
# view_currencies_list[
|
|
|
|
# {'date': '2017-12-29, 'USD':'1.00', 'EUR':'1.00', 'GBP':'1.00', 'JPY':'1.00'},
|
|
|
|
# {'date': '2017-12-30, 'USD':'1.00', 'EUR':'1.00', 'GBP':'1.00', 'JPY':'1.00'},
|
|
|
|
# ]
|
|
|
|
# but the dict of 'date:' does not seam to deliver the same values as
|
|
|
|
# the dict's of key name:'USD' im not able to fix this in moment.
|
|
|
|
# nor am i able to generate a HTML table with date | USD | EUR | ...
|
|
|
|
# -------------------------------------------------------------------
|
|
|
|
# -------------------------------------------------------------------
|
2018-01-02 14:51:06 +01:00
|
|
|
# prepare data to be displayed in a html table:
|
|
|
|
# https://stackoverflow.com/questions/8749158/removing-duplicates-from-dictionary#8749473
|
|
|
|
# A: https://stackoverflow.com/questions/37205793/django-values-list-vs-values#37205928
|
|
|
|
# B: https://stackoverflow.com/questions/6521892/how-to-access-a-dictionary-key-value-present-inside-a-list
|
2018-01-03 08:34:25 +01:00
|
|
|
# # search for currencies in a date and apend them to the list
|
2018-01-10 00:58:34 +01:00
|
|
|
# view_currency_list = []
|
|
|
|
# view_currencies_list = ExchangeRate_name.objects.all()
|
|
|
|
# view_dates_list = ExchangeRate_date.objects.all()
|
2018-01-03 08:34:25 +01:00
|
|
|
# count_date = 0
|
|
|
|
# count_currencies = 0
|
2018-01-10 00:58:34 +01:00
|
|
|
# for view_date in view_dates_list:
|
2018-01-03 08:34:25 +01:00
|
|
|
# count_date += 1
|
2018-01-10 00:58:34 +01:00
|
|
|
# view_currency_dict = {view_date}
|
|
|
|
# # view_currency_dict.update({})
|
|
|
|
# for view_currency in view_currencies_list:
|
2018-01-03 08:34:25 +01:00
|
|
|
# count_currencies += 1
|
|
|
|
# try:
|
2018-01-10 00:58:34 +01:00
|
|
|
# x = ExchangeRate.objects.filter(date__date=str(
|
|
|
|
# view_date),
|
|
|
|
# name__name=str(
|
|
|
|
# view_currency
|
|
|
|
# )).values() # A
|
|
|
|
# view_exchange_rate_to_chf = x[0]['exchange_rate_to_chf']
|
2018-01-03 08:34:25 +01:00
|
|
|
# except Exception as e:
|
2018-01-10 00:58:34 +01:00
|
|
|
# print('prepare_view %s (%s) for %s on %s is %s'
|
|
|
|
# % (e, type(e), view_currency, view_date,
|
|
|
|
# view_exchange_rate_to_chf))
|
|
|
|
# view_exchange_rate_to_chf = " "
|
|
|
|
#
|
|
|
|
# view_currency_dict.update({view_currency:
|
|
|
|
# view_exchange_rate_to_chf}) # B
|
|
|
|
#
|
|
|
|
# view_currency_list.append(view_currency_dict)
|
|
|
|
# assert False
|
2017-12-19 21:28:05 +01:00
|
|
|
return render(request,
|
|
|
|
'currencies/index.html',
|
2017-12-29 17:02:33 +01:00
|
|
|
{'currency_list': currency_list,
|
2018-01-10 00:58:34 +01:00
|
|
|
'currency_USD_list': currency_USD_list,
|
|
|
|
'currency_EUR_list': currency_EUR_list,
|
|
|
|
'currency_JPY_list': currency_JPY_list,
|
|
|
|
'currency_GBP_list': currency_GBP_list,
|
2017-12-29 19:26:01 +01:00
|
|
|
'message': message})
|