diff --git a/django/didgeridoo/currencies/exchange_rates.py b/django/didgeridoo/currencies/exchange_rates.py
index 173de19..bbc50a6 100644
--- a/django/didgeridoo/currencies/exchange_rates.py
+++ b/django/didgeridoo/currencies/exchange_rates.py
@@ -25,11 +25,12 @@ def get_exchange_rate():
# urlsocket = urllib.request.urlopen(SNB_URL)
# root = ET.parse(urlsocket)
# root = ET.ElementTree(root)
-
+ # today = datetime.now().strftime("%Y-%m-%d")
# ~~~~~~~~~~~~~~~~~~~~~
# development block:
# ~~~~~~~~~~~~~~~~~~~~~
root = ET.ElementTree(file='rss')
+ today = "2018-01-01"
# ~~~~~~~~~~~~~~~~~~~~~
# Namespaces
@@ -42,8 +43,7 @@ def get_exchange_rate():
# Pathvariables to XML Namespaces
rate_path = 'cb:statistics/cb:exchangeRate/'
observation_path = 'cb:statistics/cb:exchangeRate/cb:observation/'
- # today = datetime.now().strftime("%Y-%m-%d")
- today = "2018-01-02"
+
exchange_rates = {}
for item in root.findall('none:item', ns):
diff --git a/django/didgeridoo/currencies/models.py b/django/didgeridoo/currencies/models.py
index ca6ed15..e9fe0e7 100644
--- a/django/didgeridoo/currencies/models.py
+++ b/django/didgeridoo/currencies/models.py
@@ -1,9 +1,23 @@
from django.db import models
+class ExchangeRate_name(models.Model):
+ name = models.CharField(max_length=200, unique=True)
+
+ def __str__(self):
+ return self.name
+
+
+class ExchangeRate_date(models.Model):
+ date = models.DateField('%Y-%m-%d', unique_for_date=True)
+
+ def __str__(self):
+ return str(self.date)
+
+
class ExchangeRate(models.Model):
- name = models.CharField(max_length=200)
- date = models.DateField('%Y-%m-%d', null=True)
+ name = models.ForeignKey(ExchangeRate_name)
+ date = models.ForeignKey(ExchangeRate_date)
exchange_rate_to_chf = models.FloatField(max_length=5)
def __str__(self):
diff --git a/django/didgeridoo/currencies/templates/currencies/index.html b/django/didgeridoo/currencies/templates/currencies/index.html
index d61c474..d466c35 100644
--- a/django/didgeridoo/currencies/templates/currencies/index.html
+++ b/django/didgeridoo/currencies/templates/currencies/index.html
@@ -23,15 +23,19 @@
{% endifchanged %}
{% endfor %}
-->
-
+ {% else %}
+
+ currency_list missing.
+
+ {% endif %}
{{ message }}
{% for currency in currency_list %}
-
- {{ currency.date }} :
- {{ currency.name }} :
+ {{ currency.date.date }} :
+ {{ currency.name.name }} :
{{ currency.exchange_rate_to_chf }}
{% endfor %}
@@ -47,11 +51,6 @@
{% endfor %}
- {% else %}
-
- Something whent totaly wrong.
-
- {% endif %}
today
@@ -78,11 +77,9 @@
{% endfor %}
- count_date
+ count_raw_data
- -
- {{ count_date }}
-
+ - {{ count_raw_data }}
count_currencies
diff --git a/django/didgeridoo/currencies/views.py b/django/didgeridoo/currencies/views.py
index b2c2b63..774513a 100644
--- a/django/didgeridoo/currencies/views.py
+++ b/django/didgeridoo/currencies/views.py
@@ -1,5 +1,5 @@
from django.shortcuts import render
-from currencies.models import ExchangeRate
+from currencies.models import ExchangeRate, ExchangeRate_date, ExchangeRate_name
from currencies import exchange_rates
@@ -11,17 +11,53 @@ def currencies(request):
raw_data, today = exchange_rates.get_exchange_rate()
message_no = "Already querried today: "
message_yes = " Updated successfully: "
+ count_raw_data = 0
for currency, rate in raw_data.items():
- if ExchangeRate.objects.filter(date=today, name=currency)[:1]:
+ count_raw_data += 1
+ if ExchangeRate_date.objects.filter(date__date=today) \
+ and ExchangeRate_name.objects.get(name=currency):
message_no += currency + ", "
+ # A: https://stackoverflow.com/a/27802801/4061870
else:
- e = ExchangeRate.objects.create(
- name=currency,
- exchange_rate_to_chf=rate,
- date=today
- )
- e.save()
- message_yes += currency + ", "
+ if ExchangeRate_date.objects.filter(date=today)[:1]:
+ try:
+ 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('date_dict %s (%s)' % (e, type(e)))
+ else:
+ try:
+ exdate = ExchangeRate_date.objects.create(date=today)
+ exdate.save()
+ except Exception as e:
+ print('exdate %s (%s)' % (e, type(e)))
+ if ExchangeRate_name.objects.filter(name=currency)[:1]:
+ try:
+ name_dict = ExchangeRate_name.objects.get(name=currency)
+ name = name_dict[0]['name']
+ name_id = name_dict[0]['id']
+ except Exception as e:
+ print('exdate %s (%s)' % (e, type(e)))
+ else:
+ try:
+ exname = ExchangeRate_name.objects.create(name=currency)
+ exname.save()
+ except Exception as e:
+ print('exname %s (%s)' % (e, type(e)))
+ try:
+ exrate = ExchangeRate.objects.create(
+ # name_id=name_id,
+ name_id=ExchangeRate_name.objects.only('id').get(name=currency).id,
+ # date_id=date_id,
+ date_id=ExchangeRate_date.objects.only('id').get(date=today).id,
+ exchange_rate_to_chf=rate,
+ )
+ exrate.save()
+ message_yes += currency + ", "
+
+ except Exception as e:
+ print('exrate %s (%s)' % (e, type(e)))
# prepare messages:
message_no = message_no[::-1] # invert the string
@@ -39,41 +75,42 @@ def currencies(request):
message = message_yes
else:
message = "something whent wrong"
-
+ currency_list = ExchangeRate.objects.all()
# prepare data to be displayed in a html table:
# https://stackoverflow.com/questions/8749158/removing-duplicates-from-dictionary#8749473
# atomar_dates
# 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
- unique_dates_list = ExchangeRate.objects.values_list('date', flat=True).distinct()
- # atomar_currenies
- unique_currencies_list = ExchangeRate.objects.values_list('name', flat=True).distinct()
- # search for currencies in a date and apend them to the list
- currency_list = []
- count_date = 0
- count_currencies = 0
- for unique_date in unique_dates_list:
- count_date += 1
- currency_dict = {}
- currency_dict['date'] = unique_date
- for unique_currency in unique_currencies_list:
- count_currencies += 1
- try:
- temp = ExchangeRate.objects.filter(date=unique_date, name=unique_currency).values() #A
- exchange_rate_to_chf = temp[0]['exchange_rate_to_chf']
- currency_dict = currency_dict.update({unique_currency: exchange_rate_to_chf}) #B
- except Exception as e:
- print('%s (%s)' % (e, type(e)))
- currency_list.append(currency_dict)
- assert False
+ # dates_list = ExchangeRate_date.objects.values_list('date', flat=True)
+ # # atomar_currenies
+ # currencies_list = ExchangeRate_name.objects.values_list('name', flat=True)
+ # # search for currencies in a date and apend them to the list
+ # currency_list = []
+ # count_date = 0
+ # count_currencies = 0
+ # for date in dates_list:
+ # count_date += 1
+ # currency_dict = {}
+ # currency_dict['date'] = date
+ # for currency in currencies_list:
+ # count_currencies += 1
+ # try:
+ # temp = ExchangeRate.objects.objects.only('exchange_rate_to_chf').get(name=currency).id
+ # #temp = ExchangeRate.objects.filter(date=unique_date, name=currency).values() # A
+ # exchange_rate_to_chf = temp[0]['exchange_rate_to_chf']
+ # currency_dict = currency_dict.update({currency: exchange_rate_to_chf}) # B
+ # except Exception as e:
+ # print('%s (%s)' % (e, type(e)))
+ # currency_list.append(currency_dict)
+ # assert False
return render(request,
'currencies/index.html',
{'currency_list': currency_list,
'raw_data': raw_data,
'today': today,
- 'unique_dates_list': unique_dates_list,
- 'unique_currencies_list': unique_currencies_list,
- 'count_date': count_date,
- 'count_currencies': count_currencies,
- 'currency_dict': currency_dict,
+ # 'unique_dates_list': unique_dates_list,
+ # 'unique_currencies_list': unique_currencies_list,
+ 'count_raw_data': count_raw_data,
+ # 'count_currencies': count_currencies,
+ # 'currency_dict': currency_dict,
'message': message})
diff --git a/django/didgeridoo/rss b/django/didgeridoo/rss
index 009f10e..b1dc21a 100644
--- a/django/didgeridoo/rss
+++ b/django/didgeridoo/rss
@@ -37,7 +37,7 @@
CH: 1.3081 CHF = 1 GBP 2017-11-27 Tägliche Kurse (11:00)
https://www.snb.ch
1 GBP = 1.3081 CHF (Tägliche Kurse (11:00); 2017-11-27T12:16:53.767+01:00)
- 2018-01-02T12:16:53.767+01:00
+ 2018-01-01T12:16:53.767+01:00
de
@@ -66,7 +66,7 @@
CH: 0.8813 CHF = 100 JPY 2017-11-27 Tägliche Kurse (11:00)
https://www.snb.ch
100 JPY = 0.8813 CHF (Tägliche Kurse (11:00); 2017-11-27T12:16:53.760+01:00)
- 2018-01-02T12:16:53.760+01:00
+ 2018-01-01T12:16:53.760+01:00
de
@@ -96,7 +96,7 @@
CH: 1.1697 CHF = 1 EUR 2017-11-27 Tägliche Kurse (11:00)
https://www.snb.ch
1 EUR = 1.1697 CHF (Tägliche Kurse (11:00); 2017-11-27T12:16:53.750+01:00)
- 2018-01-02T12:16:53.750+01:00
+ 2018-01-01T12:16:53.750+01:00
de
@@ -125,7 +125,7 @@
CH: 0.9803 CHF = 1 USD 2017-11-27 Tägliche Kurse (11:00)
https://www.snb.ch
1 USD = 0.9803 CHF (Tägliche Kurse (11:00); 2017-11-27T12:16:53.737+01:00)
- 2018-01-02T12:16:53.737+01:00
+ 2018-01-01T12:16:53.737+01:00
de