web_AI-5/django/didgeridoo/webshop/views.py

421 lines
16 KiB
Python
Raw Normal View History

2017-12-29 16:46:21 +01:00
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
2017-12-28 19:22:00 +01:00
from django.contrib.auth.decorators import login_required
2017-12-29 16:46:21 +01:00
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
2018-02-17 10:51:32 +01:00
from django.db import transaction
2018-02-11 19:58:49 +01:00
from webshop.models import (Article,
Category,
Person,
City,
Picture,
CartPosition,
2018-02-26 19:56:45 +01:00
ShoppingCart,
2018-02-26 21:22:07 +01:00
Order,
2018-02-26 22:22:45 +01:00
OrderStatus,
OrderPosition)
2018-02-11 19:58:49 +01:00
from webshop.forms import (RegistrationForm,
AddToCartForm,
2018-02-17 10:51:32 +01:00
CartForm,
CheckoutForm)
from webshop.utils import (get_categories,
get_hidden_status_id,
process_article_prices)
2017-11-12 21:35:59 +01:00
from currencies.models import ExchangeRate, ExchangeRate_name
from currencies.forms import CurrenciesForm
2018-02-19 21:13:08 +01:00
def index(request):
category_list = get_categories()
currencies_form = CurrenciesForm
article_view = True
articles = Article.objects.all().exclude(status=get_hidden_status_id())
return_values = process_article_prices(request, articles)
articles_list = return_values['articles_list']
currency_name = return_values['currency_name']
2017-12-19 20:12:21 +01:00
return render(request,
2018-02-01 17:02:08 +01:00
'webshop/index.html',
{'category_list': category_list,
'articles_list': articles_list,
'currencies_form': currencies_form,
'article_view': article_view,
'currency_name': currency_name})
def articles_in_category(request, category_id):
category_list = get_categories()
selected_category = Category.objects.get(id=category_id)
currencies_form = CurrenciesForm
article_view = True
articles = Article.objects.filter(
category=selected_category.id).exclude(status=get_hidden_status_id())
return_values = process_article_prices(request, articles)
articles_list = return_values['articles_list']
currency_name = return_values['currency_name']
2017-12-19 20:12:21 +01:00
return render(request, 'webshop/category.html',
{'articles_list': articles_list,
'category_list': category_list,
'currencies_form': currencies_form,
'article_view': article_view,
'currency_name': currency_name,
2017-12-19 20:12:21 +01:00
'category': selected_category})
def restrict_cart_to_one_article(user_id, article_id, amount, operation):
2018-02-25 09:30:46 +01:00
# if cart_id is not existent create a cart:
cart_id, created_cart = ShoppingCart.objects.get_or_create(user=user_id)
article = Article.objects.get(id=article_id)
# transfair Article to CartPosition:
2018-02-25 09:30:46 +01:00
cart_position, created_position = CartPosition.objects.get_or_create(
article=article,
defaults={'amount': amount,
'position_price': article.price_in_chf,
'cart': cart_id
}
)
if created_position is False:
2018-02-21 22:35:29 +01:00
if operation == 'delete':
2018-02-25 09:30:46 +01:00
cart_position.delete()
if (operation == 'add') or (operation == 'replace'):
if operation == 'add':
2018-02-25 09:30:46 +01:00
new_amount = cart_position.amount + amount
if operation == 'replace':
2018-02-26 19:56:45 +01:00
new_amount = amount
2018-02-25 09:30:46 +01:00
# if article is in cart already update amount:
cart_position = CartPosition.objects.filter(
article=article_id).update(
amount=new_amount
)
def article_details(request, article_id):
category_list = get_categories()
currencies_form = CurrenciesForm
2018-02-01 17:02:08 +01:00
amount = AddToCartForm
rate = ExchangeRate
article_view = True
currency_name = "CHF"
2018-02-25 19:57:50 +01:00
user = request.user
2018-02-19 21:13:08 +01:00
if 'currency' not in request.session:
request.session['currency'] = None
article = get_object_or_404(Article, pk=article_id)
picture_list = Picture.objects.filter(article=article_id)
if request.method == 'POST':
2018-02-01 17:02:08 +01:00
# hier wird das Currency dropdown bearbeitet:
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
2018-02-01 17:02:08 +01:00
# hier wird der Artikel in den Wahrenkorb transferiert:
if 'amount' in request.POST:
amount = AddToCartForm(request.POST)
if amount.is_valid():
2018-02-01 17:05:58 +01:00
amount = amount.cleaned_data['amount']
operation = 'add'
restrict_cart_to_one_article(
2018-02-25 19:57:50 +01:00
user,
article_id,
amount,
operation
)
2018-02-11 23:49:36 +01:00
# write default value (1) to form field:
2018-02-01 17:05:58 +01:00
amount = AddToCartForm()
2018-02-01 17:02:08 +01:00
else:
amount = AddToCartForm()
if request.session['currency']:
currency = request.session['currency']
article.price_in_chf = rate.exchange(currency, article.price_in_chf)
2018-02-01 17:02:08 +01:00
currency_name = ExchangeRate_name.objects.get(id=currency)
return render(request, 'webshop/article_details.html',
{'article': article,
'category_list': category_list,
'currencies_form': currencies_form,
'article_view': article_view,
'currency_name': currency_name,
2018-02-01 17:02:08 +01:00
'picture_list': picture_list,
'amount': amount
})
2017-12-28 19:22:00 +01:00
@login_required
def profile(request):
category_list = get_categories()
2017-12-28 19:22:00 +01:00
person = Person.objects.get(user=request.user)
return render(request, 'registration/profile.html',
{'person': person,
'category_list': category_list})
2017-12-29 16:46:21 +01:00
def registration(request):
category_list = get_categories()
2017-12-29 16:46:21 +01:00
if request.method == 'POST':
profile_form = RegistrationForm(request.POST)
user_form = UserCreationForm(request.POST)
if (profile_form.is_valid() and user_form.is_valid()):
with transaction.atomic():
pf = profile_form.cleaned_data
uf = user_form.cleaned_data
user = User.objects.create_user(uf['username'],
pf['email'],
uf['password2'])
user.last_name = pf['last_name']
user.first_name = pf['first_name']
user.save()
2018-02-26 22:32:21 +01:00
Person.objects.create(
salutation=pf['salutation'],
city=City.objects.get(zip_code=pf['zip_code'],
2018-02-19 21:13:08 +01:00
name=pf['city']),
street_name=pf['street_name'],
street_number=pf['street_number'],
user=user)
2017-12-29 16:46:21 +01:00
return HttpResponseRedirect('/login/')
else:
profile_form = RegistrationForm
user_form = UserCreationForm
return render(request, 'registration/register.html',
{'profile_form': profile_form,
'category_list': category_list,
2017-12-29 16:46:21 +01:00
'user_form': user_form})
2018-02-04 18:32:54 +01:00
@login_required
2018-02-04 18:32:54 +01:00
def cart(request):
2018-02-04 18:58:21 +01:00
category_list = get_categories()
2018-02-04 18:32:54 +01:00
currencies_form = CurrenciesForm
amount_form = CartForm
2018-02-04 18:32:54 +01:00
rate = ExchangeRate
article_view = True
currency_name = "CHF"
2018-02-04 19:44:09 +01:00
message = ""
cart_position_list = []
amount_form_list = []
2018-02-11 22:37:27 +01:00
totalprice_list = []
total = 0
cart_position_list_zip = []
# here we configure the users Currency:
2018-02-19 21:13:08 +01:00
if 'currency' not in request.session:
2018-02-04 18:32:54 +01:00
request.session['currency'] = None
2018-02-11 18:19:21 +01:00
else:
currency = request.session['currency']
2018-02-04 20:38:35 +01:00
# Here we handle all POST Operations:
2018-02-04 18:32:54 +01:00
if request.method == 'POST':
2018-02-11 19:58:49 +01:00
# here we react to a currency dropdown change:
if 'currencies' in request.POST:
2018-02-18 19:28:39 +01:00
print('currencies')
2018-02-11 19:58:49 +01:00
currencies_form = CurrenciesForm(request.POST)
if currencies_form.is_valid():
cf = currencies_form.cleaned_data
if cf['currencies']:
2018-02-25 12:02:32 +01:00
print('currencies cf:', cf)
2018-02-11 19:58:49 +01:00
selection = cf['currencies']
request.session['currency'] = selection.id
currency_name = ExchangeRate_name.objects.get(
id=selection.id)
2018-02-25 12:02:32 +01:00
print('currencies currency_name:', currency_name)
2018-02-11 19:58:49 +01:00
else:
request.session['currency'] = None
2018-02-11 19:58:49 +01:00
# here we react to a change of amount per item in the Cart:
if 'amount_form' in request.POST:
amount_form = CartForm(request.POST)
if amount_form.is_valid():
amount = amount_form.cleaned_data['amount_form']
article_id = request.POST.get('article_id')
operation = 'replace'
restrict_cart_to_one_article(
request.user.id,
article_id,
amount,
operation
)
2018-02-21 21:59:50 +01:00
# here we react to a change of amount per item in the Cart:
if 'delete' in request.POST:
delete = CartForm(request.POST)
if delete.is_valid():
amount = delete.cleaned_data['amount_form']
article_id = request.POST.get('article_id')
amount = 1
operation = 'delete'
restrict_cart_to_one_article(
request.user.id,
2018-02-21 21:59:50 +01:00
article_id,
amount,
operation
)
2018-02-20 20:59:18 +01:00
2018-02-25 14:22:22 +01:00
# here we handle the normal cart view:
# if cart_id is not existent create a cart:
2018-02-25 17:30:20 +01:00
cart_id, created_cart = ShoppingCart.objects.get_or_create(
user=request.user
)
# get all items in the cart of this customer:
cart_positions = CartPosition.objects.filter(cart=cart_id)
if (cart_positions.count()) > 0:
# make a list out of all articles:
cart_position_list = list(cart_positions)
# enumerate the list of articles and loop over items:
2018-02-20 20:59:18 +01:00
for idx, cart_position in enumerate(cart_position_list):
# scrap out the details to calculate Total of item and Summ of All:
if request.session['currency']:
currency = request.session['currency']
# get currencyname to display:
currency_name = ExchangeRate_name.objects.get(id=currency)
# get exchange_rate multiplyed:
cart_position.article.price_in_chf = rate.exchange(
currency,
cart_position.article.price_in_chf
)
amount_form = CartForm(
initial={'amount_form': cart_position.amount}
)
2018-02-25 19:58:30 +01:00
cart_position.calculate_position_price()
totalprice_list.append(cart_position.position_price)
amount_form_list.append(amount_form)
cart_position_list[idx] = cart_position
cart_position_list_zip = zip(cart_position_list, amount_form_list)
total = sum(totalprice_list)
return render(request, 'webshop/cart.html',
{'cart_position_list_zip': cart_position_list_zip,
'totalprice_list': totalprice_list,
'total': total,
'currencies_form': currencies_form,
'amount_form': amount_form,
'article_view': article_view,
'currency_name': currency_name,
'category_list': category_list,
'message': message,
})
2018-02-25 14:23:18 +01:00
@login_required
def checkout(request):
category_list = get_categories()
rate = ExchangeRate
article_view = False
2018-02-26 22:22:22 +01:00
currency_name = "CHF"
exchange_rate = False
message = ""
cart_position_list = []
totalprice_list = []
total = 0
2018-02-26 22:23:15 +01:00
person = Person.objects.get(user=request.user.id)
2018-02-26 19:56:45 +01:00
checkout_form = CheckoutForm()
if 'currency' not in request.session:
request.session['currency'] = None
else:
currency = request.session['currency']
2018-02-26 21:54:11 +01:00
if currency:
exchange_rate = rate.objects.filter(name=currency).latest('date')
2018-02-20 20:59:18 +01:00
cart = ShoppingCart.objects.get(user=request.user)
if cart:
2018-02-26 19:56:45 +01:00
# get all items in the cart of this customer:
cart_positions = CartPosition.objects.filter(cart=cart)
2018-02-26 19:56:45 +01:00
if (cart_positions.count()) > 0:
# make a list out of all articles:
cart_position_list = list(cart_positions)
# enumerate the list of articles and loop over items:
for idx, cart_position in enumerate(cart_position_list):
if request.session['currency']:
2018-02-26 19:56:45 +01:00
# get currencyname to display:
currency_name = ExchangeRate_name.objects.get(id=currency)
# get exchange_rate multiplyed:
cart_position.article.price_in_chf = rate.exchange(
currency,
cart_position.article.price_in_chf
)
cart_position.calculate_position_price()
totalprice_list.append(cart_position.position_price)
cart_position_list[idx] = cart_position
2018-02-26 22:23:29 +01:00
2018-02-26 19:56:45 +01:00
else:
message = """something whent wrong.
Seams like your cart was
not existent before. How come? """
2018-02-26 22:23:29 +01:00
2018-02-20 20:59:18 +01:00
total = sum(totalprice_list)
2018-02-11 18:19:21 +01:00
# Here we handle all POST Operations:
if request.method == 'POST':
print('checkout post', request.POST)
# here we react to a change of amount per item in the Cart:
if 'checkout' in request.POST:
print('checkout post request.POST = checkout_form')
checkout_form = CheckoutForm(request.POST)
if checkout_form.is_valid():
orderstatus = OrderStatus.objects.get(name='ordered')
if exchange_rate:
order = Order.objects.create(user=request.user,
status=orderstatus,
exchange_rate=exchange_rate)
else:
order = Order.objects.create(user=request.user,
status=orderstatus)
print('order', order, 'created:', order)
for position in cart_positions:
OrderPosition.objects.create(
2018-02-26 22:44:36 +01:00
article=position.article,
order=order,
amount=position.amount,
price_in_chf=position.article.price_in_chf
)
2018-02-26 22:44:47 +01:00
cart.delete()
cart = False
return render(request, 'webshop/checkout.html',
2018-02-26 19:56:45 +01:00
{'cart_position_list': cart_position_list,
2018-02-04 21:48:29 +01:00
'total': total,
'checkout_form': checkout_form,
2018-02-04 18:32:54 +01:00
'currency_name': currency_name,
2018-02-26 19:56:45 +01:00
'article_view': article_view,
2018-02-04 18:58:21 +01:00
'category_list': category_list,
'message': message,
2018-02-25 14:20:59 +01:00
'person': person
2018-02-04 18:32:54 +01:00
})
2018-02-26 19:56:45 +01:00
def order(request):
2018-02-27 07:22:57 +01:00
cart = ShoppingCart.objects.get(user=request.user)
if cart:
# get all items in the cart of this customer:
cart_positions = CartPosition.objects.filter(cart=cart)
if (cart_positions.count()) > 0:
for cart_position in cart_positions:
restrict_cart_to_one_article(
request.user,
cart_position.article.id,
0,
'delete'
)
else:
message = """something whent wrong.
We cold not delete your cartitems. """
2018-02-26 21:56:07 +01:00
return render(request, 'webshop/order.html', {})