push for review

This commit is contained in:
Andreas Zweili 2018-02-11 21:56:53 +01:00
parent 775f32887a
commit 7a9c06bbba
4 changed files with 30 additions and 9 deletions

View File

@ -112,6 +112,15 @@ class CartPosition(models.Model):
article = models.ForeignKey(Article, on_delete=models.CASCADE)
amount = models.FloatField(max_length=5)
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):
decimal_amount = Decimal.from_float(self.amount)
self.position_price = decimal_amount * self.article.price_in_chf
class City(models.Model):

View File

@ -1,4 +1,5 @@
{% extends "webshop/base.html" %}
{% load cart_filters %}
{% block section_title %}<h1>Cart</h1>{% endblock %}
{% block content %}
<h3>List of Items in your Shopping Cart:</h3>
@ -11,7 +12,8 @@
<th scope="col">STOCK</th>
<th scope="col">AMOUNT</th>
<th scope="col">PRICE p.pce.</th>
<th scope="col">PRICE</th>
<th scope="col">PRICE through Model</th>
<th scope="col">PRICE through Filter</th>
</tr>
{% for article in articles_list %}
<tr class="table_content">
@ -28,7 +30,8 @@
{{ article.article.price_in_chf }}
{{ currency_name }}
</td>
<td scope="col">{{ totalprice_of_one }}</td>
<td scope="col">{{ article.position_price }}</td>
<td scope="col">{{ totalprice_list|lookup:article.article.id }}</td>
</tr>
{% endfor %}
<tr>

View File

@ -0,0 +1,7 @@
from django import template
register = template.Library()
@register.filter
def lookup(d, key):
return d[key]

View File

@ -234,6 +234,7 @@ def cart(request):
cart_id = False
articles_list = ""
prices_in_cart = []
totalprice_list = {}
total = 0
if not 'currency' in request.session:
@ -262,19 +263,20 @@ def cart(request):
articles_list = list(articles)
for idx, article in enumerate(articles_list):
print(article, idx)
if currency is not None:
article.calculate_position_price()
if currency:
article.price_in_chf = rate.exchange(
currency, article.article.price_in_chf)
currency_name = ExchangeRate_name.objects.get(id=currency)
article.price_in_chf = rate.exchange(
currency,
article.price_in_chf)
amount = article.amount
totalprice_of_one = Decimal(amount) * article.article.price_in_chf
amount = Decimal.from_float(article.amount)
totalprice_list.update({
article.article.id:amount * article.article.price_in_chf
})
articles_list[idx] = article
prices_in_cart.append(article.article.price_in_chf)
# if cart_id and request.session['currency']:
# articles = CartPosition.objects.filter(cart=cart_id)
# articles_list = list(articles)
@ -295,11 +297,11 @@ def cart(request):
# for idx, article in enumerate(articles_list):
# prices_in_cart.append(article.article.price_in_chf)
total = sum(prices_in_cart)
total = sum(totalprice_list.values())
return render(request, 'webshop/cart.html',
{'articles_list': articles_list,
'totalprice_of_one': totalprice_of_one,
'totalprice_list': totalprice_list,
'total': total,
'currencies_form': currencies_form,
'article_view': article_view,