corrected currencies app and added different view.
This commit is contained in:
parent
6273941c8d
commit
6290eb7a6a
|
@ -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!')
|
||||
|
|
|
@ -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'),
|
||||
),
|
||||
]
|
||||
|
|
|
@ -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),
|
||||
),
|
||||
]
|
|
@ -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'),
|
||||
),
|
||||
]
|
|
@ -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),
|
||||
),
|
||||
]
|
|
@ -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'),
|
||||
),
|
||||
]
|
|
@ -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'),
|
||||
),
|
||||
]
|
|
@ -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'),
|
||||
),
|
||||
]
|
|
@ -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)
|
||||
|
|
|
@ -3,34 +3,109 @@
|
|||
<link rel="stylesheet" href="{{ STATIC_URL }}/static/admin/css/base.css" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="content" class="flex">
|
||||
<h1>Currencies in CHF</h1>
|
||||
{% if currency_list %}
|
||||
<!-- <table>
|
||||
<div id="content" class="flex">
|
||||
<h1>Currencies in CHF</h1>
|
||||
<p> {{ message }} </p>
|
||||
<h2> Frühere Daten: </h2>
|
||||
<br>
|
||||
<h3> US Dollars: </h3>
|
||||
{% if currency_USD_list %}
|
||||
<table>
|
||||
<tr>
|
||||
<th scope="col">DATE</th>
|
||||
{% for currency in currency_list %}
|
||||
<th scope="col">{{ currency.name }}</th>
|
||||
{% endfor %}
|
||||
</tr>
|
||||
{% for currencydate in currency_list %}
|
||||
{% ifchanged currencydate %}
|
||||
<tr>{{ currency.date }}
|
||||
{% for currency in currency_list %}
|
||||
<td>{{ currency.exchange_rate_to_chf }}</td>
|
||||
{% for element in currency_USD_list %}
|
||||
{% for key, value in element.items %}
|
||||
<th scope="col">{{ key }}</th>
|
||||
{% endfor %}
|
||||
</tr>
|
||||
{% endifchanged %}
|
||||
{% endfor %}
|
||||
<tr>
|
||||
{% for element in currency_USD_list %}
|
||||
<tr>
|
||||
{% for key, value in element.items %}
|
||||
<td scope="col">{{ value }}</td>
|
||||
{% endfor %}
|
||||
<tr>
|
||||
{% endfor %}
|
||||
</table> -->
|
||||
</table>
|
||||
{% else %}
|
||||
<p class="alert">
|
||||
currency_USD_list missing.
|
||||
</p>
|
||||
{% endif %}
|
||||
<br>
|
||||
<h3> EURO: </h3>
|
||||
{% if currency_EUR_list %}
|
||||
<table>
|
||||
<tr>
|
||||
{% for element in currency_EUR_list %}
|
||||
{% for key, value in element.items %}
|
||||
<th scope="col">{{ key }}</th>
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
<tr>
|
||||
{% for element in currency_EUR_list %}
|
||||
<tr>
|
||||
{% for key, value in element.items %}
|
||||
<td scope="col">{{ value }}</td>
|
||||
{% endfor %}
|
||||
<tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
{% else %}
|
||||
<p class="alert">
|
||||
currency_list missing.
|
||||
currency_EUR_list missing.
|
||||
</p>
|
||||
{% endif %}
|
||||
<br>
|
||||
<p> {{ message }} </p>
|
||||
<h3> Japanese Yenn: </h3>
|
||||
{% if currency_JPY_list %}
|
||||
<table>
|
||||
<tr>
|
||||
{% for element in currency_JPY_list %}
|
||||
{% for key, value in element.items %}
|
||||
<th scope="col">{{ key }}</th>
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
<tr>
|
||||
{% for element in currency_JPY_list %}
|
||||
<tr>
|
||||
{% for key, value in element.items %}
|
||||
<td scope="col">{{ value }}</td>
|
||||
{% endfor %}
|
||||
<tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
{% else %}
|
||||
<p class="alert">
|
||||
currency_JPY_list missing.
|
||||
</p>
|
||||
{% endif %}
|
||||
<br>
|
||||
<h3> Great Britain Pounds: </h3>
|
||||
{% if currency_GBP_list %}
|
||||
<table>
|
||||
<tr>
|
||||
{% for element in currency_GBP_list %}
|
||||
{% for key, value in element.items %}
|
||||
<th scope="col">{{ key }}</th>
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
<tr>
|
||||
{% for element in currency_GBP_list %}
|
||||
<tr>
|
||||
{% for key, value in element.items %}
|
||||
<td scope="col">{{ value }}</td>
|
||||
{% endfor %}
|
||||
<tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
{% else %}
|
||||
<p class="alert">
|
||||
currency_GBP_list missing.
|
||||
</p>
|
||||
{% endif %}
|
||||
<br>
|
||||
<!-- <br>
|
||||
<h2> The full List of Currencies </h2>
|
||||
<ul>
|
||||
{% for currency in currency_list %}
|
||||
<li>
|
||||
|
@ -40,74 +115,9 @@
|
|||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<br>
|
||||
<!-- Development helpers: -->
|
||||
raw_data
|
||||
<ul>
|
||||
{% for key, value in raw_data.items %}
|
||||
<li>
|
||||
{{ key }} :
|
||||
{{ value }}
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<br>
|
||||
today
|
||||
<ul>
|
||||
<li>
|
||||
{{ today }}
|
||||
</li>
|
||||
</ul>
|
||||
<br>
|
||||
unique_dates_list
|
||||
<ul>
|
||||
{% for key in unique_dates_list %}
|
||||
<li>
|
||||
{{ key }}
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<br>
|
||||
unique_currencies_list
|
||||
<ul>
|
||||
{% for key in unique_currencies_list %}
|
||||
<li>
|
||||
{{ key }}
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<br>
|
||||
count_raw_data
|
||||
<ul>
|
||||
<li>{{ count_raw_data }}</li>
|
||||
</ul>
|
||||
<br>
|
||||
count_currencies
|
||||
<ul>
|
||||
<li>
|
||||
{{ count_currencies }}
|
||||
</li>
|
||||
</ul>
|
||||
<br>
|
||||
currencies_list
|
||||
<ul>
|
||||
{% for key in currencies_list.values %}
|
||||
<li>
|
||||
{{ key }}
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
currency_dict
|
||||
<ul>
|
||||
{% for key, value in currency_dict %}
|
||||
<li>
|
||||
{{ key }}
|
||||
</li>
|
||||
<li>
|
||||
{{ value }}
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{{ count_raw_data }} -->
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
from django.shortcuts import render
|
||||
from currencies.models import ExchangeRate, ExchangeRate_date, ExchangeRate_name
|
||||
import datetime
|
||||
from currencies.models import (ExchangeRate,
|
||||
ExchangeRate_date,
|
||||
ExchangeRate_name)
|
||||
from currencies import exchange_rates
|
||||
|
||||
|
||||
|
@ -8,109 +11,165 @@ 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 = '1970-01-01'
|
||||
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))
|
||||
message_no = "Already querried today: "
|
||||
message_yes = " Updated successfully: "
|
||||
count_raw_data = 0
|
||||
for currency, rate in raw_data.items():
|
||||
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:
|
||||
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
|
||||
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 first , 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
|
||||
if raw_data == "SNB did not update the currencies for today.":
|
||||
message = """Die SNB hat die Währungsliste noch nicht aktualisiert."""
|
||||
else:
|
||||
message = "something whent wrong"
|
||||
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 first , 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 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').values()
|
||||
currency_EUR_list = ExchangeRate.objects.filter(name__name='EUR').values()
|
||||
currency_JPY_list = ExchangeRate.objects.filter(name__name='JPY').values()
|
||||
currency_GBP_list = ExchangeRate.objects.filter(name__name='GBP').values()
|
||||
# -------------------------------------------------------------------
|
||||
# -------------------------------------------------------------------
|
||||
# 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 | ...
|
||||
# -------------------------------------------------------------------
|
||||
# -------------------------------------------------------------------
|
||||
# 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
|
||||
# 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 = []
|
||||
# view_currency_list = []
|
||||
# view_currencies_list = ExchangeRate_name.objects.all()
|
||||
# view_dates_list = ExchangeRate_date.objects.all()
|
||||
# count_date = 0
|
||||
# count_currencies = 0
|
||||
# for date in dates_list:
|
||||
# for view_date in view_dates_list:
|
||||
# count_date += 1
|
||||
# currency_dict = {}
|
||||
# currency_dict['date'] = date
|
||||
# for currency in currencies_list:
|
||||
# view_currency_dict = {view_date}
|
||||
# # view_currency_dict.update({})
|
||||
# for view_currency in view_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
|
||||
# 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']
|
||||
# except Exception as e:
|
||||
# print('%s (%s)' % (e, type(e)))
|
||||
# currency_list.append(currency_dict)
|
||||
# assert False
|
||||
# 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
|
||||
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,
|
||||
'currency_USD_list': currency_USD_list,
|
||||
'currency_EUR_list': currency_EUR_list,
|
||||
'currency_JPY_list': currency_JPY_list,
|
||||
'currency_GBP_list': currency_GBP_list,
|
||||
'count_raw_data': count_raw_data,
|
||||
# 'count_currencies': count_currencies,
|
||||
# 'currency_dict': currency_dict,
|
||||
'message': message})
|
||||
|
|
|
@ -35,6 +35,7 @@ ALLOWED_HOSTS = [
|
|||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
'currencies',
|
||||
'webshop.apps.WebshopConfig',
|
||||
'django_extensions',
|
||||
'django.contrib.admin',
|
||||
|
@ -43,7 +44,6 @@ INSTALLED_APPS = [
|
|||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'currencies',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
|
|
|
@ -37,7 +37,7 @@
|
|||
<title>CH: 1.3081 CHF = 1 GBP 2017-11-27 Tägliche Kurse (11:00)</title>
|
||||
<link>https://www.snb.ch</link>
|
||||
<description>1 GBP = 1.3081 CHF (Tägliche Kurse (11:00); 2017-11-27T12:16:53.767+01:00)</description>
|
||||
<dc:date>2018-01-01T12:16:53.767+01:00</dc:date>
|
||||
<dc:date>2018-01-03T12:16:53.767+01:00</dc:date>
|
||||
<dc:language>de</dc:language>
|
||||
<cb:statistics rdf:parseType="Resource">
|
||||
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#Statistics"/>
|
||||
|
@ -66,7 +66,7 @@
|
|||
<title>CH: 0.8813 CHF = 100 JPY 2017-11-27 Tägliche Kurse (11:00)</title>
|
||||
<link>https://www.snb.ch</link>
|
||||
<description>100 JPY = 0.8813 CHF (Tägliche Kurse (11:00); 2017-11-27T12:16:53.760+01:00)</description>
|
||||
<dc:date>2018-01-01T12:16:53.760+01:00</dc:date>
|
||||
<dc:date>2018-01-03T12:16:53.760+01:00</dc:date>
|
||||
<dc:language>de</dc:language>
|
||||
<cb:statistics rdf:parseType="Resource">
|
||||
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#Statistics"/>
|
||||
|
@ -96,7 +96,7 @@
|
|||
<title>CH: 1.1697 CHF = 1 EUR 2017-11-27 Tägliche Kurse (11:00)</title>
|
||||
<link>https://www.snb.ch</link>
|
||||
<description>1 EUR = 1.1697 CHF (Tägliche Kurse (11:00); 2017-11-27T12:16:53.750+01:00)</description>
|
||||
<dc:date>2018-01-01T12:16:53.750+01:00</dc:date>
|
||||
<dc:date>2018-01-03T12:16:53.750+01:00</dc:date>
|
||||
<dc:language>de</dc:language>
|
||||
<cb:statistics rdf:parseType="Resource">
|
||||
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#Statistics"/>
|
||||
|
@ -125,7 +125,7 @@
|
|||
<title>CH: 0.9803 CHF = 1 USD 2017-11-27 Tägliche Kurse (11:00)</title>
|
||||
<link>https://www.snb.ch</link>
|
||||
<description>1 USD = 0.9803 CHF (Tägliche Kurse (11:00); 2017-11-27T12:16:53.737+01:00)</description>
|
||||
<dc:date>2018-01-01T12:16:53.737+01:00</dc:date>
|
||||
<dc:date>2018-01-03T12:16:53.737+01:00</dc:date>
|
||||
<dc:language>de</dc:language>
|
||||
<cb:statistics rdf:parseType="Resource">
|
||||
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#Statistics"/>
|
||||
|
|
Loading…
Reference in New Issue