Merge branch 'cart' of git.2li.ch:ibz/web_AI-5 into cart

This commit is contained in:
Andreas Zweili 2018-02-11 22:00:17 +01:00
commit 6ef396a6d2
2 changed files with 68 additions and 21 deletions

View File

@ -1,5 +1,12 @@
from django import forms 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): class RegistrationForm(forms.Form):
@ -58,3 +65,14 @@ class AddToCartForm(forms.Form):
label='Amount in piece.', label='Amount in piece.',
help_text="Enter a Value between 1 and 99.", help_text="Enter a Value between 1 and 99.",
initial=1) 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

View File

@ -3,9 +3,17 @@ from django.shortcuts import get_object_or_404, render
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm from django.contrib.auth.forms import UserCreationForm
from webshop.models import (Article, Category, ArticleStatus, Person, from webshop.models import (Article,
City, Picture, CartPosition, ShoppingCart) Category,
from webshop.forms import RegistrationForm, AddToCartForm ArticleStatus,
Person,
City,
Picture,
CartPosition,
ShoppingCart)
from webshop.forms import (RegistrationForm,
AddToCartForm,
CartForm)
from currencies.models import ExchangeRate, ExchangeRate_name from currencies.models import ExchangeRate, ExchangeRate_name
from currencies.forms import CurrenciesForm from currencies.forms import CurrenciesForm
@ -125,11 +133,9 @@ def article_details(request, article_id):
picture_list = Picture.objects.filter(article=article_id) picture_list = Picture.objects.filter(article=article_id)
if request.method == 'POST': if request.method == 'POST':
print(request.POST)
# hier wird das Currency dropdown bearbeitet: # hier wird das Currency dropdown bearbeitet:
if 'currencies' in request.POST: if 'currencies' in request.POST:
currencies_form = CurrenciesForm(request.POST) currencies_form = CurrenciesForm(request.POST)
print("currencies_form")
if currencies_form.is_valid(): if currencies_form.is_valid():
cf = currencies_form.cleaned_data cf = currencies_form.cleaned_data
if cf['currencies']: if cf['currencies']:
@ -143,14 +149,9 @@ def article_details(request, article_id):
# hier wird der Artikel in den Wahrenkorb transferiert: # hier wird der Artikel in den Wahrenkorb transferiert:
if 'amount' in request.POST: if 'amount' in request.POST:
amount = AddToCartForm(request.POST) amount = AddToCartForm(request.POST)
print("add_to_cart_form")
if amount.is_valid(): if amount.is_valid():
print("is valid")
amount = amount.cleaned_data['amount'] amount = amount.cleaned_data['amount']
currency_id = request.session['currency'] currency_id = request.session['currency']
print("amount:", amount,
"article_id:", article_id,
"currency_id:", currency_id)
article = Article.objects.get(id=article_id) article = Article.objects.get(id=article_id)
try: try:
cart_id = ShoppingCart.objects.get(user=request.user) cart_id = ShoppingCart.objects.get(user=request.user)
@ -243,16 +244,41 @@ def cart(request):
currency = request.session['currency'] currency = request.session['currency']
if request.method == 'POST': if request.method == 'POST':
currencies_form = CurrenciesForm(request.POST) # here we react to a currency dropdown change:
if currencies_form.is_valid(): if 'currencies' in request.POST:
cf = currencies_form.cleaned_data currencies_form = CurrenciesForm(request.POST)
if cf['currencies']: if currencies_form.is_valid():
selection = cf['currencies'] cf = currencies_form.cleaned_data
request.session['currency'] = selection.id if cf['currencies']:
currency_name = ExchangeRate_name.objects.get(id=selection.id) selection = cf['currencies']
else: request.session['currency'] = selection.id
request.session['currency'] = None 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: 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:
@ -261,13 +287,16 @@ def cart(request):
if cart_id: if cart_id:
articles = CartPosition.objects.filter(cart=cart_id) articles = CartPosition.objects.filter(cart=cart_id)
articles_list = list(articles) articles_list = list(articles)
# scrap out the details to calculate Total of item and Summ of All:
for idx, article in enumerate(articles_list): for idx, article in enumerate(articles_list):
print(article, idx) print(article, idx)
article.calculate_position_price() article.calculate_position_price()
if currency: if currency:
article.price_in_chf = rate.exchange( article.price_in_chf = rate.exchange(
currency, article.article.price_in_chf) currency, article.article.price_in_chf)
# get currencyname to display:
currency_name = ExchangeRate_name.objects.get(id=currency) currency_name = ExchangeRate_name.objects.get(id=currency)
# get exchange_rate multiplyed:
article.price_in_chf = rate.exchange( article.price_in_chf = rate.exchange(
currency, currency,
article.price_in_chf) article.price_in_chf)