diff --git a/django/didgeridoo/webshop/models.py b/django/didgeridoo/webshop/models.py index 111f346..33c9106 100644 --- a/django/didgeridoo/webshop/models.py +++ b/django/didgeridoo/webshop/models.py @@ -86,6 +86,9 @@ class Order(models.Model): date = models.DateTimeField(default=timezone.now) exchange_rate = models.ForeignKey(ExchangeRate) + def __str__(self): + return str(self.id) + class OrderPosition(models.Model): """ Items in Submitted Orders""" diff --git a/django/didgeridoo/webshop/templates/webshop/checkout.html b/django/didgeridoo/webshop/templates/webshop/checkout.html index 0802077..a680d0d 100644 --- a/django/didgeridoo/webshop/templates/webshop/checkout.html +++ b/django/didgeridoo/webshop/templates/webshop/checkout.html @@ -2,6 +2,7 @@ {% block section_title %}

CHECKOUT

{% endblock %} {% block content %}

Preview your Purchase:

+

Shipping Address:

{% if person %}

Salutation: {{ person.salutation }}

@@ -17,48 +18,58 @@

{% endif %} - {% if articles_list %} -

Articles:

- + {% if cart_position_list %} +
+

Your Items:

+
- - + + - {% for article in articles_list %} + {% for cart_position in cart_position_list %} - - + + - - - + + - + {% endfor %} - +
POS. ART# NAME STOCK AMOUNTPRICE p.pce.POSITION PRICEPRICE 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 }} - {{ article.amount }} - - {{ article.article.price_in_chf }} + {{ cart_position.article.stock }}{{ cart_position.article.amount }} + {{ cart_position.article.price_in_chf }} {{ currency_name }} {{ article.position_price }} {{ currency_name }} + {{ cart_position.position_price }} {{ currency_name }} +
- Total: {{ total }} {{ currency_name }} + + +
Total:
+
{{ total }} {{ currency_name }}
+
+ {% csrf_token %} + {{ checkout_form.as_p }} + +
{% else %}

- Something whent wrong. Your cart is empty. + Your cart seamed to lack Items. + Go get some in the store!

{% endif %} diff --git a/django/didgeridoo/webshop/templates/webshop/nav.html b/django/didgeridoo/webshop/templates/webshop/nav.html index f65151f..d43e0e3 100644 --- a/django/didgeridoo/webshop/templates/webshop/nav.html +++ b/django/didgeridoo/webshop/templates/webshop/nav.html @@ -13,6 +13,9 @@ {% else %}
  • LOGIN
  • {% endif %} + {% if checkout %} + + {% else %} diff --git a/django/didgeridoo/webshop/templates/webshop/orders.html b/django/didgeridoo/webshop/templates/webshop/orders.html new file mode 100644 index 0000000..220d4a5 --- /dev/null +++ b/django/didgeridoo/webshop/templates/webshop/orders.html @@ -0,0 +1,8 @@ +{% extends "webshop/base.html" %} +{% load customfilters %} +{% block section_title %}Order{% endblock %} + +{% block content %} +

    Your order was submitted.

    +

    Thank you for Purchase.

    +{% endblock %} diff --git a/django/didgeridoo/webshop/urls.py b/django/didgeridoo/webshop/urls.py index 090420d..1b041bd 100644 --- a/django/didgeridoo/webshop/urls.py +++ b/django/didgeridoo/webshop/urls.py @@ -23,4 +23,7 @@ urlpatterns = [ url(r'^cart/checkout/$', views.checkout, name='checkout'), + url(r'^orders$', + views.orders, + name='orders'), ] diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 58d1870..a7e7fd9 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -10,7 +10,8 @@ from webshop.models import (Article, City, Picture, CartPosition, - ShoppingCart) + ShoppingCart, + Order) from webshop.forms import (RegistrationForm, AddToCartForm, CartForm, @@ -83,7 +84,7 @@ def restrict_cart_to_one_article(user_id, article_id, amount, operation): if operation == 'add': new_amount = cart_position.amount + amount if operation == 'replace': - new_amount = amount # ref two times check later !! + new_amount = amount # if article is in cart already update amount: cart_position = CartPosition.objects.filter( article=article_id).update( @@ -280,11 +281,6 @@ def cart(request): currency, cart_position.article.price_in_chf ) - cart_position.position_price = rate.exchange( - currency, - cart_position.position_price - ) - amount_form = CartForm( initial={'amount_form': cart_position.amount} ) @@ -315,106 +311,88 @@ def checkout(request): amount_form = CartForm rate = ExchangeRate article_view = True - currency_name = "CHF" message = "" cart_position_list = [] - amount_form_list = [] totalprice_list = [] total = 0 - cart_position_list_zip = [] - # here we configure the users Currency: + + checkout_form = CheckoutForm() if 'currency' not in request.session: request.session['currency'] = None else: currency = request.session['currency'] + exchange_rate = ExchangeRate.objects.filter(name=currency).latest('date') # 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 + print('checkout post') # 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( - request.user.id, - article_id, - amount, - operation + if 'checkout_form' in request.POST: + print('checkout post request.POST = checkout_form') + checkout_form = CartForm(request.POST) + if checkout_form.is_valid(): + print('checkout post valid') + order, created_order = Order.objects.get_or_create( + user=request.user, + defaults={'status': 1, + 'exchange_rate': exchange_rate.id, + } ) - # 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( - 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=request.user) - # 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): - # 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) + print('order', order, 'created:', created_order) + if created_order is False: + message = """something whent wrong. + Seams like this cart was already submitted. How come? """ + # order status variables: + # • ordered -> vom Kunden bestellt + #  • delivered -> Bestellung wurde versandt + # • cancelled -> Bestellung storniert + # • on hold -> Bestellung pausiert + cart_id, created_cart = ShoppingCart.objects.get_or_create( + user=request.user) + if created_cart is False: + # 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): + if currency: + # get currencyname to display: + currency_name = ExchangeRate_name.objects.get(id=currency) + # get exchange_rate multiplyed: + cart_position.article.price_in_chf = rate.exchange( + currency, + cart_position.article.price_in_chf + ) + cart_position.calculate_position_price() + totalprice_list.append(cart_position.position_price) + cart_position_list[idx] = cart_position + else: + message = """something whent wrong. + Seams like your cart was + not existent before. How come? """ total = sum(totalprice_list) - - checkout_form = CheckoutForm() - registration_form = RegistrationForm() person = Person.objects.get(user=request.user.id) return render(request, 'webshop/checkout.html', - {'cart_position_list_zip': cart_position_list_zip, + {'cart_position_list': cart_position_list, 'totalprice_list': totalprice_list, '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, + 'article_view': article_view, 'category_list': category_list, 'message': message, 'person': person }) + + +def orders(request): + return render(request, 'webshop/orders.html', + { + + })