From 4f17a85dc73f456be259fe07a7ef64ee7c503317 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Sun, 4 Feb 2018 21:48:29 +0100 Subject: [PATCH 1/9] add total summ --- .../didgeridoo/webshop/templates/webshop/cart.html | 13 +++++++++++-- django/didgeridoo/webshop/views.py | 10 +++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/django/didgeridoo/webshop/templates/webshop/cart.html b/django/didgeridoo/webshop/templates/webshop/cart.html index 637d524..be29662 100644 --- a/django/didgeridoo/webshop/templates/webshop/cart.html +++ b/django/didgeridoo/webshop/templates/webshop/cart.html @@ -17,12 +17,21 @@ {{ article.article.name }} - + + {{ article.article.stock }} {{ article.amount }} - {{ article.article.price_in_chf }} {{ currency_name }} + + {{ article.article.price_in_chf }} + {{ currency_name }} + {% endfor %} + + + Total: {{ total }} + + {% else %}

diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 32fab77..8ca0986 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -9,10 +9,9 @@ from webshop.forms import RegistrationForm, AddToCartForm from currencies.models import ExchangeRate, ExchangeRate_name from currencies.forms import CurrenciesForm +from decimal import Decimal -# Create your views here. - def get_categories(): parent_category_list = Category.objects.filter(parent_category=None) category_list = {} @@ -234,6 +233,7 @@ def cart(request): message = "" cart_id = False articles_list = "" + total = Decimal(0) if not 'currency' in request.session: request.session['currency'] = None @@ -262,13 +262,17 @@ def cart(request): currency, article.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) + article.price_in_chf = rate.exchange( + currency, + article.price_in_chf) + total += article.price_in_chf else: articles = CartPosition.objects.filter(cart=cart_id) articles_list = list(articles) return render(request, 'webshop/cart.html', {'articles_list': articles_list, + 'total': total, 'currencies_form': currencies_form, 'article_view': article_view, 'currency_name': currency_name, From d0217960d3c1beccf76c278af3aa2351d5251f31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Sun, 11 Feb 2018 16:24:36 +0100 Subject: [PATCH 2/9] how to get(article.id) in line 273 --- django/didgeridoo/webshop/views.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 8ca0986..7018f9a 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -265,10 +265,14 @@ def cart(request): article.price_in_chf = rate.exchange( currency, article.price_in_chf) - total += article.price_in_chf else: articles = CartPosition.objects.filter(cart=cart_id) articles_list = list(articles) + for idx, article in enumerate(articles_list): + articles_list[idx] = article + article.price_in_chf = CartPosition.objects.get(article.article.id) + + total += article.price_in_chf return render(request, 'webshop/cart.html', {'articles_list': articles_list, From 8b6568c64032518de6783a1986540e7b6e41efc1 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 11 Feb 2018 16:48:45 +0100 Subject: [PATCH 3/9] add a rough version of the "total" calculation --- django/didgeridoo/webshop/views.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 7018f9a..83deedf 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -233,7 +233,8 @@ def cart(request): message = "" cart_id = False articles_list = "" - total = Decimal(0) + prices_in_cart = [] + total = 0 if not 'currency' in request.session: request.session['currency'] = None @@ -266,13 +267,15 @@ def cart(request): currency, article.price_in_chf) else: - articles = CartPosition.objects.filter(cart=cart_id) - articles_list = list(articles) - for idx, article in enumerate(articles_list): - articles_list[idx] = article - article.price_in_chf = CartPosition.objects.get(article.article.id) - - total += article.price_in_chf + cart_position = CartPosition.objects.filter(cart=cart_id) + if len(cart_position) > 0: + cart_position_list = list(cart_position) + for idx, cart_position in enumerate(cart_position_list): + prices_in_cart.append(cart_position.article.price_in_chf) + prices_sum = sum(prices_in_cart) + prices_length = len(prices_in_cart) + total = prices_sum / prices_length + articles_list = cart_position_list return render(request, 'webshop/cart.html', {'articles_list': articles_list, From 775f32887acd1707ba99dff16358802de94221f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Sun, 11 Feb 2018 18:19:21 +0100 Subject: [PATCH 4/9] add art# and Price of all items --- .../webshop/templates/webshop/cart.html | 10 +++- django/didgeridoo/webshop/views.py | 57 +++++++++++++------ 2 files changed, 46 insertions(+), 21 deletions(-) diff --git a/django/didgeridoo/webshop/templates/webshop/cart.html b/django/didgeridoo/webshop/templates/webshop/cart.html index be29662..9b6c8a7 100644 --- a/django/didgeridoo/webshop/templates/webshop/cart.html +++ b/django/didgeridoo/webshop/templates/webshop/cart.html @@ -5,17 +5,20 @@ {% if articles_list %} - + + + {% for article in articles_list %} + @@ -25,10 +28,11 @@ {{ article.article.price_in_chf }} {{ currency_name }} + {% endfor %} - diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 83deedf..ad5e0bb 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -238,6 +238,8 @@ def cart(request): if not 'currency' in request.session: request.session['currency'] = None + else: + currency = request.session['currency'] if request.method == 'POST': currencies_form = CurrenciesForm(request.POST) @@ -254,31 +256,50 @@ def cart(request): cart_id = ShoppingCart.objects.get(user=request.user) except Exception as e: message = "You have no items in the Basket" - if cart_id and request.session['currency']: + + if cart_id: articles = CartPosition.objects.filter(cart=cart_id) articles_list = list(articles) - currency = request.session['currency'] for idx, article in enumerate(articles_list): - article.price_in_chf = rate.exchange( - currency, article.article.price_in_chf) + print(article, idx) + if currency is not None: + 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 articles_list[idx] = article - currency_name = ExchangeRate_name.objects.get(id=currency) - article.price_in_chf = rate.exchange( - currency, - article.price_in_chf) - else: - cart_position = CartPosition.objects.filter(cart=cart_id) - if len(cart_position) > 0: - cart_position_list = list(cart_position) - for idx, cart_position in enumerate(cart_position_list): - prices_in_cart.append(cart_position.article.price_in_chf) - prices_sum = sum(prices_in_cart) - prices_length = len(prices_in_cart) - total = prices_sum / prices_length - articles_list = cart_position_list + + 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) + # currency = request.session['currency'] + # for idx, article in enumerate(articles_list): + # article.price_in_chf = rate.exchange( + # currency, article.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) + # prices_in_cart.append(article.article.price_in_chf) + # + # if cart_id: + # articles = CartPosition.objects.filter(cart=cart_id) + # articles_list = list(articles) + # for idx, article in enumerate(articles_list): + # prices_in_cart.append(article.article.price_in_chf) + + total = sum(prices_in_cart) return render(request, 'webshop/cart.html', {'articles_list': articles_list, + 'totalprice_of_one': totalprice_of_one, 'total': total, 'currencies_form': currencies_form, 'article_view': article_view, From 080bb7823dca4a9a3a22d64e9fcea13d27763460 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Sun, 11 Feb 2018 19:58:49 +0100 Subject: [PATCH 5/9] commit of pseudo idea of the form --- django/didgeridoo/webshop/forms.py | 20 ++++++++- django/didgeridoo/webshop/views.py | 70 ++++++++++++++++++++---------- 2 files changed, 67 insertions(+), 23 deletions(-) diff --git a/django/didgeridoo/webshop/forms.py b/django/didgeridoo/webshop/forms.py index 680f7a8..3cca54d 100644 --- a/django/didgeridoo/webshop/forms.py +++ b/django/didgeridoo/webshop/forms.py @@ -1,5 +1,12 @@ from django import forms -from webshop.models import Salutation, City, Picture, Article, Option +from webshop.models import ( + Salutation, + City, + Picture, + Article, + Option, + OrderPosition + ) class RegistrationForm(forms.Form): @@ -58,3 +65,14 @@ class AddToCartForm(forms.Form): label='Amount in piece.', help_text="Enter a Value between 1 and 99.", initial=1) + + +class CartForm(forms.Form): + def ChangeAmount(self): + article = OrderPosition.objects.filter(pk=self.id) + amountfield = forms.IntegerField( + label='pce', + help_text='Enter a Value between 1 and 99.', + initial=article.amount + ) + return amountfield diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index ad5e0bb..75b6ddd 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -3,9 +3,17 @@ 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, CartPosition, ShoppingCart) -from webshop.forms import RegistrationForm, AddToCartForm +from webshop.models import (Article, + Category, + ArticleStatus, + Person, + City, + Picture, + CartPosition, + ShoppingCart) +from webshop.forms import (RegistrationForm, + AddToCartForm, + CartForm) from currencies.models import ExchangeRate, ExchangeRate_name from currencies.forms import CurrenciesForm @@ -125,11 +133,9 @@ def article_details(request, article_id): picture_list = Picture.objects.filter(article=article_id) if request.method == 'POST': - print(request.POST) # hier wird das Currency dropdown bearbeitet: if 'currencies' in request.POST: currencies_form = CurrenciesForm(request.POST) - print("currencies_form") if currencies_form.is_valid(): cf = currencies_form.cleaned_data if cf['currencies']: @@ -143,14 +149,9 @@ def article_details(request, article_id): # hier wird der Artikel in den Wahrenkorb transferiert: if 'amount' in request.POST: amount = AddToCartForm(request.POST) - print("add_to_cart_form") if amount.is_valid(): - print("is valid") amount = amount.cleaned_data['amount'] currency_id = request.session['currency'] - print("amount:", amount, - "article_id:", article_id, - "currency_id:", currency_id) article = Article.objects.get(id=article_id) try: cart_id = ShoppingCart.objects.get(user=request.user) @@ -234,6 +235,7 @@ def cart(request): cart_id = False articles_list = "" prices_in_cart = [] + totalprice_of_one = [] total = 0 if not 'currency' in request.session: @@ -242,15 +244,40 @@ def cart(request): 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 + # here we react to a currency dropdown change: + if 'currencies' in request.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 + # here we react to a change of amount per item in the Cart: + if 'amount' in request.POST: + print(request.POST) + amount = CartForm.ChangeAmount(request.POST) + if amount.is_valid(): + amount = amount.cleaned_data['amount'] + article = Article.objects.get(id=article_id) + try: + cart_id = ShoppingCart.objects.get(user=request.user) + except: + cart_id = ShoppingCart.objects.create(user=request.user) + cart_id.save() + if cart_id: + cart_position = CartPosition.objects.create( + article=article, + amount=amount, + cart=ShoppingCart.objects.get(user=request.user) + ) + cart_position.save() + amount = CartForm.ChangeAmount() + else: + amount = AddToCartForm() try: cart_id = ShoppingCart.objects.get(user=request.user) @@ -261,8 +288,8 @@ def cart(request): articles = CartPosition.objects.filter(cart=cart_id) articles_list = list(articles) for idx, article in enumerate(articles_list): - print(article, idx) if currency is not None: + articles_list[idx] = article article.price_in_chf = rate.exchange( currency, article.article.price_in_chf) currency_name = ExchangeRate_name.objects.get(id=currency) @@ -271,7 +298,6 @@ def cart(request): article.price_in_chf) amount = article.amount totalprice_of_one = Decimal(amount) * article.article.price_in_chf - articles_list[idx] = article prices_in_cart.append(article.article.price_in_chf) @@ -299,7 +325,7 @@ def cart(request): return render(request, 'webshop/cart.html', {'articles_list': articles_list, - 'totalprice_of_one': totalprice_of_one, + 'totalprice_of_one': totalprice_of_one, 'total': total, 'currencies_form': currencies_form, 'article_view': article_view, From 5220b4d5677da1660c07ee32d687a5caf047291d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Sun, 11 Feb 2018 20:07:18 +0100 Subject: [PATCH 6/9] add comments of thaught --- django/didgeridoo/webshop/views.py | 36 +++++++++--------------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 75b6ddd..65fdc05 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -278,7 +278,7 @@ def cart(request): amount = CartForm.ChangeAmount() else: amount = AddToCartForm() - + # if the cart_id is set the user has already added items to cart. try: cart_id = ShoppingCart.objects.get(user=request.user) except Exception as e: @@ -287,40 +287,26 @@ def cart(request): if cart_id: articles = CartPosition.objects.filter(cart=cart_id) articles_list = list(articles) + # scrap out the details to calculate Total of item and Summ of All: for idx, article in enumerate(articles_list): + # only recalculate prices if currency is not CHF: if currency is not None: + # no idea what this does: (!!!) articles_list[idx] = article + # get price of item in CHF: article.price_in_chf = rate.exchange( currency, article.article.price_in_chf) + # get currencyname to display: currency_name = ExchangeRate_name.objects.get(id=currency) + # get exchange_rate multiplyed: article.price_in_chf = rate.exchange( currency, article.price_in_chf) - amount = article.amount - totalprice_of_one = Decimal(amount) * article.article.price_in_chf - + # calculate item * price for one row in cart: + totalprice_of_one = Decimal(article.amount) * article.article.price_in_chf + # add to a list of total prices per item 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) - # currency = request.session['currency'] - # for idx, article in enumerate(articles_list): - # article.price_in_chf = rate.exchange( - # currency, article.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) - # prices_in_cart.append(article.article.price_in_chf) - # - # if cart_id: - # articles = CartPosition.objects.filter(cart=cart_id) - # articles_list = list(articles) - # for idx, article in enumerate(articles_list): - # prices_in_cart.append(article.article.price_in_chf) - + # sum the list of totalprices of items to a summ of all items in cart. total = sum(prices_in_cart) return render(request, 'webshop/cart.html', From 7a9c06bbba4efa497cb34cd1669e8a88b9c41509 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 11 Feb 2018 21:56:53 +0100 Subject: [PATCH 7/9] push for review --- django/didgeridoo/webshop/models.py | 9 +++++++++ .../webshop/templates/webshop/cart.html | 7 +++++-- .../webshop/templatetags/cart_filters.py | 7 +++++++ django/didgeridoo/webshop/views.py | 16 +++++++++------- 4 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 django/didgeridoo/webshop/templatetags/cart_filters.py 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 @@
- + + {% for article in articles_list %} @@ -28,7 +30,8 @@ {{ article.article.price_in_chf }} {{ currency_name }} - + + {% 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, From 9120b019aa290ecabb85aa6fa5dacf4fbbe31350 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 11 Feb 2018 22:37:27 +0100 Subject: [PATCH 8/9] remove the solution with the filter --- django/didgeridoo/webshop/templates/webshop/cart.html | 5 +---- django/didgeridoo/webshop/templatetags/cart_filters.py | 7 ------- django/didgeridoo/webshop/views.py | 8 +++----- 3 files changed, 4 insertions(+), 16 deletions(-) delete mode 100644 django/didgeridoo/webshop/templatetags/cart_filters.py diff --git a/django/didgeridoo/webshop/templates/webshop/cart.html b/django/didgeridoo/webshop/templates/webshop/cart.html index 047d6e8..d54c385 100644 --- a/django/didgeridoo/webshop/templates/webshop/cart.html +++ b/django/didgeridoo/webshop/templates/webshop/cart.html @@ -1,5 +1,4 @@ {% extends "webshop/base.html" %} -{% load cart_filters %} {% block section_title %}

Cart

{% endblock %} {% block content %}

List of Items in your Shopping Cart:

@@ -12,8 +11,7 @@
- - + {% for article in articles_list %} @@ -31,7 +29,6 @@ {{ currency_name }} - {% endfor %} diff --git a/django/didgeridoo/webshop/templatetags/cart_filters.py b/django/didgeridoo/webshop/templatetags/cart_filters.py deleted file mode 100644 index 23494b4..0000000 --- a/django/didgeridoo/webshop/templatetags/cart_filters.py +++ /dev/null @@ -1,7 +0,0 @@ -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 9c88cdf..8f3a5c6 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -235,7 +235,7 @@ def cart(request): cart_id = False articles_list = "" prices_in_cart = [] - totalprice_list = {} + totalprice_list = [] total = 0 if not 'currency' in request.session: @@ -301,9 +301,7 @@ def cart(request): currency, article.price_in_chf) amount = Decimal.from_float(article.amount) - totalprice_list.update({ - article.article.id:amount * article.article.price_in_chf - }) + totalprice_list.append(article.position_price) articles_list[idx] = article # if cart_id and request.session['currency']: @@ -326,7 +324,7 @@ def cart(request): # for idx, article in enumerate(articles_list): # prices_in_cart.append(article.article.price_in_chf) - total = sum(totalprice_list.values()) + total = sum(totalprice_list) return render(request, 'webshop/cart.html', {'articles_list': articles_list, From 059ca9d0f7ac781baa166e5cc5220ce6d0d134f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Sun, 11 Feb 2018 23:49:36 +0100 Subject: [PATCH 9/9] add update routine for existing items --- django/didgeridoo/webshop/views.py | 46 +++++++++++++----------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 8f3a5c6..0280c19 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -154,17 +154,31 @@ def article_details(request, article_id): currency_id = request.session['currency'] article = Article.objects.get(id=article_id) try: + # lookup if cart_id is already existent: cart_id = ShoppingCart.objects.get(user=request.user) except: + # if cart_id is not existent create a cart: cart_id = ShoppingCart.objects.create(user=request.user) cart_id.save() if cart_id: - cart_position = CartPosition.objects.create( - article=article, - amount=amount, - cart=ShoppingCart.objects.get(user=request.user) - ) - cart_position.save() + # check if the article is existent in cart already: + try: + article_amount = CartPosition.objects.get( + article=article_id) + new_amount = article_amount.amount + amount + # if article is in cart already update amount: + cart_position = CartPosition.objects.update( + amount=new_amount + ) + except Exception as e: + # if the article is not in cart yet add full item: + cart_position = CartPosition.objects.create( + article=article, + amount=amount, + cart=ShoppingCart.objects.get(user=request.user) + ) + cart_position.save() + # write default value (1) to form field: amount = AddToCartForm() else: amount = AddToCartForm() @@ -304,26 +318,6 @@ def cart(request): totalprice_list.append(article.position_price) articles_list[idx] = article - # if cart_id and request.session['currency']: - # articles = CartPosition.objects.filter(cart=cart_id) - # articles_list = list(articles) - # currency = request.session['currency'] - # for idx, article in enumerate(articles_list): - # article.price_in_chf = rate.exchange( - # currency, article.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) - # prices_in_cart.append(article.article.price_in_chf) - # - # if cart_id: - # articles = CartPosition.objects.filter(cart=cart_id) - # articles_list = list(articles) - # for idx, article in enumerate(articles_list): - # prices_in_cart.append(article.article.price_in_chf) - total = sum(totalprice_list) return render(request, 'webshop/cart.html',
IDPOS.ART# NAME STOCK AMOUNTPRICE p.pce. PRICE
{{ article.id }}{{ article.article.id }} - + {{ article.article.name }} {{ totalprice_of_one }}
+ Total: {{ total }}
STOCK AMOUNT PRICE p.pce.PRICEPRICE through ModelPRICE through Filter
{{ totalprice_of_one }}{{ article.position_price }}{{ totalprice_list|lookup:article.article.id }}
STOCK AMOUNT PRICE p.pce.PRICE through ModelPRICE through FilterPOSITION PRICE
{{ article.position_price }}{{ totalprice_list|lookup:article.article.id }}