integrate process_article_prices() into cart
This commit is contained in:
parent
9b31f2a20f
commit
70e21130ea
|
@ -112,11 +112,6 @@ class CartPosition(models.Model):
|
||||||
article = models.ForeignKey(Article, on_delete=models.CASCADE)
|
article = models.ForeignKey(Article, on_delete=models.CASCADE)
|
||||||
amount = models.FloatField(max_length=5)
|
amount = models.FloatField(max_length=5)
|
||||||
cart = models.ForeignKey(ShoppingCart, on_delete=models.CASCADE)
|
cart = models.ForeignKey(ShoppingCart, on_delete=models.CASCADE)
|
||||||
position_price = models.DecimalField(max_digits=19,
|
|
||||||
decimal_places=2,
|
|
||||||
validators=[MinValueValidator(
|
|
||||||
Decimal('0.00'))],
|
|
||||||
null=True)
|
|
||||||
|
|
||||||
def calculate_position_price(self):
|
def calculate_position_price(self):
|
||||||
decimal_amount = Decimal.from_float(self.amount)
|
decimal_amount = Decimal.from_float(self.amount)
|
||||||
|
|
|
@ -13,22 +13,22 @@
|
||||||
<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 article in articles_list %}
|
{% for position in cart_position_list %}
|
||||||
<tr class="table_content">
|
<tr class="table_content">
|
||||||
<td scope="col">{{ article.id }}</td>
|
<td scope="col">{{ position.id }}</td>
|
||||||
<td scope="col">{{ article.article.id }}</td>
|
<td scope="col">{{ position.article.id }}</td>
|
||||||
<td scope="col">
|
<td scope="col">
|
||||||
<a href="{% url 'details' article.article.id %}">
|
<a href="{% url 'details' position.article.id %}">
|
||||||
{{ article.article.name }}
|
{{ position.article.name }}
|
||||||
</a>
|
</a>
|
||||||
</td>
|
</td>
|
||||||
<td scope="col">{{ article.article.stock }}</td>
|
<td scope="col">{{ position.article.stock }}</td>
|
||||||
<td scope="col">{{ article.amount }}</td>
|
<td scope="col">{{ position.amount }}</td>
|
||||||
<td scope="col">
|
<td scope="col">
|
||||||
{{ article.article.price_in_chf }}
|
{{ position.article.price_in_chf }}
|
||||||
{{ currency_name }}
|
{{ currency_name }}
|
||||||
</td>
|
</td>
|
||||||
<td scope="col">{{ article.position_price }} {{ currency_name }}</td>
|
<td scope="col">{{ position.position_price }} {{ currency_name }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<tr>
|
<tr>
|
||||||
|
|
|
@ -123,7 +123,6 @@ def article_details(request, article_id):
|
||||||
cart_position = CartPosition.objects.create(
|
cart_position = CartPosition.objects.create(
|
||||||
article=article,
|
article=article,
|
||||||
amount=amount,
|
amount=amount,
|
||||||
position_price=article.price_in_chf,
|
|
||||||
cart=ShoppingCart.objects.get(user=request.user)
|
cart=ShoppingCart.objects.get(user=request.user)
|
||||||
)
|
)
|
||||||
cart_position.save()
|
cart_position.save()
|
||||||
|
@ -194,33 +193,12 @@ def cart(request):
|
||||||
currencies_form = CurrenciesForm
|
currencies_form = CurrenciesForm
|
||||||
rate = ExchangeRate
|
rate = ExchangeRate
|
||||||
article_view = True
|
article_view = True
|
||||||
currency_name = "CHF"
|
|
||||||
message = ""
|
message = ""
|
||||||
cart_id = False
|
cart_id = False
|
||||||
articles_list = ""
|
|
||||||
prices_in_cart = []
|
|
||||||
totalprice_list = []
|
totalprice_list = []
|
||||||
total = 0
|
total = 0
|
||||||
|
|
||||||
if not 'currency' in request.session:
|
|
||||||
request.session['currency'] = None
|
|
||||||
else:
|
|
||||||
currency = request.session['currency']
|
|
||||||
|
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
# here we react to a currency dropdown change:
|
|
||||||
if 'currencies' in request.POST:
|
|
||||||
currencies_form = CurrenciesForm(request.POST)
|
|
||||||
if currencies_form.is_valid():
|
|
||||||
cf = currencies_form.cleaned_data
|
|
||||||
if cf['currencies']:
|
|
||||||
selection = cf['currencies']
|
|
||||||
request.session['currency'] = selection.id
|
|
||||||
currency_name = ExchangeRate_name.objects.get(
|
|
||||||
id=selection.id)
|
|
||||||
else:
|
|
||||||
request.session['currency'] = None
|
|
||||||
# here we react to a change of amount per item in the Cart:
|
|
||||||
if 'amount' in request.POST:
|
if 'amount' in request.POST:
|
||||||
print(request.POST)
|
print(request.POST)
|
||||||
amount = CartForm.ChangeAmount(request.POST)
|
amount = CartForm.ChangeAmount(request.POST)
|
||||||
|
@ -243,40 +221,42 @@ def cart(request):
|
||||||
else:
|
else:
|
||||||
amount = AddToCartForm()
|
amount = AddToCartForm()
|
||||||
# if the cart_id is set the user has already added items to cart.
|
# if the cart_id is set the user has already added items to cart.
|
||||||
|
|
||||||
try:
|
try:
|
||||||
cart_id = ShoppingCart.objects.get(user=request.user)
|
cart_id = ShoppingCart.objects.get(user=request.user)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
message = "You have no items in the Basket"
|
message = "You have no items in the Basket"
|
||||||
|
return render(request, 'webshop/cart.html',
|
||||||
|
{'currencies_form': currencies_form,
|
||||||
|
'article_view': article_view,
|
||||||
|
'currency_name': "CHF",
|
||||||
|
'category_list': category_list,
|
||||||
|
'message': message})
|
||||||
|
|
||||||
if cart_id:
|
articles = []
|
||||||
articles = CartPosition.objects.filter(cart=cart_id)
|
cart_position = CartPosition.objects.filter(cart=cart_id)
|
||||||
articles_list = list(articles)
|
cart_position_list = list(cart_position)
|
||||||
# scrap out the details to calculate Total of item and Summ of All:
|
for idx, cart_position in enumerate(cart_position_list):
|
||||||
for idx, article in enumerate(articles_list):
|
articles.append(cart_position.article)
|
||||||
print(article, idx)
|
|
||||||
article.calculate_position_price()
|
return_values = process_article_prices(request, articles)
|
||||||
if currency:
|
articles_list = return_values['articles_list']
|
||||||
article.price_in_chf = rate.exchange(
|
currency_name = return_values['currency_name']
|
||||||
currency, article.article.price_in_chf)
|
|
||||||
# get currencyname to display:
|
for idx, cart_position in enumerate(cart_position_list):
|
||||||
currency_name = ExchangeRate_name.objects.get(id=currency)
|
cart_position.calculate_position_price()
|
||||||
# get exchange_rate multiplyed:
|
cart_position.article = articles_list[idx]
|
||||||
article.price_in_chf = rate.exchange(
|
totalprice_list.append(cart_position.position_price)
|
||||||
currency,
|
|
||||||
article.price_in_chf)
|
|
||||||
amount = Decimal.from_float(article.amount)
|
|
||||||
totalprice_list.append(article.position_price)
|
|
||||||
articles_list[idx] = article
|
|
||||||
|
|
||||||
total = sum(totalprice_list)
|
total = sum(totalprice_list)
|
||||||
|
|
||||||
return render(request, 'webshop/cart.html',
|
return render(request, 'webshop/cart.html',
|
||||||
{'articles_list': articles_list,
|
{'articles_list': articles_list,
|
||||||
'totalprice_list': totalprice_list,
|
'totalprice_list': totalprice_list,
|
||||||
'total': total,
|
'cart_position_list': cart_position_list,
|
||||||
'currencies_form': currencies_form,
|
'total': total,
|
||||||
'article_view': article_view,
|
'currencies_form': currencies_form,
|
||||||
'currency_name': currency_name,
|
'article_view': article_view,
|
||||||
'category_list': category_list,
|
'currency_name': currency_name,
|
||||||
'message': message,
|
'category_list': category_list,
|
||||||
})
|
'message': message,
|
||||||
|
})
|
||||||
|
|
Loading…
Reference in New Issue