From cb19ca62056b243a7fcfeac0451c6ec4a23b23ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ho=CC=88rler?= Date: Sun, 18 Feb 2018 21:58:46 +0100 Subject: [PATCH] add amountform not working yet --- django/didgeridoo/webshop/forms.py | 4 +- django/didgeridoo/webshop/models.py | 2 +- .../webshop/templates/webshop/cart.html | 23 +++---- .../webshop/templates/webshop/checkout.html | 60 +++++++++++++++++++ django/didgeridoo/webshop/views.py | 35 +++++------ 5 files changed, 90 insertions(+), 34 deletions(-) create mode 100644 django/didgeridoo/webshop/templates/webshop/checkout.html diff --git a/django/didgeridoo/webshop/forms.py b/django/didgeridoo/webshop/forms.py index 46145a7..9c31a3b 100644 --- a/django/didgeridoo/webshop/forms.py +++ b/django/didgeridoo/webshop/forms.py @@ -69,9 +69,9 @@ class AddToCartForm(forms.Form): class CartForm(forms.Form): print('CartForm') - def ChangeAmount(self, _article_id): + def ChangeAmount(self, _cart_id, _article_id): print('CartForm.ChangeAmount') - article = CartPosition.objects.filter(pk=_article_id) + article = CartPosition.objects.get(cart=_cart_id, article=_article_id) amountfield = forms.IntegerField( label='pce', help_text='Enter a Value between 1 and 99.', diff --git a/django/didgeridoo/webshop/models.py b/django/didgeridoo/webshop/models.py index 9551f84..111f346 100644 --- a/django/didgeridoo/webshop/models.py +++ b/django/didgeridoo/webshop/models.py @@ -104,7 +104,7 @@ class ShoppingCart(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) def __str__(self): - return self.name + return str(self.id) class CartPosition(models.Model): diff --git a/django/didgeridoo/webshop/templates/webshop/cart.html b/django/didgeridoo/webshop/templates/webshop/cart.html index 83c69e0..334f5a6 100644 --- a/django/didgeridoo/webshop/templates/webshop/cart.html +++ b/django/didgeridoo/webshop/templates/webshop/cart.html @@ -2,7 +2,7 @@ {% block section_title %}

Cart

{% endblock %} {% block content %}

List of Items in your Shopping Cart:

- {% if articles_list %} + {% if cart_position_list %} @@ -13,16 +13,16 @@ - {% for article in articles_list %} + {% for cart_position in cart_position_list %} - - + + - + - + {% endfor %} @@ -44,11 +44,6 @@
POS.PRICE p.pce. POSITION PRICE
{{ article.id }}{{ article.article.id }}{{ cart_position.id }}{{ cart_position.article.id }} - - {{ article.article.name }} + + {{ cart_position.article.name }} {{ article.article.stock }}{{ cart_position.article.stock }}
{{ CartForm }} @@ -32,10 +32,10 @@
- {{ article.article.price_in_chf }} + {{ cart_position.article.price_in_chf }} {{ currency_name }} {{ article.position_price }} {{ currency_name }}{{ cart_position.position_price }} {{ currency_name }}
- - {% csrf_token %} - {{ checkout_form.as_p }} - - {% else %}

diff --git a/django/didgeridoo/webshop/templates/webshop/checkout.html b/django/didgeridoo/webshop/templates/webshop/checkout.html new file mode 100644 index 0000000..e81dae1 --- /dev/null +++ b/django/didgeridoo/webshop/templates/webshop/checkout.html @@ -0,0 +1,60 @@ +{% extends "webshop/base.html" %} +{% block section_title %}

Cart

{% endblock %} +{% block content %} +

List of Items in your Shopping Cart:

+ {% if articles_list %} + + + + + + + + + + + {% for article in articles_list %} + + + + + + + + + + {% endfor %} + + + +
POS.ART#NAMESTOCKAMOUNTPRICE p.pce.POSITION PRICE
{{ article.id }}{{ article.article.id }} + + {{ article.article.name }} + + {{ article.article.stock }} + {{ article.amount }} + + {{ article.article.price_in_chf }} + {{ currency_name }} + {{ article.position_price }} {{ currency_name }}
+ Total: {{ total }} {{ currency_name }} +
+
+ {% csrf_token %} + {{ checkout_form.as_p }} + +
+ {% else %} +

+ + This cart seams to lack some Items. + Go get some in the store! + +

+ {% endif %} +

+ + {{ message }} + +

+{% endblock %} diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 9607e87..87becee 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -252,40 +252,41 @@ def cart(request): if checkout_form is True: # add to order order = '' - else: - message = 'Plese accept our General Terms and Conditions!' - print('else') checkout_form = CheckoutForm() # if the cart_id is set the user has already added items to cart. try: - cart_id = ShoppingCart.objects.get(user=request.user) + 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_id', cart_id) articles = CartPosition.objects.filter(cart=cart_id) - articles_list = list(articles) + cart_position_list = list(articles) # scrap out the details to calculate Total of item and Summ of All: - for idx, article in enumerate(articles_list): - article.calculate_position_price() + for idx, cart_position in enumerate(cart_position_list): + cart_position.calculate_position_price() if currency: - article.price_in_chf = rate.exchange( - currency, article.article.price_in_chf) + cart_position.price_in_chf = rate.exchange( + currency, cart_position.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 = CartForm.ChangeAmount(request.POST, article.id) - totalprice_list.append(article.position_price) - articles_list[idx] = article + cart_position.price_in_chf = rate.exchange( + currency, + article.price_in_chf) + amount = CartForm.ChangeAmount(request.POST, + cart_id, + cart_position.article.id) + totalprice_list.append(cart_position.position_price) + cart_position_list[idx] = cart_position total = sum(totalprice_list) return render(request, 'webshop/cart.html', - {'articles_list': articles_list, + {'cart_position_list': cart_position_list, 'totalprice_list': totalprice_list, 'total': total, 'currencies_form': currencies_form,