From faba1a597d6ea946b24f3c04d31bcb7967e10e9c Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 28 Jan 2018 20:24:35 +0100 Subject: [PATCH 01/15] add a form for a currency drop down --- django/didgeridoo/currencies/forms.py | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 django/didgeridoo/currencies/forms.py diff --git a/django/didgeridoo/currencies/forms.py b/django/didgeridoo/currencies/forms.py new file mode 100644 index 0000000..23723cd --- /dev/null +++ b/django/didgeridoo/currencies/forms.py @@ -0,0 +1,8 @@ +from django import forms +from currencies.models import ExchangeRate_name + + +class CurrenciesForm(forms.Form): + currencies = forms.ModelChoiceField( + queryset=ExchangeRate_name.objects.all(), + required=False) From 3dbb614bbb7c25f3a40508956ea3abe0ab216d9b Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 28 Jan 2018 20:25:35 +0100 Subject: [PATCH 02/15] add the category list to all the views Since the categories are part of the navigation I think they should be visible from everywhere. --- django/didgeridoo/webshop/views.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 040f522..fafa986 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -68,12 +68,15 @@ def article_details(request, article_id): @login_required def profile(request): + category_list = get_categories() person = Person.objects.get(user=request.user) return render(request, 'registration/profile.html', - {'person': person}) + {'person': person, + 'category_list': category_list}) def registration(request): + category_list = get_categories() if request.method == 'POST': profile_form = RegistrationForm(request.POST) user_form = UserCreationForm(request.POST) From 13ad0fe98e27f2a426c96ee9eb9e48bf72438ae2 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 28 Jan 2018 20:26:41 +0100 Subject: [PATCH 03/15] add a method to the ExchangeRate model to calculate the new currency --- django/didgeridoo/currencies/models.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/django/didgeridoo/currencies/models.py b/django/didgeridoo/currencies/models.py index a5014f3..e2335f9 100644 --- a/django/didgeridoo/currencies/models.py +++ b/django/didgeridoo/currencies/models.py @@ -23,5 +23,9 @@ class ExchangeRate(models.Model): exchange_rate_to_chf = models.DecimalField(max_digits=12, decimal_places=5) + def exchange(_currency_id, _base_currency): + rate = ExchangeRate.objects.filter(name=_currency_id).latest('date') + return round(rate.exchange_rate_to_chf * _base_currency,2) + def __str__(self): return str(self.name) From 138f712e3b9d60ea08a48ed237e18714ae576bff Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 28 Jan 2018 20:28:36 +0100 Subject: [PATCH 04/15] add names and ids to forms and buttons --- django/didgeridoo/webshop/templates/registration/login.html | 4 ++-- .../didgeridoo/webshop/templates/registration/register.html | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/django/didgeridoo/webshop/templates/registration/login.html b/django/didgeridoo/webshop/templates/registration/login.html index 4ff603e..358cdbb 100644 --- a/django/didgeridoo/webshop/templates/registration/login.html +++ b/django/didgeridoo/webshop/templates/registration/login.html @@ -3,10 +3,10 @@ {% block section_title %}Login{% endblock %} {% block content %} -
+ {% 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 index 032758f..833af05 100644 --- a/django/didgeridoo/webshop/templates/registration/register.html +++ b/django/didgeridoo/webshop/templates/registration/register.html @@ -8,7 +8,7 @@ Please correct the error{{ form.errors|pluralize }} below.

{% endif %} -
+ {{ user_form.as_table }} {{ profile_form.as_table }} From c2758afb009b3c8726c2b3c86dd61901908a7463 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 28 Jan 2018 20:30:35 +0100 Subject: [PATCH 05/15] add a name to the register button --- django/didgeridoo/webshop/templates/registration/register.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django/didgeridoo/webshop/templates/registration/register.html b/django/didgeridoo/webshop/templates/registration/register.html index 833af05..5e5d2f4 100644 --- a/django/didgeridoo/webshop/templates/registration/register.html +++ b/django/didgeridoo/webshop/templates/registration/register.html @@ -14,6 +14,6 @@ {{ profile_form.as_table }}
{% csrf_token %} - +
{% endblock %} From ed0649f5fc7da8326a2067fedf88cf2605ee6ad5 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 28 Jan 2018 20:31:51 +0100 Subject: [PATCH 06/15] refactor the models import --- django/didgeridoo/webshop/views.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index fafa986..647844d 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -3,12 +3,8 @@ from django.shortcuts import get_object_or_404, render from django.contrib.auth.decorators import login_required from django.contrib.auth.models import User from django.contrib.auth.forms import UserCreationForm -from webshop.models import (Article, - Category, - ArticleStatus, - Person, - City, - Picture) +from webshop.models import (Article, Category, ArticleStatus, Person, + City, Picture) from webshop.forms import RegistrationForm From 68a8110aeab41d4467568a241658758ae1e34f28 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 28 Jan 2018 20:37:47 +0100 Subject: [PATCH 07/15] add a function to collect the category list --- django/didgeridoo/webshop/views.py | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 647844d..7d4d48e 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -8,19 +8,19 @@ from webshop.models import (Article, Category, ArticleStatus, Person, from webshop.forms import RegistrationForm + # Create your views here. - -def index(request): +def get_categories(): parent_category_list = Category.objects.filter(parent_category=None) category_list = {} - hidden = ArticleStatus.objects.get(name="hidden") - articles_list = Article.objects.all().exclude(status=hidden.id) for i in parent_category_list: category_list.update( {i: Category.objects.filter(parent_category=i.id)}) + return category_list + category_list = get_categories() return render(request, 'webshop/index.html', {'category_list': category_list, @@ -28,18 +28,13 @@ def index(request): def articles_in_category(request, category_id): + category_list = get_categories() selected_category = Category.objects.get(id=category_id) hidden = ArticleStatus.objects.get(name="hidden") article_list = Article.objects.filter( category=selected_category.id).exclude(status=hidden.id) - parent_category_list = Category.objects.filter(parent_category=None) - category_list = {} - - for i in parent_category_list: - category_list.update( - {i: Category.objects.filter(parent_category=i.id)}) return render(request, 'webshop/category.html', {'article_list': article_list, @@ -48,12 +43,7 @@ def articles_in_category(request, category_id): def article_details(request, article_id): - parent_category_list = Category.objects.filter(parent_category=None) - category_list = {} - - for i in parent_category_list: - category_list.update( - {i: Category.objects.filter(parent_category=i.id)}) + category_list = get_categories() article = get_object_or_404(Article, pk=article_id) picture_list = Picture.objects.filter(article=article_id) @@ -98,4 +88,5 @@ def registration(request): user_form = UserCreationForm return render(request, 'registration/register.html', {'profile_form': profile_form, + 'category_list': category_list, 'user_form': user_form}) From e76b3cb80d283960d3ca3404dcdff4fbe61a5648 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 28 Jan 2018 20:39:22 +0100 Subject: [PATCH 08/15] add a function to get the id of the hidden status --- django/didgeridoo/webshop/views.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 7d4d48e..4c01bab 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -20,6 +20,12 @@ def get_categories(): {i: Category.objects.filter(parent_category=i.id)}) return category_list + +def get_hidden_status_id(): + hidden_status = ArticleStatus.objects.get(name="hidden") + return hidden_status.id + + category_list = get_categories() return render(request, 'webshop/index.html', From 1481727a510fcabb8fbd2c974b4878d5ef13dc71 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 28 Jan 2018 20:41:14 +0100 Subject: [PATCH 09/15] 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): From fb6831920c1bd31372c6943b3b4a3e05e84def4a Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 28 Jan 2018 20:41:32 +0100 Subject: [PATCH 10/15] remove jquery from the base template --- django/didgeridoo/webshop/templates/webshop/base.html | 1 - 1 file changed, 1 deletion(-) diff --git a/django/didgeridoo/webshop/templates/webshop/base.html b/django/didgeridoo/webshop/templates/webshop/base.html index 58f7905..e52a4f4 100644 --- a/django/didgeridoo/webshop/templates/webshop/base.html +++ b/django/didgeridoo/webshop/templates/webshop/base.html @@ -3,7 +3,6 @@ - From 62db260ec1147d20b56bc58143dbc54a18af3d38 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 28 Jan 2018 21:09:27 +0100 Subject: [PATCH 11/15] rename the articles_list in the category view --- django/didgeridoo/webshop/templates/webshop/category.html | 4 ++-- django/didgeridoo/webshop/views.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/django/didgeridoo/webshop/templates/webshop/category.html b/django/didgeridoo/webshop/templates/webshop/category.html index 5ea3a14..9b99c4a 100644 --- a/django/didgeridoo/webshop/templates/webshop/category.html +++ b/django/didgeridoo/webshop/templates/webshop/category.html @@ -1,7 +1,7 @@ {% extends "webshop/base.html" %} {% block section_title %}Category Overview{% endblock %} {% block content %} - {% if article_list %} + {% if articles_list %} @@ -10,7 +10,7 @@ - {% for article in article_list %} + {% for article in articles_list %} - + {% endfor %}
    IDSTOCK PRICE
    {{ article.id }} diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 072fe44..43911fc 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -69,12 +69,12 @@ def articles_in_category(request, category_id): selected_category = Category.objects.get(id=category_id) hidden = ArticleStatus.objects.get(name="hidden") - article_list = Article.objects.filter( - category=selected_category.id).exclude(status=hidden.id) + articles_list = Article.objects.filter( + category=selected_category.id).exclude(status=get_hidden_status_id()) return render(request, 'webshop/category.html', - {'article_list': article_list, + {'articles_list': articles_list, 'category_list': category_list, 'category': selected_category}) From 0162e1091565614244fa387c161e3e16765c5cce Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 28 Jan 2018 21:09:57 +0100 Subject: [PATCH 12/15] remove old code --- django/didgeridoo/webshop/views.py | 1 - 1 file changed, 1 deletion(-) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 43911fc..5bf1ebf 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -67,7 +67,6 @@ def index(request): def articles_in_category(request, category_id): category_list = get_categories() selected_category = Category.objects.get(id=category_id) - hidden = ArticleStatus.objects.get(name="hidden") articles_list = Article.objects.filter( category=selected_category.id).exclude(status=get_hidden_status_id()) From 915f31c4132c0ca854ca8a63a3a3925326e1ee95 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 28 Jan 2018 21:14:55 +0100 Subject: [PATCH 13/15] add the currency calculation to the categories view --- .../webshop/templates/webshop/category.html | 2 +- django/didgeridoo/webshop/views.py | 28 +++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/django/didgeridoo/webshop/templates/webshop/category.html b/django/didgeridoo/webshop/templates/webshop/category.html index 9b99c4a..fe541b4 100644 --- a/django/didgeridoo/webshop/templates/webshop/category.html +++ b/django/didgeridoo/webshop/templates/webshop/category.html @@ -19,7 +19,7 @@ {{ article.category }} {{ article.stock }}{{ article.price_in_chf }}{{ article.price_in_chf }} {{ currency_name }}
    diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 5bf1ebf..f140bd3 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -67,14 +67,38 @@ def index(request): def articles_in_category(request, category_id): category_list = get_categories() selected_category = Category.objects.get(id=category_id) - - articles_list = Article.objects.filter( + articles = Article.objects.filter( category=selected_category.id).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/category.html', {'articles_list': articles_list, 'category_list': category_list, + 'currencies_form': currencies_form, + 'article_view': article_view, + 'currency_name': currency_name, 'category': selected_category}) From 6ee5f56e732be7702d016bc077696adf836f9488 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 28 Jan 2018 21:26:36 +0100 Subject: [PATCH 14/15] add the currency calculation to the article view --- .../templates/webshop/article_details.html | 4 ++-- django/didgeridoo/webshop/views.py | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/django/didgeridoo/webshop/templates/webshop/article_details.html b/django/didgeridoo/webshop/templates/webshop/article_details.html index 3c1c3bb..8703063 100644 --- a/django/didgeridoo/webshop/templates/webshop/article_details.html +++ b/django/didgeridoo/webshop/templates/webshop/article_details.html @@ -4,8 +4,8 @@

    Description

    {{ article.description }}

    Stock: {{ article.stock }}

    -

    Status: {{ article.status}}

    -

    Price: {{ article.price_in_chf }}

    +

    Status: {{ article.status }}

    +

    Price: {{ article.price_in_chf }} {{ currency_name }}

    {% for picture in picture_list %}

    {% endfor %} diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index f140bd3..ab31203 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -104,12 +104,36 @@ def articles_in_category(request, category_id): def article_details(request, article_id): category_list = get_categories() + currencies_form = CurrenciesForm + rate=ExchangeRate + article_view = True + currency_name = "CHF" article = get_object_or_404(Article, pk=article_id) picture_list = Picture.objects.filter(article=article_id) + + 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'] + article.price_in_chf = rate.exchange(currency, article.price_in_chf) + currency_name=ExchangeRate_name.objects.get(id=currency) + return render(request, 'webshop/article_details.html', {'article': article, 'category_list': category_list, + 'currencies_form': currencies_form, + 'article_view': article_view, + 'currency_name': currency_name, 'picture_list': picture_list}) @login_required From c5dbcc3d580153833cf23d343d07b21952e743ad Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 28 Jan 2018 21:31:27 +0100 Subject: [PATCH 15/15] add a check for the currency key in the session --- django/didgeridoo/webshop/views.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index ab31203..84ab9e4 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -37,6 +37,9 @@ def index(request): article_view = True currency_name = "CHF" + if not 'currency' in request.session: + request.session['currency'] = None + if request.method == 'POST': currencies_form = CurrenciesForm(request.POST) if currencies_form.is_valid(): @@ -75,6 +78,9 @@ def articles_in_category(request, category_id): article_view = True currency_name = "CHF" + if not 'currency' in request.session: + request.session['currency'] = None + if request.method == 'POST': currencies_form = CurrenciesForm(request.POST) if currencies_form.is_valid(): @@ -109,6 +115,9 @@ def article_details(request, article_id): article_view = True currency_name = "CHF" + if not 'currency' in request.session: + request.session['currency'] = None + article = get_object_or_404(Article, pk=article_id) picture_list = Picture.objects.filter(article=article_id)