Merge branch 'cart' into andreas
This commit is contained in:
commit
33239d9e3f
|
@ -3,15 +3,15 @@
|
|||
{% block content %}
|
||||
<h3>List of Items in your Shopping Cart:</h3>
|
||||
{% if cart_position_list_zip %}
|
||||
<table class="table">
|
||||
<table class="table price-table">
|
||||
<tr class="table_header">
|
||||
<th scope="col">POS.</th>
|
||||
<th scope="col">ART#</th>
|
||||
<th scope="col">NAME</th>
|
||||
<th scope="col">STOCK</th>
|
||||
<th scope="col">AMOUNT</th>
|
||||
<th scope="col">PRICE p.pce.</th>
|
||||
<th scope="col">POSITION PRICE</th>
|
||||
<th scope="col" class="price-label">PRICE p.pce.</th>
|
||||
<th scope="col" class="price-label">POSITION PRICE</th>
|
||||
</tr>
|
||||
{% for cart_position, amount_form in cart_position_list_zip %}
|
||||
<tr class="table_content">
|
||||
|
@ -33,19 +33,27 @@
|
|||
</form>
|
||||
<!-- {{ article.amount }} -->
|
||||
</td>
|
||||
<td scope="col">
|
||||
<td scope="col" class="price-value">
|
||||
{{ cart_position.article.price_in_chf }}
|
||||
{{ currency_name }}
|
||||
</td>
|
||||
<td scope="col">{{ cart_position.position_price }} {{ currency_name }}</td>
|
||||
<td scope="col" class="price-value">
|
||||
{{ cart_position.position_price }} {{ currency_name }}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
<tr>
|
||||
<td scope="col" colspan="7" class="text-right">
|
||||
Total: {{ total }} {{ currency_name }}
|
||||
<td scope="col" colspan="5"class="text-right">
|
||||
<td scope="col" class="price-value">
|
||||
<dl><dt>Total:</dl></dt></td>
|
||||
<td scope="col" class="price-value">
|
||||
<dl><dt>{{ total }} {{ currency_name }}</dl></dt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a href="{% url 'checkout' %}" class="btn btn-primary" role="button">
|
||||
CHECKOUT
|
||||
</a>
|
||||
{% else %}
|
||||
<p class="alert alert-danger">
|
||||
<strong>
|
||||
|
|
|
@ -1,8 +1,24 @@
|
|||
{% extends "webshop/base.html" %}
|
||||
{% block section_title %}<h1>Cart</h1>{% endblock %}
|
||||
{% block section_title %}<h1>CHECKOUT</h1>{% endblock %}
|
||||
{% block content %}
|
||||
<h3>List of Items in your Shopping Cart:</h3>
|
||||
<h3>Preview your Purchase:</h3>
|
||||
<h4>Shipping Address:</h4>
|
||||
{% if person %}
|
||||
<p><b>Salutation: </b>{{ person.salutation }}</p>
|
||||
<p><b>Firstname: </b>{{ request.user.first_name }}</p>
|
||||
<p><b>Lastname: </b>{{ request.user.last_name }}</p>
|
||||
<p><b>Street: </b>{{ person.street_name }}</p>
|
||||
<p><b>Streetnumber: </b>{{ person.street_number }}</p>
|
||||
<p><b>City: </b>{{ person.city }}</p>
|
||||
{% else %}
|
||||
<p class="alert alert-danger">
|
||||
<strong>
|
||||
Something whent wrong. Your User is incomplete.
|
||||
<strong>
|
||||
</p>
|
||||
{% endif %}
|
||||
{% if articles_list %}
|
||||
<h4>Articles:</h4>
|
||||
<table class="table">
|
||||
<tr class="table_header">
|
||||
<th scope="col">POS.</th>
|
||||
|
@ -39,11 +55,6 @@
|
|||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<form id="checkout" method="post">
|
||||
{% csrf_token %}
|
||||
{{ checkout_form.as_p }}
|
||||
<input type="submit" value="checkout ->" />
|
||||
</form>
|
||||
{% else %}
|
||||
<p class="alert alert-danger">
|
||||
<strong>
|
||||
|
|
|
@ -20,4 +20,7 @@ urlpatterns = [
|
|||
url(r'^cart/$',
|
||||
views.cart,
|
||||
name='cart'),
|
||||
url(r'^cart/checkout/$',
|
||||
views.checkout,
|
||||
name='checkout'),
|
||||
]
|
||||
|
|
|
@ -64,51 +64,31 @@ 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):
|
||||
# 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)
|
||||
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:
|
||||
# transfair Article to CartPosition:
|
||||
cart_position, created_position = CartPosition.objects.get_or_create(
|
||||
article=article,
|
||||
defaults={'amount': amount,
|
||||
'position_price': article.price_in_chf,
|
||||
'cart': cart_id
|
||||
}
|
||||
)
|
||||
if created_position is 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)
|
||||
cart_position.delete()
|
||||
if (operation == 'add') or (operation == '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
|
||||
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)
|
||||
# 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):
|
||||
|
@ -118,6 +98,7 @@ def article_details(request, article_id):
|
|||
rate = ExchangeRate
|
||||
article_view = True
|
||||
currency_name = "CHF"
|
||||
user = request.user
|
||||
|
||||
if 'currency' not in request.session:
|
||||
request.session['currency'] = None
|
||||
|
@ -144,10 +125,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,
|
||||
article_id,
|
||||
amount,
|
||||
operation
|
||||
|
@ -226,7 +206,6 @@ def cart(request):
|
|||
amount_form_list = []
|
||||
totalprice_list = []
|
||||
total = 0
|
||||
user_name = request.user
|
||||
cart_position_list_zip = []
|
||||
|
||||
# here we configure the users Currency:
|
||||
|
@ -237,7 +216,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')
|
||||
|
@ -245,29 +223,31 @@ 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'
|
||||
restrict_cart_to_one_article(
|
||||
user_name,
|
||||
request.user.id,
|
||||
article_id,
|
||||
amount,
|
||||
operation
|
||||
)
|
||||
|
||||
# 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']
|
||||
|
@ -275,64 +255,50 @@ def cart(request):
|
|||
amount = 1
|
||||
operation = 'delete'
|
||||
restrict_cart_to_one_article(
|
||||
user_name,
|
||||
request.user.id,
|
||||
article_id,
|
||||
amount,
|
||||
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 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)
|
||||
# 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(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()
|
||||
# scrap out the details to calculate Total of item and Summ of All:
|
||||
if currency:
|
||||
print('calc 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:
|
||||
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
|
||||
)
|
||||
|
||||
print('cart cart_position.article.id', cart_position.article.id,
|
||||
'articleamount:', cart_position.amount)
|
||||
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)
|
||||
|
||||
total = sum(totalprice_list)
|
||||
|
||||
checkout_form = CheckoutForm()
|
||||
|
||||
return render(request, 'webshop/cart.html',
|
||||
{'cart_position_list_zip': cart_position_list_zip,
|
||||
'totalprice_list': totalprice_list,
|
||||
|
@ -344,3 +310,114 @@ def cart(request):
|
|||
'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
|
||||
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(
|
||||
request.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(
|
||||
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)
|
||||
|
||||
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,
|
||||
'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,
|
||||
'category_list': category_list,
|
||||
'message': message,
|
||||
'person': person
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue