Merge branch 'cart' into andreas
This commit is contained in:
commit
5084b2a42b
|
@ -1,5 +1,12 @@
|
|||
from django import forms
|
||||
from webshop.models import Salutation, City, Picture, Article, Option
|
||||
from webshop.models import (
|
||||
Salutation,
|
||||
City,
|
||||
Picture,
|
||||
Article,
|
||||
Option,
|
||||
OrderPosition
|
||||
)
|
||||
|
||||
|
||||
class RegistrationForm(forms.Form):
|
||||
|
@ -58,3 +65,14 @@ class AddToCartForm(forms.Form):
|
|||
label='Amount in piece.',
|
||||
help_text="Enter a Value between 1 and 99.",
|
||||
initial=1)
|
||||
|
||||
|
||||
class CartForm(forms.Form):
|
||||
def ChangeAmount(self):
|
||||
article = OrderPosition.objects.filter(pk=self.id)
|
||||
amountfield = forms.IntegerField(
|
||||
label='pce',
|
||||
help_text='Enter a Value between 1 and 99.',
|
||||
initial=article.amount
|
||||
)
|
||||
return amountfield
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -5,24 +5,37 @@
|
|||
{% if articles_list %}
|
||||
<table class="table">
|
||||
<tr class="table_header">
|
||||
<th scope="col">ID</th>
|
||||
<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</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.id %}">
|
||||
<a href="{% url 'details' article.article.id %}">
|
||||
{{ article.article.name }}
|
||||
</a></td>
|
||||
</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.article.price_in_chf }}
|
||||
{{ currency_name }}
|
||||
</td>
|
||||
<td scope="col">{{ article.position_price }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
<tr>
|
||||
<td scope="col" colspan="7" class="text-right">
|
||||
Total: {{ total }}
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
{% else %}
|
||||
<p class="alert">
|
||||
|
|
|
@ -3,17 +3,23 @@ from django.shortcuts import get_object_or_404, render
|
|||
from django.contrib.auth.decorators import login_required
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.auth.forms import UserCreationForm
|
||||
from django.db import transaction
|
||||
from webshop.models import (Article, Category, ArticleStatus, Person,
|
||||
City, Picture, CartPosition, ShoppingCart)
|
||||
from webshop.forms import RegistrationForm, AddToCartForm
|
||||
from webshop.models import (Article,
|
||||
Category,
|
||||
ArticleStatus,
|
||||
Person,
|
||||
City,
|
||||
Picture,
|
||||
CartPosition,
|
||||
ShoppingCart)
|
||||
from webshop.forms import (RegistrationForm,
|
||||
AddToCartForm,
|
||||
CartForm)
|
||||
|
||||
from currencies.models import ExchangeRate, ExchangeRate_name
|
||||
from currencies.forms import CurrenciesForm
|
||||
from decimal import Decimal
|
||||
|
||||
|
||||
# Create your views here.
|
||||
|
||||
def get_categories():
|
||||
parent_category_list = Category.objects.filter(parent_category=None)
|
||||
category_list = {}
|
||||
|
@ -127,11 +133,9 @@ def article_details(request, article_id):
|
|||
picture_list = Picture.objects.filter(article=article_id)
|
||||
|
||||
if request.method == 'POST':
|
||||
print(request.POST)
|
||||
# hier wird das Currency dropdown bearbeitet:
|
||||
if 'currencies' in request.POST:
|
||||
currencies_form = CurrenciesForm(request.POST)
|
||||
print("currencies_form")
|
||||
if currencies_form.is_valid():
|
||||
cf = currencies_form.cleaned_data
|
||||
if cf['currencies']:
|
||||
|
@ -145,27 +149,36 @@ def article_details(request, article_id):
|
|||
# hier wird der Artikel in den Wahrenkorb transferiert:
|
||||
if 'amount' in request.POST:
|
||||
amount = AddToCartForm(request.POST)
|
||||
print("add_to_cart_form")
|
||||
if amount.is_valid():
|
||||
print("is valid")
|
||||
amount = amount.cleaned_data['amount']
|
||||
currency_id = request.session['currency']
|
||||
print("amount:", amount,
|
||||
"article_id:", article_id,
|
||||
"currency_id:", currency_id)
|
||||
article = Article.objects.get(id=article_id)
|
||||
try:
|
||||
# lookup if cart_id is already existent:
|
||||
cart_id = ShoppingCart.objects.get(user=request.user)
|
||||
except:
|
||||
# if cart_id is not existent create a cart:
|
||||
cart_id = ShoppingCart.objects.create(user=request.user)
|
||||
cart_id.save()
|
||||
if cart_id:
|
||||
cart_position = CartPosition.objects.create(
|
||||
article=article,
|
||||
amount=amount,
|
||||
cart=ShoppingCart.objects.get(user=request.user)
|
||||
)
|
||||
cart_position.save()
|
||||
# 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.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,
|
||||
cart=ShoppingCart.objects.get(user=request.user)
|
||||
)
|
||||
cart_position.save()
|
||||
# write default value (1) to form field:
|
||||
amount = AddToCartForm()
|
||||
else:
|
||||
amount = AddToCartForm()
|
||||
|
@ -236,41 +249,82 @@ def cart(request):
|
|||
message = ""
|
||||
cart_id = False
|
||||
articles_list = ""
|
||||
prices_in_cart = []
|
||||
totalprice_list = []
|
||||
total = 0
|
||||
|
||||
if not 'currency' in request.session:
|
||||
request.session['currency'] = None
|
||||
else:
|
||||
currency = request.session['currency']
|
||||
|
||||
if request.method == '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 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:
|
||||
print(request.POST)
|
||||
amount = CartForm.ChangeAmount(request.POST)
|
||||
if amount.is_valid():
|
||||
amount = amount.cleaned_data['amount']
|
||||
article = Article.objects.get(id=article_id)
|
||||
try:
|
||||
cart_id = ShoppingCart.objects.get(user=request.user)
|
||||
except:
|
||||
cart_id = ShoppingCart.objects.create(user=request.user)
|
||||
cart_id.save()
|
||||
if cart_id:
|
||||
cart_position = CartPosition.objects.create(
|
||||
article=article,
|
||||
amount=amount,
|
||||
cart=ShoppingCart.objects.get(user=request.user)
|
||||
)
|
||||
cart_position.save()
|
||||
amount = CartForm.ChangeAmount()
|
||||
else:
|
||||
amount = AddToCartForm()
|
||||
# if the cart_id is set the user has already added items to cart.
|
||||
try:
|
||||
cart_id = ShoppingCart.objects.get(user=request.user)
|
||||
except Exception as e:
|
||||
message = "You have no items in the Basket"
|
||||
if cart_id and request.session['currency']:
|
||||
|
||||
if cart_id:
|
||||
articles = CartPosition.objects.filter(cart=cart_id)
|
||||
articles_list = list(articles)
|
||||
currency = request.session['currency']
|
||||
# scrap out the details to calculate Total of item and Summ of All:
|
||||
for idx, article in enumerate(articles_list):
|
||||
article.price_in_chf = rate.exchange(
|
||||
currency, article.article.price_in_chf)
|
||||
print(article, idx)
|
||||
article.calculate_position_price()
|
||||
if currency:
|
||||
article.price_in_chf = rate.exchange(
|
||||
currency, article.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 = Decimal.from_float(article.amount)
|
||||
totalprice_list.append(article.position_price)
|
||||
articles_list[idx] = article
|
||||
currency_name = ExchangeRate_name.objects.get(id=currency)
|
||||
article.price_in_chf = rate.exchange(currency, article.price_in_chf)
|
||||
else:
|
||||
articles = CartPosition.objects.filter(cart=cart_id)
|
||||
articles_list = list(articles)
|
||||
|
||||
total = sum(totalprice_list)
|
||||
|
||||
return render(request, 'webshop/cart.html',
|
||||
{'articles_list': articles_list,
|
||||
'totalprice_list': totalprice_list,
|
||||
'total': total,
|
||||
'currencies_form': currencies_form,
|
||||
'article_view': article_view,
|
||||
'currency_name': currency_name,
|
||||
|
|
Loading…
Reference in New Issue