diff --git a/django/didgeridoo/currencies/exchange_rates.py b/django/didgeridoo/currencies/exchange_rates.py
index bbc50a6..df33d6e 100644
--- a/django/didgeridoo/currencies/exchange_rates.py
+++ b/django/didgeridoo/currencies/exchange_rates.py
@@ -18,19 +18,26 @@ def get_exchange_rate():
# To develop i need a testresource.
# In that case i comment the Online Resource block and uncomment the
# development Block...
+
# ~~~~~~~~~~~~~~~~~~~~~
# Online Resource block:
# ~~~~~~~~~~~~~~~~~~~~~
- # SNB_URL = 'https://www.snb.ch/selector/de/mmr/exfeed/rss'
- # urlsocket = urllib.request.urlopen(SNB_URL)
- # root = ET.parse(urlsocket)
- # root = ET.ElementTree(root)
- # today = datetime.now().strftime("%Y-%m-%d")
+ today = datetime.now().strftime("%Y-%m-%d")
+ SNB_URL = 'https://www.snb.ch/selector/de/mmr/exfeed/rss'
+ urlsocket = urllib.request.urlopen(SNB_URL)
+ root = ET.parse(urlsocket)
+ root = ET.ElementTree(root)
+
# ~~~~~~~~~~~~~~~~~~~~~
# development block:
# ~~~~~~~~~~~~~~~~~~~~~
- root = ET.ElementTree(file='rss')
- today = "2018-01-01"
+ # today = "2018-01-03"
+ # try:
+ # root = ET.ElementTree(file='rss')
+ # except Exception as e:
+ # print('exchange_rates.py_urlsocket failed %s (
+ # %s) on date: %s for %s'
+ # % (e, type(e), root))
# ~~~~~~~~~~~~~~~~~~~~~
# Namespaces
@@ -43,9 +50,7 @@ def get_exchange_rate():
# Pathvariables to XML Namespaces
rate_path = 'cb:statistics/cb:exchangeRate/'
observation_path = 'cb:statistics/cb:exchangeRate/cb:observation/'
-
exchange_rates = {}
-
for item in root.findall('none:item', ns):
# THE CURRENCY DATE:
datetime_str = item.find('dc:date', ns).text
@@ -53,20 +58,19 @@ def get_exchange_rate():
# 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:
- try:
- date = datetime.strptime(''.join(
- datetime_str.rsplit(':', 1)),
- "%Y-%m-%dT%H:%M:%S.%f%z").strftime(
- "%Y-%m-%d")
- except Exception as e:
- print('%s (%s)' % (e, type(e)))
try:
date = datetime.strptime(''.join(
datetime_str.rsplit(':', 1)),
"%Y-%m-%dT%H:%M:%S%z").strftime(
"%Y-%m-%d")
- except Exception as e:
- print('%s (%s)' % (e, type(e)))
+ except:
+ try:
+ date = datetime.strptime(''.join(
+ datetime_str.rsplit(':', 1)),
+ "%Y-%m-%dT%H:%M:%S.%f%z").strftime(
+ "%Y-%m-%d")
+ except Exception as e:
+ print('%s (%s)' % (e, type(e)))
# Print dates for development:
# print("date:", date, "today:", today)
# only the values of today are used so check for date in XML:
@@ -122,7 +126,8 @@ def get_exchange_rate():
# Print the Dictionary:
# print(exchange_rates)
else:
- break
+ exchange_rates = "SNB did not update the currencies for today."
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/migrations/0001_initial.py b/django/didgeridoo/currencies/migrations/0001_initial.py
index c7f74a7..ee9f2f4 100644
--- a/django/didgeridoo/currencies/migrations/0001_initial.py
+++ b/django/didgeridoo/currencies/migrations/0001_initial.py
@@ -1,8 +1,9 @@
# -*- coding: utf-8 -*-
-# Generated by Django 1.10.7 on 2017-12-18 18:01
+# Generated by Django 1.10.7 on 2018-01-09 18:21
from __future__ import unicode_literals
from django.db import migrations, models
+import django.db.models.deletion
class Migration(migrations.Migration):
@@ -17,8 +18,31 @@ class Migration(migrations.Migration):
name='ExchangeRate',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
- ('name', models.CharField(max_length=200, unique=True)),
- ('exchange_rate_to_chf', models.FloatField(max_length=5)),
+ ('exchange_rate_to_chf', models.DecimalField(decimal_places=5, max_digits=12)),
],
),
+ migrations.CreateModel(
+ name='ExchangeRate_date',
+ fields=[
+ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+ ('date', models.DateField(unique_for_date=True, verbose_name='%Y-%m-%d')),
+ ],
+ ),
+ migrations.CreateModel(
+ name='ExchangeRate_name',
+ fields=[
+ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+ ('name', models.CharField(max_length=200, unique=True)),
+ ],
+ ),
+ migrations.AddField(
+ model_name='exchangerate',
+ name='date',
+ field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='currencies.ExchangeRate_date'),
+ ),
+ migrations.AddField(
+ model_name='exchangerate',
+ name='name',
+ field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='currencies.ExchangeRate_name'),
+ ),
]
diff --git a/django/didgeridoo/currencies/migrations/0002_exchangerate_date.py b/django/didgeridoo/currencies/migrations/0002_exchangerate_date.py
deleted file mode 100644
index 37fb831..0000000
--- a/django/didgeridoo/currencies/migrations/0002_exchangerate_date.py
+++ /dev/null
@@ -1,20 +0,0 @@
-# -*- coding: utf-8 -*-
-# Generated by Django 1.10.7 on 2017-12-27 10:05
-from __future__ import unicode_literals
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('currencies', '0001_initial'),
- ]
-
- operations = [
- migrations.AddField(
- model_name='exchangerate',
- name='date',
- field=models.DateField(null=True),
- ),
- ]
diff --git a/django/didgeridoo/currencies/migrations/0003_auto_20171227_1119.py b/django/didgeridoo/currencies/migrations/0003_auto_20171227_1119.py
deleted file mode 100644
index c6283a2..0000000
--- a/django/didgeridoo/currencies/migrations/0003_auto_20171227_1119.py
+++ /dev/null
@@ -1,20 +0,0 @@
-# -*- coding: utf-8 -*-
-# Generated by Django 1.10.7 on 2017-12-27 10:19
-from __future__ import unicode_literals
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('currencies', '0002_exchangerate_date'),
- ]
-
- operations = [
- migrations.AlterField(
- model_name='exchangerate',
- name='date',
- field=models.DateField(null=True, verbose_name='%Y-%m-%d'),
- ),
- ]
diff --git a/django/didgeridoo/currencies/migrations/0004_auto_20171229_1708.py b/django/didgeridoo/currencies/migrations/0004_auto_20171229_1708.py
deleted file mode 100644
index ad84626..0000000
--- a/django/didgeridoo/currencies/migrations/0004_auto_20171229_1708.py
+++ /dev/null
@@ -1,20 +0,0 @@
-# -*- coding: utf-8 -*-
-# Generated by Django 1.10.7 on 2017-12-29 16:08
-from __future__ import unicode_literals
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('currencies', '0003_auto_20171227_1119'),
- ]
-
- operations = [
- migrations.AlterField(
- model_name='exchangerate',
- name='name',
- field=models.CharField(max_length=200),
- ),
- ]
diff --git a/django/didgeridoo/currencies/migrations/0005_auto_20171229_1735.py b/django/didgeridoo/currencies/migrations/0005_auto_20171229_1735.py
deleted file mode 100644
index 3ac6227..0000000
--- a/django/didgeridoo/currencies/migrations/0005_auto_20171229_1735.py
+++ /dev/null
@@ -1,20 +0,0 @@
-# -*- coding: utf-8 -*-
-# Generated by Django 1.10.7 on 2017-12-29 16:35
-from __future__ import unicode_literals
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('currencies', '0004_auto_20171229_1708'),
- ]
-
- operations = [
- migrations.AlterField(
- model_name='exchangerate',
- name='date',
- field=models.DateField(null=True, verbose_name='%Y-%m-%dT%H:%M:%S'),
- ),
- ]
diff --git a/django/didgeridoo/currencies/migrations/0006_auto_20171229_1747.py b/django/didgeridoo/currencies/migrations/0006_auto_20171229_1747.py
deleted file mode 100644
index a2a330b..0000000
--- a/django/didgeridoo/currencies/migrations/0006_auto_20171229_1747.py
+++ /dev/null
@@ -1,20 +0,0 @@
-# -*- coding: utf-8 -*-
-# Generated by Django 1.10.7 on 2017-12-29 16:47
-from __future__ import unicode_literals
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('currencies', '0005_auto_20171229_1735'),
- ]
-
- operations = [
- migrations.AlterField(
- model_name='exchangerate',
- name='date',
- field=models.DateTimeField(null=True, verbose_name='%Y-%m-%dT%H:%M:%S'),
- ),
- ]
diff --git a/django/didgeridoo/currencies/migrations/0007_auto_20171229_1806.py b/django/didgeridoo/currencies/migrations/0007_auto_20171229_1806.py
deleted file mode 100644
index 2455467..0000000
--- a/django/didgeridoo/currencies/migrations/0007_auto_20171229_1806.py
+++ /dev/null
@@ -1,20 +0,0 @@
-# -*- coding: utf-8 -*-
-# Generated by Django 1.10.7 on 2017-12-29 17:06
-from __future__ import unicode_literals
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
-
- dependencies = [
- ('currencies', '0006_auto_20171229_1747'),
- ]
-
- operations = [
- migrations.AlterField(
- model_name='exchangerate',
- name='date',
- field=models.DateField(null=True, verbose_name='%Y-%m-%d'),
- ),
- ]
diff --git a/django/didgeridoo/currencies/models.py b/django/didgeridoo/currencies/models.py
index e9fe0e7..73a09ba 100644
--- a/django/didgeridoo/currencies/models.py
+++ b/django/didgeridoo/currencies/models.py
@@ -1,4 +1,6 @@
from django.db import models
+from decimal import Decimal
+import datetime
class ExchangeRate_name(models.Model):
@@ -18,7 +20,7 @@ class ExchangeRate_date(models.Model):
class ExchangeRate(models.Model):
name = models.ForeignKey(ExchangeRate_name)
date = models.ForeignKey(ExchangeRate_date)
- exchange_rate_to_chf = models.FloatField(max_length=5)
+ exchange_rate_to_chf = models.DecimalField(max_digits=12, decimal_places=5)
def __str__(self):
- return self.name
+ return str(self.name)
diff --git a/django/didgeridoo/currencies/templates/currencies/index.html b/django/didgeridoo/currencies/templates/currencies/index.html
index d466c35..851d38f 100644
--- a/django/didgeridoo/currencies/templates/currencies/index.html
+++ b/django/didgeridoo/currencies/templates/currencies/index.html
@@ -3,34 +3,109 @@
-
-
Currencies in CHF
- {% if currency_list %}
-
+
+ {% else %}
+
+ currency_USD_list missing.
+
+ {% endif %}
+
+
EURO:
+ {% if currency_EUR_list %}
+
+
+ {% for element in currency_EUR_list %}
+ {% for key, value in element.items %}
+ {{ key }} |
+ {% endfor %}
+ {% endfor %}
+
+ {% for element in currency_EUR_list %}
+
+ {% for key, value in element.items %}
+ {{ value }} |
+ {% endfor %}
+
+ {% endfor %}
+
{% else %}
- currency_list missing.
+ currency_EUR_list missing.
{% endif %}
-
{{ message }}
+
Japanese Yenn:
+ {% if currency_JPY_list %}
+
+
+ {% for element in currency_JPY_list %}
+ {% for key, value in element.items %}
+ {{ key }} |
+ {% endfor %}
+ {% endfor %}
+
+ {% for element in currency_JPY_list %}
+
+ {% for key, value in element.items %}
+ {{ value }} |
+ {% endfor %}
+
+ {% endfor %}
+
+ {% else %}
+
+ currency_JPY_list missing.
+
+ {% endif %}
+
Great Britain Pounds:
+ {% if currency_GBP_list %}
+
+
+ {% for element in currency_GBP_list %}
+ {% for key, value in element.items %}
+ {{ key }} |
+ {% endfor %}
+ {% endfor %}
+
+ {% for element in currency_GBP_list %}
+
+ {% for key, value in element.items %}
+ {{ value }} |
+ {% endfor %}
+
+ {% endfor %}
+
+ {% else %}
+
+ currency_GBP_list missing.
+
+ {% endif %}
+
+
- raw_data
-
- {% for key, value in raw_data.items %}
- -
- {{ key }} :
- {{ value }}
-
- {% endfor %}
-
-
- today
-
-
- unique_dates_list
-
- {% for key in unique_dates_list %}
- -
- {{ key }}
-
- {% endfor %}
-
-
- unique_currencies_list
-
- {% for key in unique_currencies_list %}
- -
- {{ key }}
-
- {% endfor %}
-
-
count_raw_data
-
- count_currencies
-
- -
- {{ count_currencies }}
-
-
-
- currencies_list
-
- {% for key in currencies_list.values %}
- -
- {{ key }}
-
- {% endfor %}
-
- currency_dict
-
- {% for key, value in currency_dict %}
- -
- {{ key }}
-
- -
- {{ value }}
-
- {% endfor %}
-
+ {{ count_raw_data }} -->