From 344447b9097617f0b8730051628cc8f777638eba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Mon, 18 Dec 2017 09:03:52 +0100 Subject: [PATCH 01/25] UN-finished Push of currency branch. Preview only, do not change. Ivan on charge. --- django/didgeridoo/currencies/__init__.py | 0 django/didgeridoo/currencies/admin.py | 3 + django/didgeridoo/currencies/apps.py | 5 + .../didgeridoo/currencies/exchange_rates.py | 117 ++++++++++++++++++ .../currencies/migrations/__init__.py | 0 django/didgeridoo/currencies/models.py | 20 +++ django/didgeridoo/currencies/tests.py | 3 + django/didgeridoo/currencies/views.py | 3 + django/didgeridoo/didgeridoo/settings.py | 3 +- django/didgeridoo/webshop/models.py | 8 -- 10 files changed, 153 insertions(+), 9 deletions(-) create mode 100644 django/didgeridoo/currencies/__init__.py create mode 100644 django/didgeridoo/currencies/admin.py create mode 100644 django/didgeridoo/currencies/apps.py create mode 100644 django/didgeridoo/currencies/exchange_rates.py create mode 100644 django/didgeridoo/currencies/migrations/__init__.py create mode 100644 django/didgeridoo/currencies/models.py create mode 100644 django/didgeridoo/currencies/tests.py create mode 100644 django/didgeridoo/currencies/views.py diff --git a/django/didgeridoo/currencies/__init__.py b/django/didgeridoo/currencies/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/django/didgeridoo/currencies/admin.py b/django/didgeridoo/currencies/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/django/didgeridoo/currencies/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/django/didgeridoo/currencies/apps.py b/django/didgeridoo/currencies/apps.py new file mode 100644 index 0000000..055501d --- /dev/null +++ b/django/didgeridoo/currencies/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class CurrenciesConfig(AppConfig): + name = 'currencies' diff --git a/django/didgeridoo/currencies/exchange_rates.py b/django/didgeridoo/currencies/exchange_rates.py new file mode 100644 index 0000000..ebbbb12 --- /dev/null +++ b/django/didgeridoo/currencies/exchange_rates.py @@ -0,0 +1,117 @@ +from datetime import datetime +import urllib.request +import xml.etree.ElementTree as ET + +today = datetime.now().strftime("%Y-%m-%d") + +""" this method calls a rss/XML Resource + of Currency's and parses it to our + needed exchange rate values. + The return is a dictionary carring + Key:Value pairs of new currencys. +""" + + +def get_exchange_rate(): + # During weekends there are no updates. + # 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) + + # ~~~~~~~~~~~~~~~~~~~~~ + # development block: + # ~~~~~~~~~~~~~~~~~~~~~ + root = ET.ElementTree(file='rss') + ns = {'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', + 'none': 'http://purl.org/rss/1.0/', + 'dc': 'http://purl.org/dc/elements/1.1/', + 'dcterms': 'http://purl.org/dc/terms/', + 'cb': 'http://www.cbwiki.net/wiki/index.php/Specification_1.2/' + } + # Pathvariables to XML Namespaces + none_path = 'none:item/' + rate_path = 'cb:statistics/cb:exchangeRate/' + observation_path = 'cb:statistics/cb:exchangeRate/cb:observation/' + # THE FILE DATE: + xml_datetime_string = root.find('none:channel/dcterms:created', ns).text + # because of few knowlede just trim end of string to avoid error + xml_datestring = xml_datetime_string.split('T')[0] + # parse string to date object: + xml_date = datetime.date(datetime.strptime(xml_datestring, "%Y-%m-%d")) + + for item in root.findall('none:item', ns): + # THE CURRENCY DATE: + datetime_str = item.find('dc:date', ns).text + # convert string to date object: + # https://stackoverflow.com/a/12282040/4061870 + date = datetime.strptime(''.join(datetime_str.rsplit(':', 1)), + "%Y-%m-%dT%H:%M:%S.%f%z").strftime("%Y-%m-%d") + # only the values of today are used so check for date: + if date == today: + title = item.find('none:title', ns).text + base_currency = item.find(rate_path + + 'cb:baseCurrency', ns).text + target_currency = item.find(rate_path + + 'cb:targetCurrency', ns).text + CHFvalue = float(item.find(observation_path + + 'cb:value', ns).text) + CHFvalue = float(CHFvalue) # convert to float + FOREIGNvalue = CHFvalue # copy to new value to have both. + unit = item.find(observation_path + 'cb:unit', ns).text + decimals = int(item.find(observation_path + + 'cb:decimals', ns).text) + # 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 0. + if item.find(observation_path + 'cb:unit_mult', ns) is None: + unit_mult = float("1.0") + else: + # shift left by 2 digits with "/" + # https://stackoverflow.com/questions/8362792/ + # because some currencys differ widly from CHF + unit_mult = item.find(observation_path + + 'cb:unit_mult', ns).text + # unit_mult defaults to '0' so we check for 8 decimal + # values (2..-6) they represent the fracton value to + # calculate the correct decimalpoint. + if unit_mult == '2': # thinking of Bitcoins + unit_mult = '0.01' + if unit_mult == '1': + unit_mult = '0.10' + if unit_mult == '-1': + unit_mult = '10' + if unit_mult == '-2': # Japan Yen + unit_mult = '100' + if unit_mult == '-3': + unit_mult = '1000' + if unit_mult == '-4': + unit_mult = '10000' + if unit_mult == '-5': + unit_mult = '100000' + if unit_mult == '-6': # indian rupies + unit_mult = '1000000' + unit_mult = float(unit_mult) # convert to float + # calculate the Currency to CHF: + FOREIGNvalue = 1 / CHFvalue + FOREIGNvalue *= unit_mult + CHFvalue = CHFvalue / unit_mult + # truncate it to decimal values provided by the xml: + FOREIGNvalue_round = round(FOREIGNvalue, 5) + # Print nice setup of all calculated currencys for Dev: + # print("date:", date, " 1 ", target_currency, " costs: ", + # CHFvalue, "CHF and 1 ", base_currency, " costs: ", + # FOREIGNvalue_round, target_currency) + exchange_rates = {target_currency: FOREIGNvalue_round} + # Print the Dictionary: + # print(exchange_rates) + return(exchange_rates) + else: + break + # for development its preferable to see that the for loop is done: + # print('no more fresh data!') diff --git a/django/didgeridoo/currencies/migrations/__init__.py b/django/didgeridoo/currencies/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/django/didgeridoo/currencies/models.py b/django/didgeridoo/currencies/models.py new file mode 100644 index 0000000..829609d --- /dev/null +++ b/django/didgeridoo/currencies/models.py @@ -0,0 +1,20 @@ +from django.db import models +import exchange_rates + + +class ExchangeRate(models.Model): + name = models.CharField(max_length=200, unique=True) + exchange_rate_to_chf = models.FloatField(max_length=5) + + def __str__(self): + return self.name + + def exchange_rates(): + exchange_rates = currencies.get_exchange_rate() + + for i in exchange_rates: + if ExchangeRate.objects.filter( + name='exchange_rates_data[dictionary_value]'): + ExchangeRate.objects.filter( + name='exchange_rates_data[dictionary_key]', + exchange_rate_to_chf=exchange_rates.value).save() diff --git a/django/didgeridoo/currencies/tests.py b/django/didgeridoo/currencies/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/django/didgeridoo/currencies/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/django/didgeridoo/currencies/views.py b/django/didgeridoo/currencies/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/django/didgeridoo/currencies/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/django/didgeridoo/didgeridoo/settings.py b/django/didgeridoo/didgeridoo/settings.py index 682b4be..3b01f6e 100644 --- a/django/didgeridoo/didgeridoo/settings.py +++ b/django/didgeridoo/didgeridoo/settings.py @@ -39,7 +39,8 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', -] + 'currencies', + ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', diff --git a/django/didgeridoo/webshop/models.py b/django/didgeridoo/webshop/models.py index 185abd4..62fbca5 100644 --- a/django/didgeridoo/webshop/models.py +++ b/django/didgeridoo/webshop/models.py @@ -29,14 +29,6 @@ class ArticleStatus(models.Model): return self.name -class ExchangeRate(models.Model): - name = models.CharField(max_length=200, unique=True) - exchange_rate_to_chf = models.FloatField(max_length=5) - - def __str__(self): - return self.name - - # Create your models here. class Category(models.Model): name = models.CharField(max_length=200, unique=True) From 409b18807bbc79e5205e95a1f343f848f3f473ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Mon, 18 Dec 2017 19:19:08 +0100 Subject: [PATCH 02/25] corrections and first try to migrate --- django/didgeridoo/currencies/index.html | 20 ++++++++++++++++ .../currencies/migrations/0001_initial.py | 24 +++++++++++++++++++ django/didgeridoo/currencies/models.py | 1 - django/didgeridoo/currencies/urls.py | 7 ++++++ django/didgeridoo/currencies/views.py | 6 ++++- django/didgeridoo/didgeridoo/settings.py | 2 +- 6 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 django/didgeridoo/currencies/index.html create mode 100644 django/didgeridoo/currencies/migrations/0001_initial.py create mode 100644 django/didgeridoo/currencies/urls.py diff --git a/django/didgeridoo/currencies/index.html b/django/didgeridoo/currencies/index.html new file mode 100644 index 0000000..41b4d61 --- /dev/null +++ b/django/didgeridoo/currencies/index.html @@ -0,0 +1,20 @@ + + + + + +
+

audioguide

+

a mobile offline audioguide

+noop +noop +noop +noop +noop +noop +

the audioguide that makes a offline usage possible.

+
+ + + diff --git a/django/didgeridoo/currencies/migrations/0001_initial.py b/django/didgeridoo/currencies/migrations/0001_initial.py new file mode 100644 index 0000000..c7f74a7 --- /dev/null +++ b/django/didgeridoo/currencies/migrations/0001_initial.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2017-12-18 18:01 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + 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)), + ], + ), + ] diff --git a/django/didgeridoo/currencies/models.py b/django/didgeridoo/currencies/models.py index 829609d..79e45c7 100644 --- a/django/didgeridoo/currencies/models.py +++ b/django/didgeridoo/currencies/models.py @@ -1,5 +1,4 @@ from django.db import models -import exchange_rates class ExchangeRate(models.Model): diff --git a/django/didgeridoo/currencies/urls.py b/django/didgeridoo/currencies/urls.py new file mode 100644 index 0000000..a3780aa --- /dev/null +++ b/django/didgeridoo/currencies/urls.py @@ -0,0 +1,7 @@ +from django.conf.urls import url + +from . import views + +urlpatterns = [ + url(r'^$', views.index, name='index'), +] diff --git a/django/didgeridoo/currencies/views.py b/django/didgeridoo/currencies/views.py index 91ea44a..47d054b 100644 --- a/django/didgeridoo/currencies/views.py +++ b/django/didgeridoo/currencies/views.py @@ -1,3 +1,7 @@ from django.shortcuts import render +from django.http import HttpResponse +import exchange_rates -# Create your views here. + +def index(request): + return HttpResponse(exchange_rates) diff --git a/django/didgeridoo/didgeridoo/settings.py b/django/didgeridoo/didgeridoo/settings.py index 3b01f6e..9d51a5b 100644 --- a/django/didgeridoo/didgeridoo/settings.py +++ b/django/didgeridoo/didgeridoo/settings.py @@ -32,6 +32,7 @@ ALLOWED_HOSTS = [] INSTALLED_APPS = [ 'webshop.apps.WebshopConfig', + 'currencies.apps.CurrenciesConfig', 'django_extensions', 'django.contrib.admin', 'django.contrib.auth', @@ -39,7 +40,6 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', - 'currencies', ] MIDDLEWARE = [ From 776f81b3c9b8f2304306227cbb064e309a6f7216 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Tue, 19 Dec 2017 21:28:05 +0100 Subject: [PATCH 03/25] testpush --- django/didgeridoo/currencies/index.html | 20 ------------------- django/didgeridoo/currencies/models.py | 10 ---------- .../templates/currencies/index.html | 19 ++++++++++++++++++ django/didgeridoo/currencies/urls.py | 4 ++-- django/didgeridoo/currencies/views.py | 17 +++++++++++++--- django/didgeridoo/didgeridoo/urls.py | 2 +- 6 files changed, 36 insertions(+), 36 deletions(-) delete mode 100644 django/didgeridoo/currencies/index.html create mode 100644 django/didgeridoo/currencies/templates/currencies/index.html diff --git a/django/didgeridoo/currencies/index.html b/django/didgeridoo/currencies/index.html deleted file mode 100644 index 41b4d61..0000000 --- a/django/didgeridoo/currencies/index.html +++ /dev/null @@ -1,20 +0,0 @@ - - - - - -
-

audioguide

-

a mobile offline audioguide

-noop -noop -noop -noop -noop -noop -

the audioguide that makes a offline usage possible.

-
- - - diff --git a/django/didgeridoo/currencies/models.py b/django/didgeridoo/currencies/models.py index 79e45c7..1edbe06 100644 --- a/django/didgeridoo/currencies/models.py +++ b/django/didgeridoo/currencies/models.py @@ -7,13 +7,3 @@ class ExchangeRate(models.Model): def __str__(self): return self.name - - def exchange_rates(): - exchange_rates = currencies.get_exchange_rate() - - for i in exchange_rates: - if ExchangeRate.objects.filter( - name='exchange_rates_data[dictionary_value]'): - ExchangeRate.objects.filter( - name='exchange_rates_data[dictionary_key]', - exchange_rate_to_chf=exchange_rates.value).save() diff --git a/django/didgeridoo/currencies/templates/currencies/index.html b/django/didgeridoo/currencies/templates/currencies/index.html new file mode 100644 index 0000000..edfd456 --- /dev/null +++ b/django/didgeridoo/currencies/templates/currencies/index.html @@ -0,0 +1,19 @@ + + + + + +
+

Music Shop

+ {% if currency_list %} +
    + {% for currency in currency_list %} +
  • {{ currency.name }} : {{ currency.exchange_rate_to_chf }}
  • + {% endfor %} +
+ {% else %} +

No categories are available.

+ {% endif %} +
+ + diff --git a/django/didgeridoo/currencies/urls.py b/django/didgeridoo/currencies/urls.py index a3780aa..2804c74 100644 --- a/django/didgeridoo/currencies/urls.py +++ b/django/didgeridoo/currencies/urls.py @@ -1,7 +1,7 @@ from django.conf.urls import url +from currencies.views import currencies -from . import views urlpatterns = [ - url(r'^$', views.index, name='index'), + url(r'^currencies/$', currencies), ] diff --git a/django/didgeridoo/currencies/views.py b/django/didgeridoo/currencies/views.py index 47d054b..7926760 100644 --- a/django/didgeridoo/currencies/views.py +++ b/django/didgeridoo/currencies/views.py @@ -1,7 +1,18 @@ from django.shortcuts import render -from django.http import HttpResponse +from .models import ExchangeRate import exchange_rates -def index(request): - return HttpResponse(exchange_rates) +def currencies(request): + # return HttpResponse("exchange_rates") + currency_list = ExchangeRate.objects.all() + raw_data = exchange_rates.get_exchange_rate() + + for i, j in raw_data.items: + ExchangeRate.objects.create( + name=i, + exchange_rate_to_chf=j) + + return render(request, + 'currencies/index.html', + {'currency_list': currency_list}) diff --git a/django/didgeridoo/didgeridoo/urls.py b/django/didgeridoo/didgeridoo/urls.py index 1eef47b..a9524c5 100644 --- a/django/didgeridoo/didgeridoo/urls.py +++ b/django/didgeridoo/didgeridoo/urls.py @@ -18,6 +18,6 @@ from django.contrib import admin urlpatterns = [ url(r'', include('webshop.urls')), - url(r'^admin/', admin.site.urls), + url(r'', include('currencies.urls')), url(r'^admin/', admin.site.urls), ] From fc5184fa778b3dc450f806616cece1c684d41556 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Tue, 19 Dec 2017 22:33:00 +0100 Subject: [PATCH 04/25] get currency from SNB rss to work --- .../didgeridoo/currencies/exchange_rates.py | 19 +- .../templates/currencies/index.html | 2 +- django/didgeridoo/currencies/views.py | 25 +- django/didgeridoo/rss | 621 ++++++++++++++++++ 4 files changed, 649 insertions(+), 18 deletions(-) create mode 100644 django/didgeridoo/rss diff --git a/django/didgeridoo/currencies/exchange_rates.py b/django/didgeridoo/currencies/exchange_rates.py index ebbbb12..2900892 100644 --- a/django/didgeridoo/currencies/exchange_rates.py +++ b/django/didgeridoo/currencies/exchange_rates.py @@ -20,15 +20,17 @@ def get_exchange_rate(): # ~~~~~~~~~~~~~~~~~~~~~ # 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) + 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') + # root = ET.ElementTree(file='rss') + # ~~~~~~~~~~~~~~~~~~~~~ + # Namespaces ns = {'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'none': 'http://purl.org/rss/1.0/', 'dc': 'http://purl.org/dc/elements/1.1/', @@ -45,7 +47,7 @@ def get_exchange_rate(): xml_datestring = xml_datetime_string.split('T')[0] # parse string to date object: xml_date = datetime.date(datetime.strptime(xml_datestring, "%Y-%m-%d")) - + exchange_rates = {} for item in root.findall('none:item', ns): # THE CURRENCY DATE: datetime_str = item.find('dc:date', ns).text @@ -107,11 +109,12 @@ def get_exchange_rate(): # print("date:", date, " 1 ", target_currency, " costs: ", # CHFvalue, "CHF and 1 ", base_currency, " costs: ", # FOREIGNvalue_round, target_currency) - exchange_rates = {target_currency: FOREIGNvalue_round} + exchange_rates.update( + {target_currency: FOREIGNvalue_round}) # Print the Dictionary: # print(exchange_rates) - return(exchange_rates) else: break + return(exchange_rates) # 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 edfd456..3c1e8a7 100644 --- a/django/didgeridoo/currencies/templates/currencies/index.html +++ b/django/didgeridoo/currencies/templates/currencies/index.html @@ -4,7 +4,7 @@
-

Music Shop

+

Currencies in CHF

{% if currency_list %}
    {% for currency in currency_list %} diff --git a/django/didgeridoo/currencies/views.py b/django/didgeridoo/currencies/views.py index 7926760..23f8952 100644 --- a/django/didgeridoo/currencies/views.py +++ b/django/didgeridoo/currencies/views.py @@ -1,18 +1,25 @@ from django.shortcuts import render -from .models import ExchangeRate -import exchange_rates +from currencies.models import ExchangeRate +from currencies import exchange_rates def currencies(request): # return HttpResponse("exchange_rates") - currency_list = ExchangeRate.objects.all() raw_data = exchange_rates.get_exchange_rate() - - for i, j in raw_data.items: - ExchangeRate.objects.create( - name=i, - exchange_rate_to_chf=j) - + for i, j in raw_data.items(): + if ExchangeRate.objects.filter(name=i): + e = ExchangeRate.objects.filter( + name=i, + ).update( + exchange_rate_to_chf=j + ) + else: + e = ExchangeRate.objects.create( + name=i, + exchange_rate_to_chf=j + ) + e.save() + currency_list = ExchangeRate.objects.all() return render(request, 'currencies/index.html', {'currency_list': currency_list}) diff --git a/django/didgeridoo/rss b/django/didgeridoo/rss new file mode 100644 index 0000000..af36b76 --- /dev/null +++ b/django/didgeridoo/rss @@ -0,0 +1,621 @@ + + + + SNB Devisenkurse + https://www.snb.ch/de/ifor/media/id/media_rss + Schweizerische Nationalbank (SNB): Devisenkurse (Ankauf Zürich 11 Uhr) + + + + + + + + + + + + + + + + + + + + + + + + + SNB + Copyright © Schweizerische Nationalbank, Zürich (Schweiz) 2017 + https://www.snb.ch/de/srv/id/disclaimer + 2017-11-28T07:50:22+01:00 + + + 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) + 2017-12-19T12:16:53.767+01:00 + de + + + CH + SNB + + + + + 1.3081 + CHF + 4 + + CHF + GBP + Tägliche Kurse (11:00) + + + daily + 2017-11-27 + + + + + + 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) + 2017-12-19T12:16:53.760+01:00 + de + + + CH + SNB + + + + + 10000.8813 + CHF + -2 + 4 + + CHF + JPY + Tägliche Kurse (11:00) + + + daily + 2017-11-27 + + + + + + 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) + 2017-12-19T12:16:53.750+01:00 + de + + + CH + SNB + + + + + 1.1697 + CHF + 4 + + CHF + EUR + Tägliche Kurse (11:00) + + + daily + 2017-11-27 + + + + + + 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) + 2017-11-29T12:16:53.737+01:00 + de + + + CH + SNB + + + + + 0.9803 + CHF + 4 + + CHF + USD + Tägliche Kurse (11:00) + + + daily + 2017-11-27 + + + + + + CH: 1.3072 CHF = 1 GBP 2017-11-24 Tägliche Kurse (11:00) + https://www.snb.ch + 1 GBP = 1.3072 CHF (Tägliche Kurse (11:00); 2017-11-24T12:19:49.733+01:00) + 2017-11-24T12:19:49.733+01:00 + de + + + CH + SNB + + + + + 1.3072 + CHF + 4 + + CHF + GBP + Tägliche Kurse (11:00) + + + daily + 2017-11-24 + + + + + + CH: 0.8806 CHF = 100 JPY 2017-11-24 Tägliche Kurse (11:00) + https://www.snb.ch + 100 JPY = 0.8806 CHF (Tägliche Kurse (11:00); 2017-11-24T12:19:49.717+01:00) + 2017-11-24T12:19:49.717+01:00 + de + + + CH + SNB + + + + + 0.8806 + CHF + -2 + 4 + + CHF + JPY + Tägliche Kurse (11:00) + + + daily + 2017-11-24 + + + + + + CH: 1.1644 CHF = 1 EUR 2017-11-24 Tägliche Kurse (11:00) + https://www.snb.ch + 1 EUR = 1.1644 CHF (Tägliche Kurse (11:00); 2017-11-24T12:19:49.710+01:00) + 2017-11-24T12:19:49.710+01:00 + de + + + CH + SNB + + + + + 1.1644 + CHF + 4 + + CHF + EUR + Tägliche Kurse (11:00) + + + daily + 2017-11-24 + + + + + + CH: 0.9809 CHF = 1 USD 2017-11-24 Tägliche Kurse (11:00) + https://www.snb.ch + 1 USD = 0.9809 CHF (Tägliche Kurse (11:00); 2017-11-24T12:19:49.703+01:00) + 2017-11-24T12:19:49.703+01:00 + de + + + CH + SNB + + + + + 0.9809 + CHF + 4 + + CHF + USD + Tägliche Kurse (11:00) + + + daily + 2017-11-24 + + + + + + CH: 1.3052 CHF = 1 GBP 2017-11-23 Tägliche Kurse (11:00) + https://www.snb.ch + 1 GBP = 1.3052 CHF (Tägliche Kurse (11:00); 2017-11-23T12:36:46.040+01:00) + 2017-11-23T12:36:46.040+01:00 + de + + + CH + SNB + + + + + 1.3052 + CHF + 4 + + CHF + GBP + Tägliche Kurse (11:00) + + + daily + 2017-11-23 + + + + + + CH: 0.8816 CHF = 100 JPY 2017-11-23 Tägliche Kurse (11:00) + https://www.snb.ch + 100 JPY = 0.8816 CHF (Tägliche Kurse (11:00); 2017-11-23T12:36:46.030+01:00) + 2017-11-23T12:36:46.030+01:00 + de + + + CH + SNB + + + + + 0.8816 + CHF + -2 + 4 + + CHF + JPY + Tägliche Kurse (11:00) + + + daily + 2017-11-23 + + + + + + CH: 1.1617 CHF = 1 EUR 2017-11-23 Tägliche Kurse (11:00) + https://www.snb.ch + 1 EUR = 1.1617 CHF (Tägliche Kurse (11:00); 2017-11-23T12:36:46.027+01:00) + 2017-11-23T12:36:46.027+01:00 + de + + + CH + SNB + + + + + 1.1617 + CHF + 4 + + CHF + EUR + Tägliche Kurse (11:00) + + + daily + 2017-11-23 + + + + + + CH: 0.9811 CHF = 1 USD 2017-11-23 Tägliche Kurse (11:00) + https://www.snb.ch + 1 USD = 0.9811 CHF (Tägliche Kurse (11:00); 2017-11-23T12:36:46.017+01:00) + 2017-11-23T12:36:46.017+01:00 + de + + + CH + SNB + + + + + 0.9811 + CHF + 4 + + CHF + USD + Tägliche Kurse (11:00) + + + daily + 2017-11-23 + + + + + + CH: 1.3109 CHF = 1 GBP 2017-11-22 Tägliche Kurse (11:00) + https://www.snb.ch + 1 GBP = 1.3109 CHF (Tägliche Kurse (11:00); 2017-11-22T12:24:04.110+01:00) + 2017-11-22T12:24:04.110+01:00 + de + + + CH + SNB + + + + + 1.3109 + CHF + 4 + + CHF + GBP + Tägliche Kurse (11:00) + + + daily + 2017-11-22 + + + + + + CH: 0.8827 CHF = 100 JPY 2017-11-22 Tägliche Kurse (11:00) + https://www.snb.ch + 100 JPY = 0.8827 CHF (Tägliche Kurse (11:00); 2017-11-22T12:24:04.103+01:00) + 2017-11-22T12:24:04.103+01:00 + de + + + CH + SNB + + + + + 0.8827 + CHF + -2 + 4 + + CHF + JPY + Tägliche Kurse (11:00) + + + daily + 2017-11-22 + + + + + + CH: 1.1633 CHF = 1 EUR 2017-11-22 Tägliche Kurse (11:00) + https://www.snb.ch + 1 EUR = 1.1633 CHF (Tägliche Kurse (11:00); 2017-11-22T12:24:04.090+01:00) + 2017-11-22T12:24:04.090+01:00 + de + + + CH + SNB + + + + + 1.1633 + CHF + 4 + + CHF + EUR + Tägliche Kurse (11:00) + + + daily + 2017-11-22 + + + + + + CH: 0.9894 CHF = 1 USD 2017-11-22 Tägliche Kurse (11:00) + https://www.snb.ch + 1 USD = 0.9894 CHF (Tägliche Kurse (11:00); 2017-11-22T12:24:04.083+01:00) + 2017-11-22T12:24:04.083+01:00 + de + + + CH + SNB + + + + + 0.9894 + CHF + 4 + + CHF + USD + Tägliche Kurse (11:00) + + + daily + 2017-11-22 + + + + + + CH: 1.3151 CHF = 1 GBP 2017-11-21 Tägliche Kurse (11:00) + https://www.snb.ch + 1 GBP = 1.3151 CHF (Tägliche Kurse (11:00); 2017-11-21T12:19:20.370+01:00) + 2017-11-21T12:19:20.370+01:00 + de + + + CH + SNB + + + + + 1.3151 + CHF + 4 + + CHF + GBP + Tägliche Kurse (11:00) + + + daily + 2017-11-21 + + + + + + CH: 0.8832 CHF = 100 JPY 2017-11-21 Tägliche Kurse (11:00) + https://www.snb.ch + 100 JPY = 0.8832 CHF (Tägliche Kurse (11:00); 2017-11-21T12:19:20.363+01:00) + 2017-11-21T12:19:20.363+01:00 + de + + + CH + SNB + + + + + 0.8832 + CHF + -2 + 4 + + CHF + JPY + Tägliche Kurse (11:00) + + + daily + 2017-11-21 + + + + + + CH: 1.1647 CHF = 1 EUR 2017-11-21 Tägliche Kurse (11:00) + https://www.snb.ch + 1 EUR = 1.1647 CHF (Tägliche Kurse (11:00); 2017-11-21T12:19:20.360+01:00) + 2017-11-21T12:19:20.360+01:00 + de + + + CH + SNB + + + + + 1.1647 + CHF + 4 + + CHF + EUR + Tägliche Kurse (11:00) + + + daily + 2017-11-21 + + + + + + CH: 0.9930 CHF = 1 USD 2017-11-21 Tägliche Kurse (11:00) + https://www.snb.ch + 1 USD = 0.9930 CHF (Tägliche Kurse (11:00); 2017-11-21T12:19:20.343+01:00) + 2017-11-21T12:19:20.343+01:00 + de + + + CH + SNB + + + + + 0.9930 + CHF + 4 + + CHF + USD + Tägliche Kurse (11:00) + + + daily + 2017-11-21 + + + + + From 9061e66151c8166002c83f931dd74f2b33dc5754 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Tue, 26 Dec 2017 13:02:19 +0100 Subject: [PATCH 05/25] Duno if thats your and my config and has to be ignored but for now il commit it. --- django/.idea/django.iml | 2 +- django/.idea/misc.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/django/.idea/django.iml b/django/.idea/django.iml index 6711606..0acd853 100644 --- a/django/.idea/django.iml +++ b/django/.idea/django.iml @@ -2,7 +2,7 @@ - + diff --git a/django/.idea/misc.xml b/django/.idea/misc.xml index 2c50d4f..539c015 100644 --- a/django/.idea/misc.xml +++ b/django/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file From 318d599a2f68225b61361c9c760243d18c693887 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Wed, 27 Dec 2017 18:17:14 +0100 Subject: [PATCH 06/25] Add date to currencies list --- .../didgeridoo/currencies/exchange_rates.py | 2 +- .../migrations/0002_exchangerate_date.py | 20 +++++++++++++++++++ .../migrations/0003_auto_20171227_1119.py | 20 +++++++++++++++++++ django/didgeridoo/currencies/models.py | 1 + .../templates/currencies/index.html | 10 ++++++++-- django/didgeridoo/currencies/views.py | 8 +++++--- django/didgeridoo/didgeridoo/settings.py | 2 +- 7 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 django/didgeridoo/currencies/migrations/0002_exchangerate_date.py create mode 100644 django/didgeridoo/currencies/migrations/0003_auto_20171227_1119.py diff --git a/django/didgeridoo/currencies/exchange_rates.py b/django/didgeridoo/currencies/exchange_rates.py index 2900892..db1cc6b 100644 --- a/django/didgeridoo/currencies/exchange_rates.py +++ b/django/didgeridoo/currencies/exchange_rates.py @@ -110,7 +110,7 @@ def get_exchange_rate(): # CHFvalue, "CHF and 1 ", base_currency, " costs: ", # FOREIGNvalue_round, target_currency) exchange_rates.update( - {target_currency: FOREIGNvalue_round}) + {target_currency: FOREIGNvalue_round, "date": date}) # Print the Dictionary: # print(exchange_rates) else: diff --git a/django/didgeridoo/currencies/migrations/0002_exchangerate_date.py b/django/didgeridoo/currencies/migrations/0002_exchangerate_date.py new file mode 100644 index 0000000..37fb831 --- /dev/null +++ b/django/didgeridoo/currencies/migrations/0002_exchangerate_date.py @@ -0,0 +1,20 @@ +# -*- 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 new file mode 100644 index 0000000..c6283a2 --- /dev/null +++ b/django/didgeridoo/currencies/migrations/0003_auto_20171227_1119.py @@ -0,0 +1,20 @@ +# -*- 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/models.py b/django/didgeridoo/currencies/models.py index 1edbe06..fb6d10c 100644 --- a/django/didgeridoo/currencies/models.py +++ b/django/didgeridoo/currencies/models.py @@ -3,6 +3,7 @@ from django.db import models class ExchangeRate(models.Model): name = models.CharField(max_length=200, unique=True) + date = models.DateField('%Y-%m-%d', null=True) 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 3c1e8a7..50d944a 100644 --- a/django/didgeridoo/currencies/templates/currencies/index.html +++ b/django/didgeridoo/currencies/templates/currencies/index.html @@ -8,11 +8,17 @@ {% if currency_list %}
      {% for currency in currency_list %} -
    • {{ currency.name }} : {{ currency.exchange_rate_to_chf }}
    • +
    • + {{ currency.date }} : + {{ currency.name }} : + {{ currency.exchange_rate_to_chf }} +
    • {% endfor %}
    {% else %} -

    No categories are available.

    +

    + Something whent wrong, no currencies are available. +

    {% endif %}
diff --git a/django/didgeridoo/currencies/views.py b/django/didgeridoo/currencies/views.py index 23f8952..4a4450c 100644 --- a/django/didgeridoo/currencies/views.py +++ b/django/didgeridoo/currencies/views.py @@ -6,17 +6,19 @@ from currencies import exchange_rates def currencies(request): # return HttpResponse("exchange_rates") raw_data = exchange_rates.get_exchange_rate() - for i, j in raw_data.items(): + for i, j, k, l in raw_data.items(): if ExchangeRate.objects.filter(name=i): e = ExchangeRate.objects.filter( name=i, ).update( - exchange_rate_to_chf=j + exchange_rate_to_chf=j, + date=l ) else: e = ExchangeRate.objects.create( name=i, - exchange_rate_to_chf=j + exchange_rate_to_chf=j, + date=l ) e.save() currency_list = ExchangeRate.objects.all() diff --git a/django/didgeridoo/didgeridoo/settings.py b/django/didgeridoo/didgeridoo/settings.py index f741185..33c98f9 100644 --- a/django/didgeridoo/didgeridoo/settings.py +++ b/django/didgeridoo/didgeridoo/settings.py @@ -32,7 +32,6 @@ ALLOWED_HOSTS = [] INSTALLED_APPS = [ 'webshop.apps.WebshopConfig', - 'currencies.apps.CurrenciesConfig', 'django_extensions', 'django.contrib.admin', 'django.contrib.auth', @@ -40,6 +39,7 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'currencies', ] MIDDLEWARE = [ From 6ddd869722c51c1c296460a2516066a503561101 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Wed, 27 Dec 2017 19:33:29 +0100 Subject: [PATCH 07/25] add fixtures with test data --- Vagrantfile | 2 +- ansible/roles/web_AI-5/tasks/setup_script.sh | 2 + .../didgeridoo/webshop/fixtures/webshop.yaml | 97 +++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 django/didgeridoo/webshop/fixtures/webshop.yaml diff --git a/Vagrantfile b/Vagrantfile index 7e8c3d7..a8fe782 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -30,7 +30,7 @@ Vagrant.configure("2") do |config| #zu installierende Pakete apt-get install -y apache2 python3-django mariadb-server avahi-daemon \ libnss-mdns libapache2-mod-wsgi-py3 python3-mysqldb python3-pip - pip3 install django-extensions Pillow + pip3 install django-extensions Pillow pyaml /vagrant/ansible/roles/web_AI-5/tasks/setup_script.sh SHELL diff --git a/ansible/roles/web_AI-5/tasks/setup_script.sh b/ansible/roles/web_AI-5/tasks/setup_script.sh index d9ed1c3..6243fb4 100755 --- a/ansible/roles/web_AI-5/tasks/setup_script.sh +++ b/ansible/roles/web_AI-5/tasks/setup_script.sh @@ -17,3 +17,5 @@ echo "from django.contrib.auth.models import User; \ User.objects.filter(email='admin@example.com').delete(); \ User.objects.create_superuser('admin', 'admin@example.com', 'password')" | python3 /vagrant/django/didgeridoo/manage.py shell + +python3 /vagrant/django/didgeridoo/manage.py loaddata webshop diff --git a/django/didgeridoo/webshop/fixtures/webshop.yaml b/django/didgeridoo/webshop/fixtures/webshop.yaml new file mode 100644 index 0000000..a3c429d --- /dev/null +++ b/django/didgeridoo/webshop/fixtures/webshop.yaml @@ -0,0 +1,97 @@ +- model: webshop.Category + fields: + name: "First Parent Category" + parent_category: +- model: webshop.Category + fields: + name: "Second Parent Category" + parent_category: +- model: webshop.Category + fields: + name: "Third Parent Category" + parent_category: +- model: webshop.Category + fields: + name: "Fourth Parent Category" + parent_category: +- model: webshop.Category + fields: + name: "Child Category 1" + parent_category: 1 +- model: webshop.Category + fields: + name: "Child Category 2" + parent_category: 2 +- model: webshop.Category + fields: + name: "Child Category 3" + parent_category: 3 +- model: webshop.Category + fields: + name: "Child Category 4" + parent_category: 4 + +- model: webshop.Article + fields: + name: "Article of First Parent Category" + category: 1 + description: "An article sorted under the First Parent Category." + stock: 10 + status: 3 + price_in_chf: 10.1 +- model: webshop.Article + fields: + name: "Article of Second Parent Category" + category: 2 + description: "An article sorted under the Second Parent Category." + stock: 20 + status: 3 + price_in_chf: 20.2 +- model: webshop.Article + fields: + name: "Article of Third Parent Category" + category: 3 + description: "An article sorted under the Third Parent Category." + stock: 30 + status: 3 + price_in_chf: 30.3 +- model: webshop.Article + fields: + name: "Article of Fourth Parent Category" + category: 4 + description: "An article sorted under the Fourth Parent Category." + stock: 40 + status: 3 + price_in_chf: 40.4 +- model: webshop.Article + fields: + name: "Article of Child Category 1" + category: 5 + description: "An article sorted under the Child Category 1." + stock: 11 + status: 3 + price_in_chf: 11.1 +- model: webshop.Article + fields: + name: "Article of Child Category 2" + category: 6 + description: "An article sorted under the Child Category 2." + stock: 22 + status: 3 + price_in_chf: 21.2 +- model: webshop.Article + fields: + name: "Article of Child Category 3" + category: 7 + description: "An article sorted under the Child Category 3." + stock: 33 + status: 3 + price_in_chf: 31.3 +- model: webshop.Article + fields: + name: "Article of Child Category 4" + category: 8 + description: "An article sorted under the Child Category 4." + stock: 44 + status: 3 + price_in_chf: 41.4 From fc34c3fa4cadf277cef274029944563a238e7438 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Wed, 27 Dec 2017 19:35:36 +0100 Subject: [PATCH 08/25] add a python package to the ansible role --- ansible/roles/web_AI-5/tasks/main.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/ansible/roles/web_AI-5/tasks/main.yml b/ansible/roles/web_AI-5/tasks/main.yml index e956179..ea68b21 100644 --- a/ansible/roles/web_AI-5/tasks/main.yml +++ b/ansible/roles/web_AI-5/tasks/main.yml @@ -27,6 +27,7 @@ name: - django-extensions - Pillow + - pyaml executable: pip3 - name: Run the setup script to add some final touches From 9589f0f9806e3256a5910615217ec0199a9049f6 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Thu, 28 Dec 2017 16:20:22 +0100 Subject: [PATCH 09/25] change the content include because the org-mode file is called doku --- docs/main.tex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/main.tex b/docs/main.tex index 3c774f0..fa3976b 100644 --- a/docs/main.tex +++ b/docs/main.tex @@ -11,7 +11,7 @@ \microtypesetup{protrusion=true} % enables protrusion \newpage -\include{content} +\include{doku} \newpage \nocite{*} From 48eee1c00b90368371b6637bf3f7666970bcddfa Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Thu, 28 Dec 2017 16:23:02 +0100 Subject: [PATCH 10/25] change the article status to active --- sql/02_insert_data.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/02_insert_data.sql b/sql/02_insert_data.sql index 6689176..8c31e0b 100644 --- a/sql/02_insert_data.sql +++ b/sql/02_insert_data.sql @@ -24,7 +24,7 @@ use webshopdb; insert into webshop_articlestatus (name) values ('out of stock'), ('hidden'), - ('on sale'); + ('active'); use webshopdb; insert into webshop_city (zip_code, name) From e5186696982bbf68b5a6765776f67207ec381f21 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Thu, 28 Dec 2017 16:23:22 +0100 Subject: [PATCH 11/25] remove an old file --- html/index.html | 20 -------------------- 1 file changed, 20 deletions(-) delete mode 100644 html/index.html diff --git a/html/index.html b/html/index.html deleted file mode 100644 index 6bc0653..0000000 --- a/html/index.html +++ /dev/null @@ -1,20 +0,0 @@ - - - - - -
-

audioguide

-

a mobile offline audioguide

- noop - noop - noop - noop - noop - noop -

the audioguide that makes a offline usage possible.

-
- - - From f48c316f39881602d16d2104f224413f6495cad1 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Thu, 28 Dec 2017 16:26:54 +0100 Subject: [PATCH 12/25] remove an unnecessary configuration --- django/didgeridoo/didgeridoo/settings.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/django/didgeridoo/didgeridoo/settings.py b/django/didgeridoo/didgeridoo/settings.py index 155ca3d..2503e23 100644 --- a/django/didgeridoo/didgeridoo/settings.py +++ b/django/didgeridoo/didgeridoo/settings.py @@ -25,7 +25,11 @@ SECRET_KEY = '(#4#-$$&mx7(%q+6&&@-c&g%i0dc4)zfks1%sy8b%lsxspou&%' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = [ + 'localhost', + '127.0.0.1', + 'didgeridoo.ml' +] # Application definition @@ -129,10 +133,3 @@ USE_TZ = True STATIC_URL = '/static/' STATIC_ROOT = '/vagrant/django/didgeridoo/static/' MEDIA_ROOT = '/vagrant/django/didgeridoo/media/' - - -ALLOWED_HOSTS = [ - 'localhost', - '127.0.0.1', - 'didgeridoo.ml' -] From f9f1c89a015d9d3cd092f1bd087412c15db3912e Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Thu, 28 Dec 2017 16:40:04 +0100 Subject: [PATCH 13/25] make imports more explicit --- django/didgeridoo/webshop/admin.py | 5 +++-- django/didgeridoo/webshop/urls.py | 2 +- django/didgeridoo/webshop/views.py | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/django/didgeridoo/webshop/admin.py b/django/didgeridoo/webshop/admin.py index 1abf027..06b4dd4 100644 --- a/django/didgeridoo/webshop/admin.py +++ b/django/didgeridoo/webshop/admin.py @@ -3,8 +3,9 @@ from django.contrib.auth.admin import UserAdmin as BaseUserAdmin from django.contrib.auth.models import User # Register your models here. -from .models import (Article, Order, OrderPosition, Person, City, Picture, - OrderOfGoods, Category, Option, Setting) +from webshop.models import (Article, Order, OrderPosition, + Person, City, Picture, OrderOfGoods, + Category, Option, Setting) class PersonInline(admin.StackedInline): diff --git a/django/didgeridoo/webshop/urls.py b/django/didgeridoo/webshop/urls.py index ae62468..e48e2ea 100644 --- a/django/didgeridoo/webshop/urls.py +++ b/django/didgeridoo/webshop/urls.py @@ -1,6 +1,6 @@ from django.conf.urls import url -from . import views +from webshop import views urlpatterns = [ url(r'^$', views.index, name='index'), diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 046ae5f..d015d3f 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -1,5 +1,5 @@ from django.shortcuts import get_object_or_404, render -from .models import Article, Category, ArticleStatus +from webshop.models import Article, Category, ArticleStatus # Create your views here. From acafb61399e545e4b61c77263c6511e852738995 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Thu, 28 Dec 2017 19:14:00 +0100 Subject: [PATCH 14/25] redirect logins back to the index --- django/didgeridoo/didgeridoo/settings.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/django/didgeridoo/didgeridoo/settings.py b/django/didgeridoo/didgeridoo/settings.py index 2503e23..3c31a02 100644 --- a/django/didgeridoo/didgeridoo/settings.py +++ b/django/didgeridoo/didgeridoo/settings.py @@ -133,3 +133,5 @@ USE_TZ = True STATIC_URL = '/static/' STATIC_ROOT = '/vagrant/django/didgeridoo/static/' MEDIA_ROOT = '/vagrant/django/didgeridoo/media/' + +LOGIN_REDIRECT_URL = '/' From fce1a6a8c25b1debb2e51ed3a6c66dc000183abc Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Thu, 28 Dec 2017 19:14:41 +0100 Subject: [PATCH 15/25] remove unnecessary attributes from the Person model --- django/didgeridoo/webshop/models.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/django/didgeridoo/webshop/models.py b/django/didgeridoo/webshop/models.py index b2bfa91..06e9f10 100644 --- a/django/didgeridoo/webshop/models.py +++ b/django/didgeridoo/webshop/models.py @@ -138,8 +138,6 @@ class Salutation(models.Model): class Person(models.Model): - first_name = models.CharField(max_length=200) - last_name = models.CharField(max_length=200) salutation = models.ForeignKey(Salutation) city = models.ForeignKey(City) street_name = models.CharField(max_length=200) @@ -147,4 +145,4 @@ class Person(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) def __str__(self): - return self.last_name + return self.user.username From 516dd47493b389372539345c99b8051bb6704b8f Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Thu, 28 Dec 2017 19:15:35 +0100 Subject: [PATCH 16/25] remove an unnecessary shebang --- django/didgeridoo/webshop/models.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/django/didgeridoo/webshop/models.py b/django/didgeridoo/webshop/models.py index 06e9f10..e168aa3 100644 --- a/django/didgeridoo/webshop/models.py +++ b/django/didgeridoo/webshop/models.py @@ -1,5 +1,3 @@ -#!/usr/bin/python3 - from decimal import Decimal from django.core.validators import MinValueValidator from django.db import models From 56e56f0c1cf2d7e0fcf091edfff1d6ee747e31a0 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Thu, 28 Dec 2017 19:19:47 +0100 Subject: [PATCH 17/25] add url patterns to support user authentication --- django/didgeridoo/webshop/urls.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/django/didgeridoo/webshop/urls.py b/django/didgeridoo/webshop/urls.py index e48e2ea..79c1d79 100644 --- a/django/didgeridoo/webshop/urls.py +++ b/django/didgeridoo/webshop/urls.py @@ -1,4 +1,4 @@ -from django.conf.urls import url +from django.conf.urls import url, include from webshop import views @@ -10,4 +10,5 @@ urlpatterns = [ url(r'^category/(?P[0-9]+)/$', views.articles_in_category, name='category'), + url('^', include('django.contrib.auth.urls')), ] From de3e07dedb61f7008fcd99b05e92c03acd9c97b6 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Thu, 28 Dec 2017 19:20:28 +0100 Subject: [PATCH 18/25] add templates to support user authentication --- .../webshop/templates/registration/logged_out.html | 6 ++++++ .../webshop/templates/registration/login.html | 11 +++++++++++ 2 files changed, 17 insertions(+) create mode 100644 django/didgeridoo/webshop/templates/registration/logged_out.html create mode 100644 django/didgeridoo/webshop/templates/registration/login.html diff --git a/django/didgeridoo/webshop/templates/registration/logged_out.html b/django/didgeridoo/webshop/templates/registration/logged_out.html new file mode 100644 index 0000000..866f568 --- /dev/null +++ b/django/didgeridoo/webshop/templates/registration/logged_out.html @@ -0,0 +1,6 @@ +{% extends 'webshop/base.html' %} + +{% block section_title %}You have been successfully logged out.{% endblock %} + +{% block content %} +{% endblock %} diff --git a/django/didgeridoo/webshop/templates/registration/login.html b/django/didgeridoo/webshop/templates/registration/login.html new file mode 100644 index 0000000..1d51a55 --- /dev/null +++ b/django/didgeridoo/webshop/templates/registration/login.html @@ -0,0 +1,11 @@ +{% extends 'webshop/base.html' %} + +{% block section_title %}Login{% endblock %} + +{% block content %} +
+ {% csrf_token %} + {{ form.as_p }} + +
+{% endblock %} From 59db6e221e6702d89e7c8901018a620adb4f50a5 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Thu, 28 Dec 2017 19:21:29 +0100 Subject: [PATCH 19/25] extend the base template with login, profile and logout links --- django/didgeridoo/webshop/templates/webshop/base.html | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/django/didgeridoo/webshop/templates/webshop/base.html b/django/didgeridoo/webshop/templates/webshop/base.html index fa6b702..cf6c6e2 100644 --- a/django/didgeridoo/webshop/templates/webshop/base.html +++ b/django/didgeridoo/webshop/templates/webshop/base.html @@ -4,6 +4,12 @@
+ Home | + {% if user.is_authenticated %} + Profile | Logout + {% else %} + Login + {% endif %}

{% block section_title %}Music Instrument Shop{% endblock %}

{% block content %}{% endblock %}
From d8a1d9ef54c1ab45235f25bbad484bd6c64d83b7 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Thu, 28 Dec 2017 19:22:00 +0100 Subject: [PATCH 20/25] add a user profile --- .../webshop/templates/registration/profile.html | 17 +++++++++++++++++ django/didgeridoo/webshop/urls.py | 3 +++ django/didgeridoo/webshop/views.py | 10 +++++++++- 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 django/didgeridoo/webshop/templates/registration/profile.html diff --git a/django/didgeridoo/webshop/templates/registration/profile.html b/django/didgeridoo/webshop/templates/registration/profile.html new file mode 100644 index 0000000..ed2e406 --- /dev/null +++ b/django/didgeridoo/webshop/templates/registration/profile.html @@ -0,0 +1,17 @@ +{% extends 'webshop/base.html' %} + +{% block section_title %}User Profile{% endblock %} + +{% block content %} +

Username: {{ request.user.username }}

+

Salutation: {{ person.salutation }}

+

Firstname: {{ request.user.first_name }}

+

Lastname: {{ request.user.last_name }}

+

City: {{ person.city }}

+

Street: {{ person.street_name }}

+

Streetnumber: {{ person.street_number }}

+
+ {% csrf_token %} + {{ form.as_p }} +
+{% endblock %} diff --git a/django/didgeridoo/webshop/urls.py b/django/didgeridoo/webshop/urls.py index 79c1d79..bcaddec 100644 --- a/django/didgeridoo/webshop/urls.py +++ b/django/didgeridoo/webshop/urls.py @@ -11,4 +11,7 @@ urlpatterns = [ views.articles_in_category, name='category'), url('^', include('django.contrib.auth.urls')), + url(r'^accounts/profile/$', + views.profile, + name='profile'), ] diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index d015d3f..0b17186 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -1,5 +1,6 @@ from django.shortcuts import get_object_or_404, render -from webshop.models import Article, Category, ArticleStatus +from django.contrib.auth.decorators import login_required +from webshop.models import Article, Category, ArticleStatus, Person # Create your views here. @@ -33,3 +34,10 @@ def article_details(request, article_id): article = get_object_or_404(Article, pk=article_id) return render(request, 'webshop/article_details.html', {'article': article}) + + +@login_required +def profile(request): + person = Person.objects.get(user=request.user) + return render(request, 'registration/profile.html', + {'person': person}) From c229194e502d1174dc3c0e0ac0ea00cbe96d1e26 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Fri, 29 Dec 2017 11:04:48 +0100 Subject: [PATCH 21/25] disable debugging for production --- django/didgeridoo/didgeridoo/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django/didgeridoo/didgeridoo/settings.py b/django/didgeridoo/didgeridoo/settings.py index 155ca3d..f4a27f1 100644 --- a/django/didgeridoo/didgeridoo/settings.py +++ b/django/didgeridoo/didgeridoo/settings.py @@ -23,7 +23,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) SECRET_KEY = '(#4#-$$&mx7(%q+6&&@-c&g%i0dc4)zfks1%sy8b%lsxspou&%' # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = False ALLOWED_HOSTS = [] From 1f3ab70dd6f9160bb81e68125470f624a6eaa8c1 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Fri, 29 Dec 2017 16:46:21 +0100 Subject: [PATCH 22/25] add a registration --- django/didgeridoo/webshop/forms.py | 24 +++++++++++++ .../webshop/templates/registration/login.html | 1 + .../templates/registration/register.html | 19 ++++++++++ django/didgeridoo/webshop/urls.py | 5 ++- django/didgeridoo/webshop/views.py | 35 ++++++++++++++++++- 5 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 django/didgeridoo/webshop/forms.py create mode 100644 django/didgeridoo/webshop/templates/registration/register.html diff --git a/django/didgeridoo/webshop/forms.py b/django/didgeridoo/webshop/forms.py new file mode 100644 index 0000000..c87ec6c --- /dev/null +++ b/django/didgeridoo/webshop/forms.py @@ -0,0 +1,24 @@ +from django import forms +from webshop.models import Salutation, City + + +class RegistrationForm(forms.Form): + email = forms.EmailField() + salutation = forms.ModelChoiceField(queryset=Salutation.objects.all()) + first_name = forms.CharField() + last_name = forms.CharField() + street_name = forms.CharField() + street_number = forms.CharField() + zip_code = forms.IntegerField(min_value=1000, max_value=9999) + city = forms.CharField() + + def clean_city(self): + # Check that the two password entries match + city = self.cleaned_data['city'] + zip_code = self.cleaned_data['zip_code'] + try: + City.objects.get(name=city, zip_code=zip_code) + except City.DoesNotExist: + raise forms.ValidationError( + "The zip code and the city don't match.") + return city diff --git a/django/didgeridoo/webshop/templates/registration/login.html b/django/didgeridoo/webshop/templates/registration/login.html index 1d51a55..4ff603e 100644 --- a/django/didgeridoo/webshop/templates/registration/login.html +++ b/django/didgeridoo/webshop/templates/registration/login.html @@ -7,5 +7,6 @@ {% csrf_token %} {{ form.as_p }} +

Go to registration.

{% endblock %} diff --git a/django/didgeridoo/webshop/templates/registration/register.html b/django/didgeridoo/webshop/templates/registration/register.html new file mode 100644 index 0000000..032758f --- /dev/null +++ b/django/didgeridoo/webshop/templates/registration/register.html @@ -0,0 +1,19 @@ +{% extends 'webshop/base.html' %} + +{% block section_title %}Registration{% endblock %} + +{% block content %} + {% if profile_form.errors %} +

+ Please correct the error{{ form.errors|pluralize }} below. +

+ {% endif %} +
+ + {{ user_form.as_table }} + {{ profile_form.as_table }} +
+ {% csrf_token %} + +
+{% endblock %} diff --git a/django/didgeridoo/webshop/urls.py b/django/didgeridoo/webshop/urls.py index bcaddec..dc79c1d 100644 --- a/django/didgeridoo/webshop/urls.py +++ b/django/didgeridoo/webshop/urls.py @@ -11,7 +11,6 @@ urlpatterns = [ views.articles_in_category, name='category'), url('^', include('django.contrib.auth.urls')), - url(r'^accounts/profile/$', - views.profile, - name='profile'), + url(r'^profile/$', views.profile, name='profile'), + url(r'^registration/$', views.registration, name='registration'), ] diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 0b17186..499c261 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -1,6 +1,10 @@ +from django.http import HttpResponseRedirect from django.shortcuts import get_object_or_404, render from django.contrib.auth.decorators import login_required -from webshop.models import Article, Category, ArticleStatus, Person +from django.contrib.auth.models import User +from django.contrib.auth.forms import UserCreationForm +from webshop.models import Article, Category, ArticleStatus, Person, City +from webshop.forms import RegistrationForm # Create your views here. @@ -41,3 +45,32 @@ def profile(request): person = Person.objects.get(user=request.user) return render(request, 'registration/profile.html', {'person': person}) + + +def registration(request): + if request.method == 'POST': + profile_form = RegistrationForm(request.POST) + user_form = UserCreationForm(request.POST) + if (profile_form.is_valid() and user_form.is_valid()): + pf = profile_form.cleaned_data + uf = user_form.cleaned_data + user = User.objects.create_user(uf['username'], + pf['email'], + uf['password2']) + user.last_name = pf['last_name'] + user.first_name = pf['first_name'] + user.save() + person = Person.objects.create( + salutation=pf['salutation'], + city=City.objects.get(zip_code=pf['zip_code'], + name=pf['city']), + street_name=pf['street_name'], + street_number=pf['street_number'], + user=user) + return HttpResponseRedirect('/login/') + else: + profile_form = RegistrationForm + user_form = UserCreationForm + return render(request, 'registration/register.html', + {'profile_form': profile_form, + 'user_form': user_form}) From d07067dcacb9e3d269b7c4d5a94dab1c9355deac Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Fri, 29 Dec 2017 16:52:19 +0100 Subject: [PATCH 23/25] checkout the production branch on the production server --- ansible/roles/web_AI-5/tasks/main.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/ansible/roles/web_AI-5/tasks/main.yml b/ansible/roles/web_AI-5/tasks/main.yml index ea68b21..4cab145 100644 --- a/ansible/roles/web_AI-5/tasks/main.yml +++ b/ansible/roles/web_AI-5/tasks/main.yml @@ -15,6 +15,7 @@ git: repo=https://git.2li.ch/ibz/web_AI-5.git dest="/vagrant" force=yes + version=production - name: Set Permissions on the repository file: From 13805f790dd3a37f762792db74db45ad6f95ab70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Fri, 29 Dec 2017 17:02:33 +0100 Subject: [PATCH 24/25] add tableview in /currencies --- .../didgeridoo/currencies/exchange_rates.py | 8 ++++--- .../templates/currencies/index.html | 16 ++++++++++++++ django/didgeridoo/currencies/views.py | 21 ++++++++++--------- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/django/didgeridoo/currencies/exchange_rates.py b/django/didgeridoo/currencies/exchange_rates.py index db1cc6b..5415301 100644 --- a/django/didgeridoo/currencies/exchange_rates.py +++ b/django/didgeridoo/currencies/exchange_rates.py @@ -53,8 +53,10 @@ def get_exchange_rate(): 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. "%Y-%m-%dT%H:%M:%S.%f%z" date = datetime.strptime(''.join(datetime_str.rsplit(':', 1)), - "%Y-%m-%dT%H:%M:%S.%f%z").strftime("%Y-%m-%d") + "%Y-%m-%dT%H:%M:%S%z").strftime("%Y-%m-%d") # only the values of today are used so check for date: if date == today: title = item.find('none:title', ns).text @@ -110,11 +112,11 @@ def get_exchange_rate(): # CHFvalue, "CHF and 1 ", base_currency, " costs: ", # FOREIGNvalue_round, target_currency) exchange_rates.update( - {target_currency: FOREIGNvalue_round, "date": date}) + {target_currency: FOREIGNvalue_round}) # Print the Dictionary: # print(exchange_rates) else: break - return(exchange_rates) + return(exchange_rates, date) # 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 50d944a..8e67685 100644 --- a/django/didgeridoo/currencies/templates/currencies/index.html +++ b/django/didgeridoo/currencies/templates/currencies/index.html @@ -6,6 +6,22 @@

Currencies in CHF

{% if currency_list %} + + + + {% for currency in currency_list %} + + {% endfor %} + + + + {% for currency in currency_list %} + + {% endfor %} + +
DATE{{ currency.name }}
{{ date }}{{ currency.exchange_rate_to_chf }}
+
+
    {% for currency in currency_list %}
  • diff --git a/django/didgeridoo/currencies/views.py b/django/didgeridoo/currencies/views.py index 4a4450c..45c280b 100644 --- a/django/didgeridoo/currencies/views.py +++ b/django/didgeridoo/currencies/views.py @@ -5,23 +5,24 @@ from currencies import exchange_rates def currencies(request): # return HttpResponse("exchange_rates") - raw_data = exchange_rates.get_exchange_rate() - for i, j, k, l in raw_data.items(): - if ExchangeRate.objects.filter(name=i): + raw_data, date = exchange_rates.get_exchange_rate() + for currency, rate in raw_data.items(): + if ExchangeRate.objects.filter(name=currency): e = ExchangeRate.objects.filter( - name=i, + name=currency, ).update( - exchange_rate_to_chf=j, - date=l + exchange_rate_to_chf=rate, + date=date ) else: e = ExchangeRate.objects.create( - name=i, - exchange_rate_to_chf=j, - date=l + name=currency, + exchange_rate_to_chf=rate, + date=date ) e.save() currency_list = ExchangeRate.objects.all() return render(request, 'currencies/index.html', - {'currency_list': currency_list}) + {'currency_list': currency_list, + 'date': date}) From 19afc446ed3ae448882d50a34a166e14e131432b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Fri, 29 Dec 2017 19:26:01 +0100 Subject: [PATCH 25/25] changed view and deleted the database unique filed --- .../didgeridoo/currencies/exchange_rates.py | 8 +++++--- .../migrations/0004_auto_20171229_1708.py | 20 +++++++++++++++++++ .../migrations/0005_auto_20171229_1735.py | 20 +++++++++++++++++++ .../migrations/0006_auto_20171229_1747.py | 20 +++++++++++++++++++ .../migrations/0007_auto_20171229_1806.py | 20 +++++++++++++++++++ django/didgeridoo/currencies/models.py | 2 +- .../templates/currencies/index.html | 13 ++++++------ django/didgeridoo/currencies/views.py | 14 ++++++------- 8 files changed, 99 insertions(+), 18 deletions(-) create mode 100644 django/didgeridoo/currencies/migrations/0004_auto_20171229_1708.py create mode 100644 django/didgeridoo/currencies/migrations/0005_auto_20171229_1735.py create mode 100644 django/didgeridoo/currencies/migrations/0006_auto_20171229_1747.py create mode 100644 django/didgeridoo/currencies/migrations/0007_auto_20171229_1806.py diff --git a/django/didgeridoo/currencies/exchange_rates.py b/django/didgeridoo/currencies/exchange_rates.py index 5415301..bb12c47 100644 --- a/django/didgeridoo/currencies/exchange_rates.py +++ b/django/didgeridoo/currencies/exchange_rates.py @@ -55,9 +55,11 @@ 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. "%Y-%m-%dT%H:%M:%S.%f%z" - date = datetime.strptime(''.join(datetime_str.rsplit(':', 1)), - "%Y-%m-%dT%H:%M:%S%z").strftime("%Y-%m-%d") - # only the values of today are used so check for date: + date = datetime.strptime(''.join( + datetime_str.rsplit(':', 1)), + "%Y-%m-%dT%H:%M:%S%z").strftime( + "%Y-%m-%d") + # only the values of today are used so check for date in XML: if date == today: title = item.find('none:title', ns).text base_currency = item.find(rate_path + diff --git a/django/didgeridoo/currencies/migrations/0004_auto_20171229_1708.py b/django/didgeridoo/currencies/migrations/0004_auto_20171229_1708.py new file mode 100644 index 0000000..ad84626 --- /dev/null +++ b/django/didgeridoo/currencies/migrations/0004_auto_20171229_1708.py @@ -0,0 +1,20 @@ +# -*- 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 new file mode 100644 index 0000000..3ac6227 --- /dev/null +++ b/django/didgeridoo/currencies/migrations/0005_auto_20171229_1735.py @@ -0,0 +1,20 @@ +# -*- 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 new file mode 100644 index 0000000..a2a330b --- /dev/null +++ b/django/didgeridoo/currencies/migrations/0006_auto_20171229_1747.py @@ -0,0 +1,20 @@ +# -*- 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 new file mode 100644 index 0000000..2455467 --- /dev/null +++ b/django/didgeridoo/currencies/migrations/0007_auto_20171229_1806.py @@ -0,0 +1,20 @@ +# -*- 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 fb6d10c..ca6ed15 100644 --- a/django/didgeridoo/currencies/models.py +++ b/django/didgeridoo/currencies/models.py @@ -2,7 +2,7 @@ from django.db import models class ExchangeRate(models.Model): - name = models.CharField(max_length=200, unique=True) + name = models.CharField(max_length=200) date = models.DateField('%Y-%m-%d', null=True) exchange_rate_to_chf = models.FloatField(max_length=5) diff --git a/django/didgeridoo/currencies/templates/currencies/index.html b/django/didgeridoo/currencies/templates/currencies/index.html index 8e67685..bddcee2 100644 --- a/django/didgeridoo/currencies/templates/currencies/index.html +++ b/django/didgeridoo/currencies/templates/currencies/index.html @@ -13,14 +13,15 @@ {{ currency.name }} {% endfor %} - - {{ date }} - {% for currency in currency_list %} - {{ currency.exchange_rate_to_chf }} - {% endfor %} - + + {{ date }} + {% for currency in currency_list %} + {{ currency.exchange_rate_to_chf }} + {% endfor %} +
    +

    {{ message }}


      {% for currency in currency_list %} diff --git a/django/didgeridoo/currencies/views.py b/django/didgeridoo/currencies/views.py index 45c280b..1128eab 100644 --- a/django/didgeridoo/currencies/views.py +++ b/django/didgeridoo/currencies/views.py @@ -6,14 +6,10 @@ from currencies import exchange_rates def currencies(request): # return HttpResponse("exchange_rates") raw_data, date = exchange_rates.get_exchange_rate() + message = "" for currency, rate in raw_data.items(): - if ExchangeRate.objects.filter(name=currency): - e = ExchangeRate.objects.filter( - name=currency, - ).update( - exchange_rate_to_chf=rate, - date=date - ) + if ExchangeRate.objects.filter(date=date): + message = "already querried today" else: e = ExchangeRate.objects.create( name=currency, @@ -21,8 +17,10 @@ def currencies(request): date=date ) e.save() + message = "updated successfully" currency_list = ExchangeRate.objects.all() return render(request, 'currencies/index.html', {'currency_list': currency_list, - 'date': date}) + 'date': date, + 'message': message})