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)
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)
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 %}
-
{% endblock %}
diff --git a/django/didgeridoo/webshop/templates/registration/register.html b/django/didgeridoo/webshop/templates/registration/register.html
index 032758f..5e5d2f4 100644
--- a/django/didgeridoo/webshop/templates/registration/register.html
+++ b/django/didgeridoo/webshop/templates/registration/register.html
@@ -8,12 +8,12 @@
Please correct the error{{ form.errors|pluralize }} below.
{% endif %}
-
{% endblock %}
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/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 @@
-
diff --git a/django/didgeridoo/webshop/templates/webshop/category.html b/django/didgeridoo/webshop/templates/webshop/category.html
index 5ea3a14..fe541b4 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 %}
- {% for article in article_list %}
+ {% for article in articles_list %}
{{ article.id }} |
@@ -19,7 +19,7 @@
|
{{ article.category }} |
{{ article.stock }} |
- {{ article.price_in_chf }} |
+ {{ article.price_in_chf }} {{ currency_name }} |
{% endfor %}
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 %}
-
-
+ {% endif %}
+
-
{% endblock %}
diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py
index 040f522..84ab9e4 100644
--- a/django/didgeridoo/webshop/views.py
+++ b/django/didgeridoo/webshop/views.py
@@ -3,77 +3,159 @@ 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 currencies.models import ExchangeRate, ExchangeRate_name
+from currencies.forms import CurrenciesForm
+
# 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
+
+
+def get_hidden_status_id():
+ hidden_status = ArticleStatus.objects.get(name="hidden")
+ 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 not 'currency' in request.session:
+ request.session['currency'] = None
+
+ 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):
+ category_list = get_categories()
selected_category = Category.objects.get(id=category_id)
- hidden = ArticleStatus.objects.get(name="hidden")
+ 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"
- article_list = Article.objects.filter(
- category=selected_category.id).exclude(status=hidden.id)
+ if not 'currency' in request.session:
+ request.session['currency'] = None
- parent_category_list = Category.objects.filter(parent_category=None)
- category_list = {}
+ 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
- for i in parent_category_list:
- category_list.update(
- {i: Category.objects.filter(parent_category=i.id)})
+ 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',
- {'article_list': article_list,
+ {'articles_list': articles_list,
'category_list': category_list,
+ 'currencies_form': currencies_form,
+ 'article_view': article_view,
+ 'currency_name': currency_name,
'category': selected_category})
def article_details(request, article_id):
- parent_category_list = Category.objects.filter(parent_category=None)
- category_list = {}
+ category_list = get_categories()
+ currencies_form = CurrenciesForm
+ rate=ExchangeRate
+ article_view = True
+ currency_name = "CHF"
- for i in parent_category_list:
- category_list.update(
- {i: Category.objects.filter(parent_category=i.id)})
+ 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)
+
+ 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
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)
@@ -99,4 +181,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})