diff --git a/django/didgeridoo/webshop/models.py b/django/didgeridoo/webshop/models.py index a17cb65..9551f84 100644 --- a/django/didgeridoo/webshop/models.py +++ b/django/didgeridoo/webshop/models.py @@ -112,6 +112,15 @@ class CartPosition(models.Model): article = models.ForeignKey(Article, on_delete=models.CASCADE) amount = models.FloatField(max_length=5) cart = models.ForeignKey(ShoppingCart, on_delete=models.CASCADE) + position_price = models.DecimalField(max_digits=19, + decimal_places=2, + validators=[MinValueValidator( + Decimal('0.00'))], + null=True) + + def calculate_position_price(self): + decimal_amount = Decimal.from_float(self.amount) + self.position_price = decimal_amount * self.article.price_in_chf class City(models.Model): diff --git a/django/didgeridoo/webshop/templates/webshop/cart.html b/django/didgeridoo/webshop/templates/webshop/cart.html index 9b6c8a7..047d6e8 100644 --- a/django/didgeridoo/webshop/templates/webshop/cart.html +++ b/django/didgeridoo/webshop/templates/webshop/cart.html @@ -1,4 +1,5 @@ {% extends "webshop/base.html" %} +{% load cart_filters %} {% block section_title %}

Cart

{% endblock %} {% block content %}

List of Items in your Shopping Cart:

@@ -11,7 +12,8 @@ STOCK AMOUNT PRICE p.pce. - PRICE + PRICE through Model + PRICE through Filter {% for article in articles_list %} @@ -28,7 +30,8 @@ {{ article.article.price_in_chf }} {{ currency_name }} - {{ totalprice_of_one }} + {{ article.position_price }} + {{ totalprice_list|lookup:article.article.id }} {% endfor %} diff --git a/django/didgeridoo/webshop/templatetags/cart_filters.py b/django/didgeridoo/webshop/templatetags/cart_filters.py new file mode 100644 index 0000000..23494b4 --- /dev/null +++ b/django/didgeridoo/webshop/templatetags/cart_filters.py @@ -0,0 +1,7 @@ +from django import template + +register = template.Library() + +@register.filter +def lookup(d, key): + return d[key] diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index ad5e0bb..0fc0a2d 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -234,6 +234,7 @@ def cart(request): cart_id = False articles_list = "" prices_in_cart = [] + totalprice_list = {} total = 0 if not 'currency' in request.session: @@ -262,19 +263,20 @@ def cart(request): articles_list = list(articles) for idx, article in enumerate(articles_list): print(article, idx) - if currency is not None: + article.calculate_position_price() + if currency: article.price_in_chf = rate.exchange( currency, article.article.price_in_chf) currency_name = ExchangeRate_name.objects.get(id=currency) article.price_in_chf = rate.exchange( currency, article.price_in_chf) - amount = article.amount - totalprice_of_one = Decimal(amount) * article.article.price_in_chf + amount = Decimal.from_float(article.amount) + totalprice_list.update({ + article.article.id:amount * article.article.price_in_chf + }) articles_list[idx] = article - prices_in_cart.append(article.article.price_in_chf) - # if cart_id and request.session['currency']: # articles = CartPosition.objects.filter(cart=cart_id) # articles_list = list(articles) @@ -295,11 +297,11 @@ def cart(request): # for idx, article in enumerate(articles_list): # prices_in_cart.append(article.article.price_in_chf) - total = sum(prices_in_cart) + total = sum(totalprice_list.values()) return render(request, 'webshop/cart.html', {'articles_list': articles_list, - 'totalprice_of_one': totalprice_of_one, + 'totalprice_list': totalprice_list, 'total': total, 'currencies_form': currencies_form, 'article_view': article_view,