diff --git a/django/didgeridoo/webshop/models.py b/django/didgeridoo/webshop/models.py
index c41f4f5..1d966f9 100644
--- a/django/didgeridoo/webshop/models.py
+++ b/django/didgeridoo/webshop/models.py
@@ -100,9 +100,8 @@ class OrderPosition(models.Model):
class ShoppingCart(models.Model):
""" Cart to User Relationships """
- name = models.CharField(max_length=200)
+ name = models.CharField(max_length=200, null=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
- article = models.ManyToManyField(Article)
def __str__(self):
return self.name
diff --git a/django/didgeridoo/webshop/templates/webshop/article_details.html b/django/didgeridoo/webshop/templates/webshop/article_details.html
index eda8ff6..a16a642 100644
--- a/django/didgeridoo/webshop/templates/webshop/article_details.html
+++ b/django/didgeridoo/webshop/templates/webshop/article_details.html
@@ -6,11 +6,15 @@
Stock: {{ article.stock }}
Status: {{ article.status }}
Price: {{ article.price_in_chf }} {{ currency_name }}
-
+ {% if user.is_authenticated %}
+
+ {% else %}
+ please login to fill your basket...
+ {% endif %}
{% for picture in picture_list %}
{% endfor %}
diff --git a/django/didgeridoo/webshop/templates/webshop/cart.html b/django/didgeridoo/webshop/templates/webshop/cart.html
new file mode 100644
index 0000000..bf1f238
--- /dev/null
+++ b/django/didgeridoo/webshop/templates/webshop/cart.html
@@ -0,0 +1,9 @@
+{% extends "webshop/base.html" %}
+{% block section_title %}Cart
{% endblock %}
+{% block content %}
+ List of Items in your Shopping Cart:
+ {{ article.description }}
+ in Stock: {{ article.stock }}
+ Amount you whant: {{ article.amount }}
+ Price: {{ article.price_in_chf }} {{ currency_name }}
+{% endblock %}
diff --git a/django/didgeridoo/webshop/templates/webshop/nav.html b/django/didgeridoo/webshop/templates/webshop/nav.html
index d5c22c9..8aa7f44 100644
--- a/django/didgeridoo/webshop/templates/webshop/nav.html
+++ b/django/didgeridoo/webshop/templates/webshop/nav.html
@@ -6,7 +6,7 @@
HOME
- - CART
+ - CART
{% if user.is_authenticated %}
-
PROFILE
diff --git a/django/didgeridoo/webshop/urls.py b/django/didgeridoo/webshop/urls.py
index 5a4685e..e59619a 100644
--- a/django/didgeridoo/webshop/urls.py
+++ b/django/didgeridoo/webshop/urls.py
@@ -17,4 +17,7 @@ urlpatterns = [
url(r'^registration/$',
views.registration,
name='registration'),
+ url(r'^cart/$',
+ views.cart,
+ name='cart'),
]
diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py
index e16991b..19d8914 100644
--- a/django/didgeridoo/webshop/views.py
+++ b/django/didgeridoo/webshop/views.py
@@ -4,7 +4,7 @@ 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)
+ City, Picture, CartPosition, ShoppingCart)
from webshop.forms import RegistrationForm, AddToCartForm
from currencies.models import ExchangeRate, ExchangeRate_name
@@ -144,7 +144,16 @@ def article_details(request, article_id):
if amount.is_valid():
print("is valid")
amount = amount.cleaned_data['amount']
- print("amount:", amount, "article_id:", article_id)
+ currency_id = request.session['currency']
+ print("amount:", amount,
+ "article_id:", article_id,
+ "currency_id:", currency_id)
+ cart_position = CartPosition.objects.create(
+ article=article_id,
+ amount=amount,
+ cart=ShoppingCart.objects.get(user=request.user)
+ )
+ cart_position.save()
amount = AddToCartForm()
else:
amount = AddToCartForm()
@@ -203,3 +212,42 @@ def registration(request):
{'profile_form': profile_form,
'category_list': category_list,
'user_form': user_form})
+
+
+def cart(request):
+ currencies_form = CurrenciesForm
+ rate = ExchangeRate
+ article_view = True
+ currency_name = "CHF"
+
+ if not 'currency' in request.session:
+ request.session['currency'] = None
+ currency = request.session['currency']
+ 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
+
+ cart_id = ShoppingCart.objects.get(user=request.user)
+ articles = CartPosition.objects.filter(cart=cart_id)
+ articles_list = list(articles)
+ 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)
+ article.price_in_chf = rate.exchange(currency, article.price_in_chf)
+
+ return render(request, 'webshop/cart.html',
+ {'article': article,
+ 'currencies_form': currencies_form,
+ 'article_view': article_view,
+ 'currency_name': currency_name,
+ })