From ff36017fa404a3b78a75e51a49859e379ddc5046 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Sun, 25 Feb 2018 09:30:46 +0100 Subject: [PATCH 01/17] fixed add update and delete function --- django/didgeridoo/webshop/views.py | 79 ++++++++++++++++-------------- 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index c8f5913..9267225 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -65,50 +65,51 @@ def articles_in_category(request, category_id): def restrict_cart_to_one_article(user_name, article_id, amount, operation): + print('operation:', operation) + # if cart_id is not existent create a cart: + cart_id, created_cart = ShoppingCart.objects.get_or_create(user=user_name) + print('restrict_cart_to_one_article cart_id:', cart_id, + 'created_cart', created_cart) + # transfair Article to CartPosition: 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: - print('restrict_cart_to_one_article cart_id:', cart_id) - # check if the article is existent in cart already: + cart_position, created_position = CartPosition.objects.get_or_create( + article=article, + defaults={'amount': amount, + 'position_price': article.price_in_chf, + 'cart': cart_id + } + ) + print('restrict_cart_to_one_article cart_position:', cart_position, + 'created_position', created_position) + if created_position is False: + print('restrict_cart_to_one_article cart_position False') if operation == 'delete': - article.delete() - print('restrict_cart_to_one_article delete article_id:', article_id) - try: - article = CartPosition.objects.get( - article=article_id) + print('restrict_cart_to_one_article delete article_id:', + article_id) + cart_position.delete() + print('restrict_cart_to_one_article cart_position:', + cart_position, + 'created_position', + created_position) + if (operation == 'add') or (operation == 'replace'): + print('yep in add or replace') if operation == 'add': - new_amount = article.amount + amount - print('restrict_cart_to_one_article add new_amount:', new_amount, - 'article_id', article_id) - cart_position = CartPosition.objects.filter( - article=article_id).update( - amount=new_amount - ) + new_amount = cart_position.amount + amount + print('restrict_cart_to_one_article add new_amount:', + new_amount, + 'article_id', + article_id) if operation == 'replace': new_amount = amount # ref two times check later !! - 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( - article=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, - amount=amount, - position_price=article.price_in_chf, - cart=ShoppingCart.objects.get(user=user_name) + 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( + article=article_id).update( + amount=new_amount ) - cart_position.save() def article_details(request, article_id): @@ -259,12 +260,14 @@ def cart(request): amount = amount_form.cleaned_data['amount_form'] article_id = request.POST.get('article_id') operation = 'replace' + print('cart amount_form going in to function restrict_cart_to_one_article') restrict_cart_to_one_article( user_name, article_id, amount, operation ) + print('cart amount_form coming back from function restrict_cart_to_one_article') # here we react to a change of amount per item in the Cart: if 'delete' in request.POST: print('delete yes delete post') From 52a6ae1ecca5c164f93a9d9a898cdb2b251c1919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Sun, 25 Feb 2018 11:46:32 +0100 Subject: [PATCH 02/17] improved message if items in cart are deleted until there are none. --- django/didgeridoo/webshop/views.py | 38 +++++++++++++----------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 9267225..c016935 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -64,14 +64,12 @@ def articles_in_category(request, category_id): 'category': selected_category}) -def restrict_cart_to_one_article(user_name, article_id, amount, operation): +def restrict_cart_to_one_article(user_id, article_id, amount, operation): print('operation:', operation) # if cart_id is not existent create a cart: - cart_id, created_cart = ShoppingCart.objects.get_or_create(user=user_name) - print('restrict_cart_to_one_article cart_id:', cart_id, - 'created_cart', created_cart) - # transfair Article to CartPosition: + cart_id, created_cart = ShoppingCart.objects.get_or_create(user=user_id) article = Article.objects.get(id=article_id) + # transfair Article to CartPosition: cart_position, created_position = CartPosition.objects.get_or_create( article=article, defaults={'amount': amount, @@ -119,6 +117,7 @@ def article_details(request, article_id): rate = ExchangeRate article_view = True currency_name = "CHF" + user_id = request.user.id if 'currency' not in request.session: request.session['currency'] = None @@ -145,10 +144,9 @@ def article_details(request, article_id): amount = AddToCartForm(request.POST) if amount.is_valid(): amount = amount.cleaned_data['amount'] - user_name = request.user operation = 'add' restrict_cart_to_one_article( - user_name, + user_id, article_id, amount, operation @@ -227,7 +225,7 @@ def cart(request): amount_form_list = [] totalprice_list = [] total = 0 - user_name = request.user + user_id = request.user.id cart_position_list_zip = [] # here we configure the users Currency: @@ -262,7 +260,7 @@ def cart(request): operation = 'replace' print('cart amount_form going in to function restrict_cart_to_one_article') restrict_cart_to_one_article( - user_name, + user_id, article_id, amount, operation @@ -278,7 +276,7 @@ def cart(request): amount = 1 operation = 'delete' restrict_cart_to_one_article( - user_name, + user_id, article_id, amount, operation @@ -294,17 +292,15 @@ def cart(request): # 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. - try: - cart_id = ShoppingCart.objects.get(user=request.user.id) - except Exception as e: - message = "You have no items in the Basket" - print('try cart_id exception as: ', e) - cart_id = False - if cart_id: - print('cart cart_id', cart_id) - # get all items in the cart of this customer: - articles = CartPosition.objects.filter(cart=cart_id) + # if cart_id is not existent create a cart: + cart_id, created_cart = ShoppingCart.objects.get_or_create(user=user_id) + print('cart cart_id:', cart_id, + 'created_cart', created_cart) + # get all items in the cart of this customer: + articles = CartPosition.objects.filter(cart=cart_id) + print('cart articles > 0:', articles.count()) + if (articles.count()) > 0: + print('cart articles > 0 = True:', articles.count()) # make a list out of all articles: cart_position_list = list(articles) # enumerate the list of articles and loop over items: From 9f8a01dbaf96c8444090b05b5688801fe5fad6ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Sun, 25 Feb 2018 12:02:32 +0100 Subject: [PATCH 03/17] clean for next step --- django/didgeridoo/webshop/views.py | 34 ++---------------------------- 1 file changed, 2 insertions(+), 32 deletions(-) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index c016935..8ca59b8 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -65,7 +65,6 @@ def articles_in_category(request, category_id): def restrict_cart_to_one_article(user_id, article_id, amount, operation): - print('operation:', operation) # if cart_id is not existent create a cart: cart_id, created_cart = ShoppingCart.objects.get_or_create(user=user_id) article = Article.objects.get(id=article_id) @@ -77,32 +76,14 @@ def restrict_cart_to_one_article(user_id, article_id, amount, operation): 'cart': cart_id } ) - print('restrict_cart_to_one_article cart_position:', cart_position, - 'created_position', created_position) if created_position is False: - print('restrict_cart_to_one_article cart_position False') if operation == 'delete': - print('restrict_cart_to_one_article delete article_id:', - article_id) cart_position.delete() - print('restrict_cart_to_one_article cart_position:', - cart_position, - 'created_position', - created_position) if (operation == 'add') or (operation == 'replace'): - print('yep in add or replace') if operation == 'add': new_amount = cart_position.amount + amount - print('restrict_cart_to_one_article add new_amount:', - new_amount, - 'article_id', - article_id) if operation == 'replace': new_amount = amount # ref two times check later !! - 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( article=article_id).update( @@ -236,7 +217,6 @@ def cart(request): # 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') @@ -244,31 +224,29 @@ def cart(request): if currencies_form.is_valid(): cf = currencies_form.cleaned_data if cf['currencies']: + print('currencies cf:', cf) selection = cf['currencies'] request.session['currency'] = selection.id currency_name = ExchangeRate_name.objects.get( id=selection.id) + print('currencies currency_name:', currency_name) 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') operation = 'replace' - print('cart amount_form going in to function restrict_cart_to_one_article') restrict_cart_to_one_article( user_id, article_id, amount, operation ) - print('cart amount_form coming back from function restrict_cart_to_one_article') # here we react to a change of amount per item in the Cart: if 'delete' in request.POST: - print('delete yes delete post') delete = CartForm(request.POST) if delete.is_valid(): amount = delete.cleaned_data['amount_form'] @@ -294,13 +272,9 @@ def cart(request): # here we handle the normal cart view: # if cart_id is not existent create a cart: cart_id, created_cart = ShoppingCart.objects.get_or_create(user=user_id) - print('cart cart_id:', cart_id, - 'created_cart', created_cart) # get all items in the cart of this customer: articles = CartPosition.objects.filter(cart=cart_id) - print('cart articles > 0:', articles.count()) if (articles.count()) > 0: - print('cart articles > 0 = True:', articles.count()) # make a list out of all articles: cart_position_list = list(articles) # enumerate the list of articles and loop over items: @@ -309,7 +283,6 @@ def cart(request): 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: currency_name = ExchangeRate_name.objects.get(id=currency) # get exchange_rate multiplyed: @@ -318,9 +291,6 @@ def cart(request): cart_position.article.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} ) From 5b1530c0bdd740c59a85e5221b0f331ff7a2d495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Sun, 25 Feb 2018 13:12:13 +0100 Subject: [PATCH 04/17] add checkout template, view and function --- .../webshop/templates/webshop/cart.html | 3 + .../webshop/templates/webshop/checkout.html | 25 +++- django/didgeridoo/webshop/urls.py | 3 + django/didgeridoo/webshop/views.py | 134 ++++++++++++++++-- 4 files changed, 144 insertions(+), 21 deletions(-) diff --git a/django/didgeridoo/webshop/templates/webshop/cart.html b/django/didgeridoo/webshop/templates/webshop/cart.html index 271ad11..8bbcec8 100644 --- a/django/didgeridoo/webshop/templates/webshop/cart.html +++ b/django/didgeridoo/webshop/templates/webshop/cart.html @@ -46,6 +46,9 @@ + + CHECKOUT + {% else %}

diff --git a/django/didgeridoo/webshop/templates/webshop/checkout.html b/django/didgeridoo/webshop/templates/webshop/checkout.html index 932757d..9fa82ef 100644 --- a/django/didgeridoo/webshop/templates/webshop/checkout.html +++ b/django/didgeridoo/webshop/templates/webshop/checkout.html @@ -1,8 +1,24 @@ {% extends "webshop/base.html" %} -{% block section_title %}

Cart

{% endblock %} +{% block section_title %}

CHECKOUT

{% endblock %} {% block content %} -

List of Items in your Shopping Cart:

+

Preview your Purchase:

+

Shipping Address:

+ {% if user_list %} +

Salutation: {{ person.salutation }}

+

Firstname: {{ request.user.first_name }}

+

Lastname: {{ request.user.last_name }}

+

Street: {{ person.street_name }}

+

Streetnumber: {{ person.street_number }}

+

City: {{ person.city }}

+ {% else %} +

+ + Something whent wrong. Your User is incomplete. + +

+ {% endif %} {% if articles_list %} +

Articles:

@@ -39,11 +55,6 @@
POS.
-
- {% csrf_token %} - {{ checkout_form.as_p }} - -
{% else %}

diff --git a/django/didgeridoo/webshop/urls.py b/django/didgeridoo/webshop/urls.py index e59619a..a57b21d 100644 --- a/django/didgeridoo/webshop/urls.py +++ b/django/didgeridoo/webshop/urls.py @@ -20,4 +20,7 @@ urlpatterns = [ url(r'^cart/$', views.cart, name='cart'), + url(r'^checkout/$', + views.checkout, + name='checkout'), ] diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 8ca59b8..261f9e0 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -260,27 +260,130 @@ def cart(request): operation ) - 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: - # todo add to order - order = '' # here we handle the normal cart view: # if cart_id is not existent create a cart: cart_id, created_cart = ShoppingCart.objects.get_or_create(user=user_id) # get all items in the cart of this customer: - articles = CartPosition.objects.filter(cart=cart_id) - if (articles.count()) > 0: + cart_positions = CartPosition.objects.filter(cart=cart_id) + if (cart_positions.count()) > 0: # make a list out of all articles: - cart_position_list = list(articles) + cart_position_list = list(cart_positions) # enumerate the list of articles and loop over items: for idx, cart_position in enumerate(cart_position_list): - # sub funciton of CartPosition: - cart_position.calculate_position_price() + # ************************************************* + # !!! here i don't understand how its intended + # to use the utils function. + # cart_position = process_article_prices(request, cart_position) + # ************************************************* + # scrap out the details to calculate Total of item and Summ of All: + if currency: + # get currencyname to display: + currency_name = ExchangeRate_name.objects.get(id=currency) + # get exchange_rate multiplyed: + cart_position.price_in_chf = rate.exchange( + currency, + cart_position.article.price_in_chf + ) + totalprice_list.append(cart_position.price_in_chf) + 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) + + return render(request, 'webshop/cart.html', + {'cart_position_list_zip': cart_position_list_zip, + 'totalprice_list': totalprice_list, + 'total': total, + 'currencies_form': currencies_form, + 'amount_form': amount_form, + 'article_view': article_view, + 'currency_name': currency_name, + 'category_list': category_list, + 'message': message, + }) + +def checkout(request): + category_list = get_categories() + currencies_form = CurrenciesForm + amount_form = CartForm + rate = ExchangeRate + article_view = True + currency_name = "CHF" + message = "" + cart_position_list = [] + amount_form_list = [] + totalprice_list = [] + total = 0 + user_id = request.user.id + cart_position_list_zip = [] +# here we configure the users Currency: + if 'currency' not in request.session: + request.session['currency'] = None + else: + currency = request.session['currency'] +# Here we handle all POST Operations: + if request.method == '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']: + print('currencies cf:', cf) + selection = cf['currencies'] + request.session['currency'] = selection.id + currency_name = ExchangeRate_name.objects.get( + id=selection.id) + print('currencies currency_name:', currency_name) + else: + request.session['currency'] = None + # here we react to a change of amount per item in the Cart: + if 'amount_form' in request.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') + operation = 'replace' + restrict_cart_to_one_article( + user_id, + article_id, + amount, + operation + ) + # here we react to a change of amount per item in the Cart: + if 'delete' in request.POST: + delete = CartForm(request.POST) + if delete.is_valid(): + amount = delete.cleaned_data['amount_form'] + article_id = request.POST.get('article_id') + amount = 1 + operation = 'delete' + restrict_cart_to_one_article( + user_id, + article_id, + amount, + operation + ) +# here we handle the normal cart view: + # if cart_id is not existent create a cart: + cart_id, created_cart = ShoppingCart.objects.get_or_create(user=user_id) + # get all items in the cart of this customer: + cart_positions = CartPosition.objects.filter(cart=cart_id) + if (cart_positions.count()) > 0: + # make a list out of all articles: + cart_position_list = list(cart_positions) + # enumerate the list of articles and loop over items: + for idx, cart_position in enumerate(cart_position_list): + # ************************************************* + # !!! here i don't understand how its intended + # to use the utils function. + # cart_position = process_article_prices(request, cart_position) + # ************************************************* # scrap out the details to calculate Total of item and Summ of All: if currency: # get currencyname to display: @@ -301,6 +404,7 @@ def cart(request): total = sum(totalprice_list) checkout_form = CheckoutForm() + registration_form = RegistrationForm() return render(request, 'webshop/cart.html', {'cart_position_list_zip': cart_position_list_zip, @@ -308,6 +412,8 @@ def cart(request): 'total': total, 'currencies_form': currencies_form, 'amount_form': amount_form, + 'checkout_form': checkout_form, + 'registration_form': registration_form, 'article_view': article_view, 'currency_name': currency_name, 'category_list': category_list, From f36550e9d468372a7f7e9cea7baa63958d440eee Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 25 Feb 2018 14:20:59 +0100 Subject: [PATCH 05/17] add a user object to the checkout view --- django/didgeridoo/webshop/templates/webshop/checkout.html | 2 +- django/didgeridoo/webshop/views.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/django/didgeridoo/webshop/templates/webshop/checkout.html b/django/didgeridoo/webshop/templates/webshop/checkout.html index 9fa82ef..0802077 100644 --- a/django/didgeridoo/webshop/templates/webshop/checkout.html +++ b/django/didgeridoo/webshop/templates/webshop/checkout.html @@ -3,7 +3,7 @@ {% block content %}

Preview your Purchase:

Shipping Address:

- {% if user_list %} + {% if person %}

Salutation: {{ person.salutation }}

Firstname: {{ request.user.first_name }}

Lastname: {{ request.user.last_name }}

diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 261f9e0..ad946bd 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -405,6 +405,7 @@ def checkout(request): checkout_form = CheckoutForm() registration_form = RegistrationForm() + person = Person.objects.get(user=request.user.id) return render(request, 'webshop/cart.html', {'cart_position_list_zip': cart_position_list_zip, @@ -418,4 +419,5 @@ def checkout(request): 'currency_name': currency_name, 'category_list': category_list, 'message': message, + 'person': person }) From 597dd70ba7e420c94c2ec803fe95e2f4bca3d5ca Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 25 Feb 2018 14:21:32 +0100 Subject: [PATCH 06/17] change the template in the checkout view --- 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 ad946bd..0620ee8 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -407,7 +407,7 @@ def checkout(request): registration_form = RegistrationForm() person = Person.objects.get(user=request.user.id) - return render(request, 'webshop/cart.html', + return render(request, 'webshop/checkout.html', {'cart_position_list_zip': cart_position_list_zip, 'totalprice_list': totalprice_list, 'total': total, From 6e54bdec0dbba570187460d524a120482ee8d546 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 25 Feb 2018 14:22:22 +0100 Subject: [PATCH 07/17] fix comment indents --- django/didgeridoo/webshop/views.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 0620ee8..9eae777 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -260,7 +260,7 @@ def cart(request): operation ) -# here we handle the normal cart view: + # here we handle the normal cart view: # if cart_id is not existent create a cart: cart_id, created_cart = ShoppingCart.objects.get_or_create(user=user_id) # get all items in the cart of this customer: @@ -320,12 +320,12 @@ def checkout(request): total = 0 user_id = request.user.id cart_position_list_zip = [] -# here we configure the users Currency: + # here we configure the users Currency: if 'currency' not in request.session: request.session['currency'] = None else: currency = request.session['currency'] -# Here we handle all POST Operations: + # Here we handle all POST Operations: if request.method == 'POST': # here we react to a currency dropdown change: if 'currencies' in request.POST: @@ -369,7 +369,7 @@ def checkout(request): amount, operation ) -# here we handle the normal cart view: + # here we handle the normal cart view: # if cart_id is not existent create a cart: cart_id, created_cart = ShoppingCart.objects.get_or_create(user=user_id) # get all items in the cart of this customer: From 0d943106a009e302c75dc530bc4da15d01ec0a07 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 25 Feb 2018 14:22:50 +0100 Subject: [PATCH 08/17] correct queries which use the user object --- django/didgeridoo/webshop/views.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 9eae777..7ce3dc4 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -206,7 +206,6 @@ def cart(request): amount_form_list = [] totalprice_list = [] total = 0 - user_id = request.user.id cart_position_list_zip = [] # here we configure the users Currency: @@ -240,7 +239,7 @@ def cart(request): article_id = request.POST.get('article_id') operation = 'replace' restrict_cart_to_one_article( - user_id, + request.user.id, article_id, amount, operation @@ -254,7 +253,7 @@ def cart(request): amount = 1 operation = 'delete' restrict_cart_to_one_article( - user_id, + request.user.id, article_id, amount, operation @@ -262,7 +261,7 @@ def cart(request): # here we handle the normal cart view: # if cart_id is not existent create a cart: - cart_id, created_cart = ShoppingCart.objects.get_or_create(user=user_id) + cart_id, created_cart = ShoppingCart.objects.get_or_create(user=request.user) # get all items in the cart of this customer: cart_positions = CartPosition.objects.filter(cart=cart_id) if (cart_positions.count()) > 0: @@ -318,7 +317,6 @@ def checkout(request): amount_form_list = [] totalprice_list = [] total = 0 - user_id = request.user.id cart_position_list_zip = [] # here we configure the users Currency: if 'currency' not in request.session: @@ -350,7 +348,7 @@ def checkout(request): article_id = request.POST.get('article_id') operation = 'replace' restrict_cart_to_one_article( - user_id, + request.user.id, article_id, amount, operation @@ -364,14 +362,14 @@ def checkout(request): amount = 1 operation = 'delete' restrict_cart_to_one_article( - user_id, + request.user.id, article_id, amount, operation ) # here we handle the normal cart view: # if cart_id is not existent create a cart: - cart_id, created_cart = ShoppingCart.objects.get_or_create(user=user_id) + cart_id, created_cart = ShoppingCart.objects.get_or_create(user=request.user) # get all items in the cart of this customer: cart_positions = CartPosition.objects.filter(cart=cart_id) if (cart_positions.count()) > 0: From 7da09d736cdee51163c22231d1aa1fb91cf3b390 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 25 Feb 2018 14:23:18 +0100 Subject: [PATCH 09/17] add a line to comply with PEP8 --- django/didgeridoo/webshop/views.py | 1 + 1 file changed, 1 insertion(+) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 7ce3dc4..5b45814 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -305,6 +305,7 @@ def cart(request): 'message': message, }) + def checkout(request): category_list = get_categories() currencies_form = CurrenciesForm From 1c52e938c3535b11ddd9bb77f3c0aacc4e7fdd97 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 25 Feb 2018 17:26:24 +0100 Subject: [PATCH 10/17] change the checkout url --- django/didgeridoo/webshop/urls.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django/didgeridoo/webshop/urls.py b/django/didgeridoo/webshop/urls.py index a57b21d..090420d 100644 --- a/django/didgeridoo/webshop/urls.py +++ b/django/didgeridoo/webshop/urls.py @@ -20,7 +20,7 @@ urlpatterns = [ url(r'^cart/$', views.cart, name='cart'), - url(r'^checkout/$', + url(r'^cart/checkout/$', views.checkout, name='checkout'), ] From 81f0fc0c4b5262b2988a2f461c6458bc4f0e105d Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 25 Feb 2018 17:26:43 +0100 Subject: [PATCH 11/17] fix the currency calculation in the cart --- django/didgeridoo/webshop/views.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 5b45814..b9575fd 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -269,21 +269,21 @@ def cart(request): cart_position_list = list(cart_positions) # enumerate the list of articles and loop over items: for idx, cart_position in enumerate(cart_position_list): - # ************************************************* - # !!! here i don't understand how its intended - # to use the utils function. - # cart_position = process_article_prices(request, cart_position) - # ************************************************* # scrap out the details to calculate Total of item and Summ of All: if currency: # get currencyname to display: currency_name = ExchangeRate_name.objects.get(id=currency) # get exchange_rate multiplyed: - cart_position.price_in_chf = rate.exchange( + cart_position.article.price_in_chf = rate.exchange( currency, cart_position.article.price_in_chf ) - totalprice_list.append(cart_position.price_in_chf) + cart_position.position_price = rate.exchange( + currency, + cart_position.position_price + ) + cart_position.calculate_position_price() + totalprice_list.append(cart_position.position_price) amount_form = CartForm( initial={'amount_form': cart_position.amount} ) From e4a58c48fdee02ac2fe9a4e7b263f3cad0147959 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 25 Feb 2018 17:30:20 +0100 Subject: [PATCH 12/17] shorten a line to under 80 characters --- django/didgeridoo/webshop/views.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index b9575fd..cd9fcda 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -261,7 +261,9 @@ def cart(request): # here we handle the normal cart view: # if cart_id is not existent create a cart: - cart_id, created_cart = ShoppingCart.objects.get_or_create(user=request.user) + cart_id, created_cart = ShoppingCart.objects.get_or_create( + user=request.user + ) # get all items in the cart of this customer: cart_positions = CartPosition.objects.filter(cart=cart_id) if (cart_positions.count()) > 0: From 2c52c2483ae16f399c925abaca53ba5e90d236d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Sun, 25 Feb 2018 19:26:26 +0100 Subject: [PATCH 13/17] improved the table styleing --- .../webshop/templates/webshop/cart.html | 19 ++++++++++++------- django/didgeridoo/webshop/views.py | 6 +----- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/django/didgeridoo/webshop/templates/webshop/cart.html b/django/didgeridoo/webshop/templates/webshop/cart.html index 8bbcec8..0de5242 100644 --- a/django/didgeridoo/webshop/templates/webshop/cart.html +++ b/django/didgeridoo/webshop/templates/webshop/cart.html @@ -3,15 +3,15 @@ {% block content %}

List of Items in your Shopping Cart:

{% if cart_position_list_zip %} - +
- - + + {% for cart_position, amount_form in cart_position_list_zip %} @@ -33,16 +33,21 @@ - - + {% endfor %} - +
POS. ART# NAME STOCK AMOUNTPRICE p.pce.POSITION PRICEPRICE p.pce.POSITION PRICE
+ {{ cart_position.article.price_in_chf }} {{ currency_name }} {{ cart_position.position_price }} {{ currency_name }} + {{ cart_position.position_price }} {{ currency_name }} +
- Total: {{ total }} {{ currency_name }} + + +
Total:
+
{{ total }} {{ currency_name }}
diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index cd9fcda..098e7b2 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -286,6 +286,7 @@ def cart(request): ) cart_position.calculate_position_price() totalprice_list.append(cart_position.position_price) + print('totalprice_list', totalprice_list) amount_form = CartForm( initial={'amount_form': cart_position.amount} ) @@ -380,11 +381,6 @@ def checkout(request): cart_position_list = list(cart_positions) # enumerate the list of articles and loop over items: for idx, cart_position in enumerate(cart_position_list): - # ************************************************* - # !!! here i don't understand how its intended - # to use the utils function. - # cart_position = process_article_prices(request, cart_position) - # ************************************************* # scrap out the details to calculate Total of item and Summ of All: if currency: # get currencyname to display: From c5b118c7c268fe9e3f69f33daf5671ebf9b4e804 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 25 Feb 2018 19:57:50 +0100 Subject: [PATCH 14/17] correct the cart creation --- django/didgeridoo/webshop/views.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index cd9fcda..d8b2d71 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -98,7 +98,7 @@ def article_details(request, article_id): rate = ExchangeRate article_view = True currency_name = "CHF" - user_id = request.user.id + user = request.user if 'currency' not in request.session: request.session['currency'] = None @@ -127,7 +127,7 @@ def article_details(request, article_id): amount = amount.cleaned_data['amount'] operation = 'add' restrict_cart_to_one_article( - user_id, + user, article_id, amount, operation From efa92ea07485baf231a429c097f7572998bccc55 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 25 Feb 2018 19:58:30 +0100 Subject: [PATCH 15/17] correct the currency calculation --- django/didgeridoo/webshop/views.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index d8b2d71..e505767 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -284,11 +284,12 @@ def cart(request): currency, cart_position.position_price ) - cart_position.calculate_position_price() - totalprice_list.append(cart_position.position_price) + amount_form = CartForm( initial={'amount_form': cart_position.amount} ) + cart_position.calculate_position_price() + totalprice_list.append(cart_position.position_price) amount_form_list.append(amount_form) cart_position_list[idx] = cart_position cart_position_list_zip = zip(cart_position_list, amount_form_list) From 798cee4c5865c4792db5e2ea7f314b059da720df Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 25 Feb 2018 21:33:34 +0100 Subject: [PATCH 16/17] add some empty lines for better readability --- django/didgeridoo/webshop/views.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 58d1870..0bf5c80 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -231,6 +231,7 @@ def cart(request): print('currencies currency_name:', currency_name) else: request.session['currency'] = None + # here we react to a change of amount per item in the Cart: if 'amount_form' in request.POST: amount_form = CartForm(request.POST) @@ -244,6 +245,7 @@ def cart(request): amount, operation ) + # here we react to a change of amount per item in the Cart: if 'delete' in request.POST: delete = CartForm(request.POST) From 66a066165db44c9d43a632a38302e59b1cff6fb9 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 25 Feb 2018 21:33:53 +0100 Subject: [PATCH 17/17] fix the currency calculation in the cart --- django/didgeridoo/webshop/views.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 0bf5c80..cd59fb5 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -274,7 +274,8 @@ def cart(request): # enumerate the list of articles and loop over items: for idx, cart_position in enumerate(cart_position_list): # scrap out the details to calculate Total of item and Summ of All: - if currency: + if request.session['currency']: + currency = request.session['currency'] # get currencyname to display: currency_name = ExchangeRate_name.objects.get(id=currency) # get exchange_rate multiplyed: