Merge branch 'cart' of git.2li.ch:ibz/web_AI-5 into cart
This commit is contained in:
commit
50c0bd847b
|
@ -24,8 +24,8 @@
|
||||||
</td>
|
</td>
|
||||||
<td scope="col">{{ cart_position.article.stock }}</td>
|
<td scope="col">{{ cart_position.article.stock }}</td>
|
||||||
<td scope="col">
|
<td scope="col">
|
||||||
<form id="amountfield" action="" method="POST" novalidate>
|
<form id="amount_form" action="" method="POST" novalidate>
|
||||||
{{ cart_form }}
|
{{ amount_form.as_p }}
|
||||||
<input type="submit" value="change" />
|
<input type="submit" value="change" />
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
</form>
|
</form>
|
||||||
|
|
|
@ -47,8 +47,7 @@
|
||||||
{% else %}
|
{% else %}
|
||||||
<p class="alert alert-danger">
|
<p class="alert alert-danger">
|
||||||
<strong>
|
<strong>
|
||||||
This cart seams to lack some Items.
|
Something whent wrong. Your cart is empty.
|
||||||
Go get some in the store!
|
|
||||||
<strong>
|
<strong>
|
||||||
</p>
|
</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
|
@ -198,16 +198,55 @@ def cart(request):
|
||||||
article_view = True
|
article_view = True
|
||||||
currency_name = "CHF"
|
currency_name = "CHF"
|
||||||
message = ""
|
message = ""
|
||||||
articles_list = ""
|
cart_position_list = ""
|
||||||
prices_in_cart = []
|
prices_in_cart = []
|
||||||
totalprice_list = []
|
totalprice_list = []
|
||||||
total = 0
|
total = 0
|
||||||
|
|
||||||
|
# here we configure the users Currency:
|
||||||
if not 'currency' in request.session:
|
if not 'currency' in request.session:
|
||||||
request.session['currency'] = None
|
request.session['currency'] = None
|
||||||
else:
|
else:
|
||||||
currency = request.session['currency']
|
currency = request.session['currency']
|
||||||
|
|
||||||
|
# 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_id', cart_id)
|
||||||
|
articles = CartPosition.objects.filter(cart=cart_id)
|
||||||
|
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(
|
||||||
|
cart=cart_id,
|
||||||
|
article=cart_position.article.id
|
||||||
|
)
|
||||||
|
cart_position.calculate_position_price()
|
||||||
|
if currency:
|
||||||
|
print('calc 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.price_in_chf
|
||||||
|
)
|
||||||
|
totalprice_list.append(cart_position.price_in_chf)
|
||||||
|
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':
|
if request.method == 'POST':
|
||||||
print(request.POST)
|
print(request.POST)
|
||||||
# here we react to a currency dropdown change:
|
# here we react to a currency dropdown change:
|
||||||
|
@ -224,11 +263,13 @@ def cart(request):
|
||||||
else:
|
else:
|
||||||
request.session['currency'] = None
|
request.session['currency'] = None
|
||||||
# here we react to a change of amount per item in the Cart:
|
# here we react to a change of amount per item in the Cart:
|
||||||
if 'amountfield' in request.POST:
|
if 'amount_field' in request.POST:
|
||||||
print('yes amount post')
|
print('yes amount post')
|
||||||
|
amount_form = CartForm(request.POST,
|
||||||
if amount.is_valid():
|
cart_id,
|
||||||
amount = amount.cleaned_data['amount']
|
cart_position.article.id)
|
||||||
|
if amount_form.is_valid():
|
||||||
|
amount = amount_form.cleaned_data['amount']
|
||||||
article = Article.objects.get(id=article_id)
|
article = Article.objects.get(id=article_id)
|
||||||
try:
|
try:
|
||||||
cart_id = ShoppingCart.objects.get(user=request.user)
|
cart_id = ShoppingCart.objects.get(user=request.user)
|
||||||
|
@ -255,35 +296,7 @@ def cart(request):
|
||||||
order = ''
|
order = ''
|
||||||
|
|
||||||
checkout_form = CheckoutForm()
|
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.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)
|
|
||||||
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):
|
|
||||||
cart_position.calculate_position_price()
|
|
||||||
if currency:
|
|
||||||
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:
|
|
||||||
cart_position.article.price_in_chf = rate.exchange(
|
|
||||||
currency,
|
|
||||||
cart_position.article.price_in_chf)
|
|
||||||
amount = cart_form.change_amount(request.POST,
|
|
||||||
cart_position.article)
|
|
||||||
totalprice_list.append(cart_position.position_price)
|
|
||||||
cart_position_list[idx] = cart_position
|
|
||||||
|
|
||||||
total = sum(totalprice_list)
|
|
||||||
|
|
||||||
return render(request, 'webshop/cart.html',
|
return render(request, 'webshop/cart.html',
|
||||||
{'cart_position_list': cart_position_list,
|
{'cart_position_list': cart_position_list,
|
||||||
|
@ -291,7 +304,7 @@ def cart(request):
|
||||||
'total': total,
|
'total': total,
|
||||||
'cart_form': cart_form,
|
'cart_form': cart_form,
|
||||||
'currencies_form': currencies_form,
|
'currencies_form': currencies_form,
|
||||||
'checkout_form': checkout_form,
|
'amount_form': amount_form,
|
||||||
'article_view': article_view,
|
'article_view': article_view,
|
||||||
'currency_name': currency_name,
|
'currency_name': currency_name,
|
||||||
'category_list': category_list,
|
'category_list': category_list,
|
||||||
|
|
Loading…
Reference in New Issue