From cbbe74a5b3857c8d46eaa6230249ca9c638a9327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Mon, 19 Feb 2018 22:58:25 +0100 Subject: [PATCH 1/6] created funciton to restrict cart to get several same items --- django/didgeridoo/webshop/forms.py | 12 +-- .../webshop/templates/webshop/cart.html | 1 + django/didgeridoo/webshop/views.py | 99 +++++++++---------- 3 files changed, 49 insertions(+), 63 deletions(-) diff --git a/django/didgeridoo/webshop/forms.py b/django/didgeridoo/webshop/forms.py index 925929a..92d5f4d 100644 --- a/django/didgeridoo/webshop/forms.py +++ b/django/didgeridoo/webshop/forms.py @@ -68,20 +68,10 @@ class AddToCartForm(forms.Form): class CartForm(forms.Form): - amount_field = forms.IntegerField( + amount_form = forms.FloatField( label='pce', help_text='Enter a Value between 1 and 99.') - def change_amount(self, article): - print('CartForm.ChangeAmount') - position = CartPosition.objects.get(article=article) - self.amount_field = forms.IntegerField( - label='pce', - help_text='Enter a Value between 1 and 99.', - initial=position.amount - ) - return self.amount_field - class CheckoutForm(forms.Form): diff --git a/django/didgeridoo/webshop/templates/webshop/cart.html b/django/didgeridoo/webshop/templates/webshop/cart.html index 9b7f44d..cce7f09 100644 --- a/django/didgeridoo/webshop/templates/webshop/cart.html +++ b/django/didgeridoo/webshop/templates/webshop/cart.html @@ -26,6 +26,7 @@
{{ amount_form.as_p }} + {% csrf_token %}
diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 498ed57..2aea5ee 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -66,6 +66,37 @@ def articles_in_category(request, category_id): 'category': selected_category}) +def restrict_cart_to_one_article(user_name, article_id, amount): + article = Article.objects.get(id=article_id) + try: + # lookup if cart_id is already existent: + cart_id = ShoppingCart.objects.get(user=user_name) + except: + # if cart_id is not existent create a cart: + cart_id = ShoppingCart.objects.create(user=user_name) + cart_id.save() + if cart_id: + # 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.filter( + id=article_id).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, + position_price=article.price_in_chf, + cart=ShoppingCart.objects.get(user=user_name) + ) + cart_position.save() + + def article_details(request, article_id): category_list = get_categories() currencies_form = CurrenciesForm @@ -99,35 +130,8 @@ def article_details(request, article_id): amount = AddToCartForm(request.POST) if amount.is_valid(): amount = amount.cleaned_data['amount'] - 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: - # 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.filter( - id=article_id).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, - position_price=article.price_in_chf, - cart=ShoppingCart.objects.get(user=request.user) - ) - cart_position.save() + user_name = request.user + restrict_cart_to_one_article(user_name, article_id, amount) # write default value (1) to form field: amount = AddToCartForm() else: @@ -202,7 +206,7 @@ def cart(request): prices_in_cart = [] totalprice_list = [] total = 0 - + user_name = request.user # here we configure the users Currency: if not 'currency' in request.session: request.session['currency'] = None @@ -223,7 +227,7 @@ def cart(request): cart_position_list = list(articles) # scrap out the details to calculate Total of item and Summ of All: for idx, cart_position in enumerate(cart_position_list): - article = CartPosition.objects.get( + article = CartPosition.objects.filter( cart=cart_id, article=cart_position.article.id ) @@ -241,7 +245,7 @@ def cart(request): cart_position_list[idx] = cart_position amount_form = CartForm( - initial=cart_position.amount + # initial=cart_position.amount ) total = sum(totalprice_list) @@ -263,28 +267,19 @@ def cart(request): else: request.session['currency'] = None # here we react to a change of amount per item in the Cart: - if 'amount_field' in request.POST: + if 'amount_form' in request.POST: print('yes amount post') - amount_form = CartForm(request.POST, - cart_id, - cart_position.article.id) + amount_form = CartForm(request.POST) if amount_form.is_valid(): - amount = amount_form.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() - + amount = amount_form.cleaned_data['amount_form'] + article_id = request.POST.get('article_id') + restrict_cart_to_one_article(user_name, article_id, amount) + article = CartPosition.objects.get(article=article_id) + articleamount = article.amount + print('articleamount', articleamount) + amount_form = CartForm( + initial=articleamount + ) if 'checkout' in request.POST: print('checkout') checkout_form = CheckoutForm(request.POST) From ae1e5e5051214973fd8c37bc23168f0a420b8f44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Tue, 20 Feb 2018 20:59:18 +0100 Subject: [PATCH 2/6] add initial value with dictionary --- django/didgeridoo/webshop/views.py | 87 +++++++++++++++--------------- 1 file changed, 43 insertions(+), 44 deletions(-) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index eac4125..53b43f8 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -211,6 +211,43 @@ def cart(request): else: currency = request.session['currency'] +# Here we handle all POST Operations: + if request.method == 'POST': + print(request.POST) + # here we react to a currency dropdown change: + if 'currencies' in request.POST: + print('currencies') + 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_form' in request.POST: + print('amount_form yes amount post') + amount_form = CartForm(request.POST) + if amount_form.is_valid(): + amount = amount_form.cleaned_data['amount_form'] + article_id = request.POST.get('article_id') + restrict_cart_to_one_article(user_name, article_id, amount) + article = CartPosition.objects.get(article=article_id) + articleamount = article.amount + print('amount_form articleamount:', articleamount) + + if 'checkout' in request.POST: + print('checkout') + checkout_form = CheckoutForm(request.POST) + if checkout_form.is_valid(): + checkout_form = checkout_form.cleaned_data['checkout'] + print('views checkout checkout_form', checkout_form) + if checkout_form is True: + # add to order + order = '' # here we handle the normal cart view: # if the cart_id is set the user has already added items to cart. try: @@ -240,54 +277,16 @@ def cart(request): cart_position.price_in_chf ) totalprice_list.append(cart_position.price_in_chf) + + print('cart cart_position.article.id', cart_position.article.id, + 'articleamount:', cart_position.amount) + amount_form = CartForm( + initial={'amount_form': cart_position.amount} + ) cart_position_list[idx] = cart_position - amount_form = CartForm( - # initial=cart_position.amount - ) - total = sum(totalprice_list) -# Here we handle all POST Operations: - if request.method == 'POST': - print(request.POST) - # here we react to a currency dropdown change: - if 'currencies' in request.POST: - print('currencies') - 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_form' in request.POST: - print('yes amount post') - amount_form = CartForm(request.POST) - if amount_form.is_valid(): - amount = amount_form.cleaned_data['amount_form'] - article_id = request.POST.get('article_id') - restrict_cart_to_one_article(user_name, article_id, amount) - article = CartPosition.objects.get(article=article_id) - articleamount = article.amount - print('articleamount', articleamount) - amount_form = CartForm( - initial=articleamount - ) - if 'checkout' in request.POST: - print('checkout') - checkout_form = CheckoutForm(request.POST) - if checkout_form.is_valid(): - checkout_form = checkout_form.cleaned_data['checkout'] - print('views checkout checkout_form', checkout_form) - if checkout_form is True: - # add to order - order = '' - checkout_form = CheckoutForm() return render(request, 'webshop/cart.html', From a6ca58591883ebafccb1398c0dbd55a139ac1561 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Tue, 20 Feb 2018 21:03:53 +0100 Subject: [PATCH 3/6] fix currency connection --- django/didgeridoo/webshop/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 53b43f8..09cac08 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -274,7 +274,7 @@ def cart(request): # get exchange_rate multiplyed: cart_position.price_in_chf = rate.exchange( currency, - cart_position.price_in_chf + cart_position.article.price_in_chf ) totalprice_list.append(cart_position.price_in_chf) From 933cd403365c82b7b869587fd266d7cab80bd4ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Tue, 20 Feb 2018 21:22:25 +0100 Subject: [PATCH 4/6] add operation mode in restrict_cart_to_one_article function --- django/didgeridoo/webshop/views.py | 31 +++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 09cac08..e3c5e99 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -64,7 +64,7 @@ def articles_in_category(request, category_id): 'category': selected_category}) -def restrict_cart_to_one_article(user_name, article_id, amount): +def restrict_cart_to_one_article(user_name, article_id, amount, operation): article = Article.objects.get(id=article_id) try: # lookup if cart_id is already existent: @@ -74,17 +74,25 @@ def restrict_cart_to_one_article(user_name, article_id, amount): cart_id = ShoppingCart.objects.create(user=user_name) cart_id.save() if cart_id: + print('restrict_cart_to_one_article cart_id:', cart_id) # 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 operation == 'add': + new_amount = article_amount.amount + amount + print('restrict_cart_to_one_article add new_amount:', new_amount, + 'article_id', article_id) + if operation == 'replace': + print('restrict_cart_to_one_article replace:', amount) + new_amount = amount # if article is in cart already update amount: cart_position = CartPosition.objects.filter( id=article_id).update( amount=new_amount ) except Exception as e: + print('restrict_cart_to_one_article except: ', e) # if the article is not in cart yet add full item: cart_position = CartPosition.objects.create( article=article, @@ -129,7 +137,13 @@ def article_details(request, article_id): if amount.is_valid(): amount = amount.cleaned_data['amount'] user_name = request.user - restrict_cart_to_one_article(user_name, article_id, amount) + operation = 'add' + restrict_cart_to_one_article( + user_name, + article_id, + amount, + operation + ) # write default value (1) to form field: amount = AddToCartForm() else: @@ -234,10 +248,13 @@ def cart(request): if amount_form.is_valid(): amount = amount_form.cleaned_data['amount_form'] article_id = request.POST.get('article_id') - restrict_cart_to_one_article(user_name, article_id, amount) - article = CartPosition.objects.get(article=article_id) - articleamount = article.amount - print('amount_form articleamount:', articleamount) + operation = 'replace' + restrict_cart_to_one_article( + user_name, + article_id, + amount, + operation + ) if 'checkout' in request.POST: print('checkout') From 799b1ba502d7309b9c2301074055ffe7719e7708 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Tue, 20 Feb 2018 21:57:13 +0100 Subject: [PATCH 5/6] add print helps to find issue in initial value --- django/didgeridoo/webshop/views.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index e3c5e99..5c65278 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -84,8 +84,9 @@ def restrict_cart_to_one_article(user_name, article_id, amount, operation): print('restrict_cart_to_one_article add new_amount:', new_amount, 'article_id', article_id) if operation == 'replace': - print('restrict_cart_to_one_article replace:', amount) new_amount = amount + print('restrict_cart_to_one_article replace:', new_amount, + 'article_id', article_id) # if article is in cart already update amount: cart_position = CartPosition.objects.filter( id=article_id).update( @@ -274,16 +275,16 @@ def cart(request): print('try cart_id exception as: ', e) cart_id = False if cart_id: - print('cart_id', cart_id) + print('cart cart_id', cart_id) + # get all items in the cart of this customer: articles = CartPosition.objects.filter(cart=cart_id) + # make a list out of all articles: cart_position_list = list(articles) - # scrap out the details to calculate Total of item and Summ of All: + # enumerate the list of articles and loop over items: for idx, cart_position in enumerate(cart_position_list): - article = CartPosition.objects.filter( - cart=cart_id, - article=cart_position.article.id - ) + # sub funciton of CartPosition: cart_position.calculate_position_price() + # scrap out the details to calculate Total of item and Summ of All: if currency: print('calc currency') # get currencyname to display: From e79cba32c173d7ce40f52a574bcdcfd456ccc084 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Wed, 21 Feb 2018 08:04:47 +0100 Subject: [PATCH 6/6] create zip to render in html position_list and amount_form. --- django/didgeridoo/webshop/templates/webshop/cart.html | 2 +- django/didgeridoo/webshop/views.py | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/django/didgeridoo/webshop/templates/webshop/cart.html b/django/didgeridoo/webshop/templates/webshop/cart.html index 0caa52b..3937aa2 100644 --- a/django/didgeridoo/webshop/templates/webshop/cart.html +++ b/django/didgeridoo/webshop/templates/webshop/cart.html @@ -13,7 +13,7 @@ PRICE p.pce. POSITION PRICE - {% for cart_position in cart_position_list %} + {% for cart_position, amount_form in cart_position_list_zip %} {{ cart_position.id }} {{ cart_position.article.id }} diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 5c65278..2fca7e7 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -215,8 +215,8 @@ def cart(request): article_view = True currency_name = "CHF" message = "" - cart_position_list = "" - prices_in_cart = [] + cart_position_list = [] + amount_form_list = [] totalprice_list = [] total = 0 user_name = request.user @@ -264,7 +264,7 @@ def cart(request): checkout_form = checkout_form.cleaned_data['checkout'] print('views checkout checkout_form', checkout_form) if checkout_form is True: - # add to order + # todo add to order order = '' # here we handle the normal cart view: # if the cart_id is set the user has already added items to cart. @@ -301,17 +301,18 @@ def cart(request): amount_form = CartForm( initial={'amount_form': cart_position.amount} ) + amount_form_list.append(amount_form) cart_position_list[idx] = cart_position + cart_position_list_zip = zip(cart_position_list, amount_form_list) total = sum(totalprice_list) checkout_form = CheckoutForm() return render(request, 'webshop/cart.html', - {'cart_position_list': cart_position_list, + {'cart_position_list_zip': cart_position_list_zip, 'totalprice_list': totalprice_list, 'total': total, - 'cart_form': cart_form, 'currencies_form': currencies_form, 'amount_form': amount_form, 'article_view': article_view,