Merge branch 'cart' of git.2li.ch:ibz/web_AI-5 into cart
This commit is contained in:
commit
203d907508
|
@ -67,20 +67,10 @@ class AddToCartForm(forms.Form):
|
||||||
|
|
||||||
|
|
||||||
class CartForm(forms.Form):
|
class CartForm(forms.Form):
|
||||||
amount_field = forms.IntegerField(
|
amount_form = forms.FloatField(
|
||||||
label='pce',
|
label='pce',
|
||||||
help_text='Enter a Value between 1 and 99.')
|
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):
|
class CheckoutForm(forms.Form):
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
<th scope="col">PRICE p.pce.</th>
|
<th scope="col">PRICE p.pce.</th>
|
||||||
<th scope="col">POSITION PRICE</th>
|
<th scope="col">POSITION PRICE</th>
|
||||||
</tr>
|
</tr>
|
||||||
{% for cart_position in cart_position_list %}
|
{% for cart_position, amount_form in cart_position_list_zip %}
|
||||||
<tr class="table_content">
|
<tr class="table_content">
|
||||||
<td scope="col">{{ cart_position.id }}</td>
|
<td scope="col">{{ cart_position.id }}</td>
|
||||||
<td scope="col">{{ cart_position.article.id }}</td>
|
<td scope="col">{{ cart_position.article.id }}</td>
|
||||||
|
@ -26,6 +26,7 @@
|
||||||
<td scope="col">
|
<td scope="col">
|
||||||
<form id="amount_form" action="" method="POST" novalidate>
|
<form id="amount_form" action="" method="POST" novalidate>
|
||||||
{{ amount_form.as_p }}
|
{{ amount_form.as_p }}
|
||||||
|
<input type="hidden" value="{{ cart_position.article.id }}" name="article_id">
|
||||||
<input type="submit" value="change" />
|
<input type="submit" value="change" />
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
</form>
|
</form>
|
||||||
|
|
|
@ -64,6 +64,46 @@ def articles_in_category(request, category_id):
|
||||||
'category': selected_category})
|
'category': selected_category})
|
||||||
|
|
||||||
|
|
||||||
|
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:
|
||||||
|
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:
|
||||||
|
try:
|
||||||
|
article_amount = CartPosition.objects.get(
|
||||||
|
article=article_id)
|
||||||
|
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':
|
||||||
|
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(
|
||||||
|
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)
|
||||||
|
)
|
||||||
|
cart_position.save()
|
||||||
|
|
||||||
|
|
||||||
def article_details(request, article_id):
|
def article_details(request, article_id):
|
||||||
category_list = get_categories()
|
category_list = get_categories()
|
||||||
currencies_form = CurrenciesForm
|
currencies_form = CurrenciesForm
|
||||||
|
@ -97,34 +137,14 @@ def article_details(request, article_id):
|
||||||
amount = AddToCartForm(request.POST)
|
amount = AddToCartForm(request.POST)
|
||||||
if amount.is_valid():
|
if amount.is_valid():
|
||||||
amount = amount.cleaned_data['amount']
|
amount = amount.cleaned_data['amount']
|
||||||
article = Article.objects.get(id=article_id)
|
user_name = request.user
|
||||||
try:
|
operation = 'add'
|
||||||
# lookup if cart_id is already existent:
|
restrict_cart_to_one_article(
|
||||||
cart_id = ShoppingCart.objects.get(user=request.user)
|
user_name,
|
||||||
except:
|
article_id,
|
||||||
# if cart_id is not existent create a cart:
|
amount,
|
||||||
cart_id = ShoppingCart.objects.create(user=request.user)
|
operation
|
||||||
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()
|
|
||||||
# write default value (1) to form field:
|
# write default value (1) to form field:
|
||||||
amount = AddToCartForm()
|
amount = AddToCartForm()
|
||||||
else:
|
else:
|
||||||
|
@ -195,54 +215,17 @@ def cart(request):
|
||||||
article_view = True
|
article_view = True
|
||||||
currency_name = "CHF"
|
currency_name = "CHF"
|
||||||
message = ""
|
message = ""
|
||||||
cart_position_list = ""
|
cart_position_list = []
|
||||||
prices_in_cart = []
|
amount_form_list = []
|
||||||
totalprice_list = []
|
totalprice_list = []
|
||||||
total = 0
|
total = 0
|
||||||
|
user_name = request.user
|
||||||
# here we configure the users Currency:
|
# here we configure the users Currency:
|
||||||
if 'currency' not in request.session:
|
if 'currency' not 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:
|
# Here we handle all POST Operations:
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
print(request.POST)
|
print(request.POST)
|
||||||
|
@ -260,27 +243,19 @@ 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 'amount_field' in request.POST:
|
if 'amount_form' in request.POST:
|
||||||
print('yes amount post')
|
print('amount_form yes amount post')
|
||||||
amount_form = CartForm(request.POST,
|
amount_form = CartForm(request.POST)
|
||||||
cart_id,
|
|
||||||
cart_position.article.id)
|
|
||||||
if amount_form.is_valid():
|
if amount_form.is_valid():
|
||||||
amount = amount_form.cleaned_data['amount']
|
amount = amount_form.cleaned_data['amount_form']
|
||||||
article = Article.objects.get(id=article_id)
|
article_id = request.POST.get('article_id')
|
||||||
try:
|
operation = 'replace'
|
||||||
cart_id = ShoppingCart.objects.get(user=request.user)
|
restrict_cart_to_one_article(
|
||||||
except:
|
user_name,
|
||||||
cart_id = ShoppingCart.objects.create(user=request.user)
|
article_id,
|
||||||
cart_id.save()
|
amount,
|
||||||
if cart_id:
|
operation
|
||||||
cart_position = CartPosition.objects.create(
|
)
|
||||||
article=article,
|
|
||||||
amount=amount,
|
|
||||||
cart=ShoppingCart.objects.get(user=request.user)
|
|
||||||
)
|
|
||||||
cart_position.save()
|
|
||||||
amount = CartForm.ChangeAmount()
|
|
||||||
|
|
||||||
if 'checkout' in request.POST:
|
if 'checkout' in request.POST:
|
||||||
print('checkout')
|
print('checkout')
|
||||||
|
@ -289,16 +264,55 @@ def cart(request):
|
||||||
checkout_form = checkout_form.cleaned_data['checkout']
|
checkout_form = checkout_form.cleaned_data['checkout']
|
||||||
print('views checkout checkout_form', checkout_form)
|
print('views checkout checkout_form', checkout_form)
|
||||||
if checkout_form is True:
|
if checkout_form is True:
|
||||||
# add to order
|
# todo add to order
|
||||||
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)
|
||||||
|
# make a list out of all articles:
|
||||||
|
cart_position_list = list(articles)
|
||||||
|
# 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()
|
||||||
|
# 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:
|
||||||
|
cart_position.price_in_chf = rate.exchange(
|
||||||
|
currency,
|
||||||
|
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}
|
||||||
|
)
|
||||||
|
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()
|
checkout_form = CheckoutForm()
|
||||||
|
|
||||||
return render(request, 'webshop/cart.html',
|
return render(request, 'webshop/cart.html',
|
||||||
{'cart_position_list': cart_position_list,
|
{'cart_position_list_zip': cart_position_list_zip,
|
||||||
'totalprice_list': totalprice_list,
|
'totalprice_list': totalprice_list,
|
||||||
'total': total,
|
'total': total,
|
||||||
'cart_form': cart_form,
|
|
||||||
'currencies_form': currencies_form,
|
'currencies_form': currencies_form,
|
||||||
'amount_form': amount_form,
|
'amount_form': amount_form,
|
||||||
'article_view': article_view,
|
'article_view': article_view,
|
||||||
|
|
Loading…
Reference in New Issue