From 1481727a510fcabb8fbd2c974b4878d5ef13dc71 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 28 Jan 2018 20:41:14 +0100 Subject: [PATCH] add the currency calculation to the index page --- .../webshop/templates/webshop/index.html | 2 +- .../webshop/templates/webshop/nav.html | 25 +++++-------- django/didgeridoo/webshop/views.py | 37 +++++++++++++++++-- 3 files changed, 45 insertions(+), 19 deletions(-) diff --git a/django/didgeridoo/webshop/templates/webshop/index.html b/django/didgeridoo/webshop/templates/webshop/index.html index 4d13d5b..f04590a 100644 --- a/django/didgeridoo/webshop/templates/webshop/index.html +++ b/django/didgeridoo/webshop/templates/webshop/index.html @@ -22,7 +22,7 @@ {{ article.category }} {{ article.stock }} - {{ article.price_in_chf }} + {{ article.price_in_chf }} {{ currency_name }} {% endfor %} diff --git a/django/didgeridoo/webshop/templates/webshop/nav.html b/django/didgeridoo/webshop/templates/webshop/nav.html index 169d043..9b01f06 100644 --- a/django/didgeridoo/webshop/templates/webshop/nav.html +++ b/django/didgeridoo/webshop/templates/webshop/nav.html @@ -19,24 +19,19 @@ LOGIN {% endif %} - -
- +
  • + {% if article_view %} + + {{ currencies_form.as_ul }} +
  • + +
  • {% csrf_token %} -
    + + {% endif %} + - {% endblock %} diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 4c01bab..072fe44 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -7,6 +7,8 @@ from webshop.models import (Article, Category, ArticleStatus, Person, City, Picture) from webshop.forms import RegistrationForm +from currencies.models import ExchangeRate, ExchangeRate_name +from currencies.forms import CurrenciesForm # Create your views here. @@ -26,11 +28,40 @@ def get_hidden_status_id(): return hidden_status.id +def index(request): category_list = get_categories() + articles = Article.objects.all().exclude(status=get_hidden_status_id()) + articles_list = list(articles) + currencies_form = CurrenciesForm + rate=ExchangeRate + article_view = True + currency_name = "CHF" + + if request.method == 'POST': + currencies_form = CurrenciesForm(request.POST) + if currencies_form.is_valid(): + cf = currencies_form.cleaned_data + if cf['currencies']: + selection = cf['currencies'] + request.session['currency'] = selection.id + currency_name=ExchangeRate_name.objects.get(id=selection.id) + else: + request.session['currency'] = None + + if request.session['currency']: + currency = request.session['currency'] + for idx, article in enumerate(articles_list): + article.price_in_chf = rate.exchange(currency, article.price_in_chf) + articles_list[idx] = article + currency_name=ExchangeRate_name.objects.get(id=currency) + return render(request, - 'webshop/index.html', - {'category_list': category_list, - 'articles_list': articles_list}) + 'webshop/index.html', + {'category_list': category_list, + 'articles_list': articles_list, + 'currencies_form': currencies_form, + 'article_view': article_view, + 'currency_name': currency_name}) def articles_in_category(request, category_id):