add amountform not working yet

This commit is contained in:
Ivan Hörler 2018-02-18 21:58:46 +01:00
parent f78bfb6db0
commit cb19ca6205
5 changed files with 90 additions and 34 deletions

View File

@ -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.',

View File

@ -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):

View File

@ -2,7 +2,7 @@
{% block section_title %}<h1>Cart</h1>{% endblock %}
{% block content %}
<h3>List of Items in your Shopping Cart:</h3>
{% if articles_list %}
{% if cart_position_list %}
<table class="table">
<tr class="table_header">
<th scope="col">POS.</th>
@ -13,16 +13,16 @@
<th scope="col">PRICE p.pce.</th>
<th scope="col">POSITION PRICE</th>
</tr>
{% for article in articles_list %}
{% for cart_position in cart_position_list %}
<tr class="table_content">
<td scope="col">{{ article.id }}</td>
<td scope="col">{{ article.article.id }}</td>
<td scope="col">{{ cart_position.id }}</td>
<td scope="col">{{ cart_position.article.id }}</td>
<td scope="col">
<a href="{% url 'details' article.article.id %}">
{{ article.article.name }}
<a href="{% url 'details' cart_position.article.id %}">
{{ cart_position.article.name }}
</a>
</td>
<td scope="col">{{ article.article.stock }}</td>
<td scope="col">{{ cart_position.article.stock }}</td>
<td scope="col">
<form id="amountfield" action="" method="POST" novalidate>
{{ CartForm }}
@ -32,10 +32,10 @@
<!-- {{ article.amount }} -->
</td>
<td scope="col">
{{ article.article.price_in_chf }}
{{ cart_position.article.price_in_chf }}
{{ currency_name }}
</td>
<td scope="col">{{ article.position_price }} {{ currency_name }}</td>
<td scope="col">{{ cart_position.position_price }} {{ currency_name }}</td>
</tr>
{% endfor %}
<tr>
@ -44,11 +44,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>

View File

@ -0,0 +1,60 @@
{% extends "webshop/base.html" %}
{% block section_title %}<h1>Cart</h1>{% endblock %}
{% block content %}
<h3>List of Items in your Shopping Cart:</h3>
{% if articles_list %}
<table class="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>
</tr>
{% for article in articles_list %}
<tr class="table_content">
<td scope="col">{{ article.id }}</td>
<td scope="col">{{ article.article.id }}</td>
<td scope="col">
<a href="{% url 'details' article.article.id %}">
{{ article.article.name }}
</a>
</td>
<td scope="col">{{ article.article.stock }}</td>
<td scope="col">
{{ article.amount }}
</td>
<td scope="col">
{{ article.article.price_in_chf }}
{{ currency_name }}
</td>
<td scope="col">{{ article.position_price }} {{ currency_name }}</td>
</tr>
{% endfor %}
<tr>
<td scope="col" colspan="7" class="text-right">
Total: {{ total }} {{ currency_name }}
</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>
This cart seams to lack some Items.
Go get some in the store!
<strong>
</p>
{% endif %}
<p class="alert text-danger">
<strong>
{{ message }}
<strong>
</p>
{% endblock %}

View File

@ -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,