2018-02-12 22:38:16 +01:00
|
|
|
from decimal import Decimal
|
|
|
|
|
2017-12-29 16:46:21 +01:00
|
|
|
from django.http import HttpResponseRedirect
|
2017-12-17 10:35:21 +01:00
|
|
|
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,
|
|
|
|
ArticleStatus,
|
|
|
|
Person,
|
|
|
|
City,
|
|
|
|
Picture,
|
|
|
|
CartPosition,
|
|
|
|
ShoppingCart)
|
|
|
|
from webshop.forms import (RegistrationForm,
|
|
|
|
AddToCartForm,
|
2018-02-17 10:51:32 +01:00
|
|
|
CartForm,
|
|
|
|
CheckoutForm)
|
2018-02-12 22:38:16 +01:00
|
|
|
from webshop.utils import (get_categories,
|
|
|
|
get_hidden_status_id,
|
|
|
|
process_article_prices)
|
2017-11-12 21:35:59 +01:00
|
|
|
|
2018-01-28 20:41:14 +01:00
|
|
|
from currencies.models import ExchangeRate, ExchangeRate_name
|
|
|
|
from currencies.forms import CurrenciesForm
|
2018-01-28 20:39:22 +01:00
|
|
|
|
2018-01-28 20:41:14 +01:00
|
|
|
def index(request):
|
2018-01-28 20:37:47 +01:00
|
|
|
category_list = get_categories()
|
2018-01-28 20:41:14 +01:00
|
|
|
currencies_form = CurrenciesForm
|
|
|
|
article_view = True
|
|
|
|
|
2018-02-12 22:38:16 +01:00
|
|
|
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']
|
2018-01-28 20:41:14 +01:00
|
|
|
|
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})
|
2017-12-17 10:36:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
def articles_in_category(request, category_id):
|
2018-01-28 20:37:47 +01:00
|
|
|
category_list = get_categories()
|
2017-12-17 10:36:44 +01:00
|
|
|
selected_category = Category.objects.get(id=category_id)
|
2018-02-12 22:38:16 +01:00
|
|
|
|
2018-01-28 21:14:55 +01:00
|
|
|
currencies_form = CurrenciesForm
|
|
|
|
article_view = True
|
|
|
|
|
2018-02-12 22:38:16 +01:00
|
|
|
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-29 22:32:27 +01:00
|
|
|
|
2017-12-19 20:12:21 +01:00
|
|
|
return render(request, 'webshop/category.html',
|
2018-01-28 21:09:27 +01:00
|
|
|
{'articles_list': articles_list,
|
2017-12-29 22:32:27 +01:00
|
|
|
'category_list': category_list,
|
2018-01-28 21:14:55 +01:00
|
|
|
'currencies_form': currencies_form,
|
|
|
|
'article_view': article_view,
|
|
|
|
'currency_name': currency_name,
|
2017-12-19 20:12:21 +01:00
|
|
|
'category': selected_category})
|
2017-12-17 10:37:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
def article_details(request, article_id):
|
2018-01-28 20:37:47 +01:00
|
|
|
category_list = get_categories()
|
2018-01-28 21:26:36 +01:00
|
|
|
currencies_form = CurrenciesForm
|
2018-02-01 17:02:08 +01:00
|
|
|
amount = AddToCartForm
|
|
|
|
rate = ExchangeRate
|
2018-01-28 21:26:36 +01:00
|
|
|
article_view = True
|
|
|
|
currency_name = "CHF"
|
2017-12-29 22:32:27 +01:00
|
|
|
|
2018-01-28 21:31:27 +01:00
|
|
|
if not 'currency' in request.session:
|
|
|
|
request.session['currency'] = None
|
|
|
|
|
2017-12-29 22:32:27 +01:00
|
|
|
article = get_object_or_404(Article, pk=article_id)
|
2018-01-14 14:07:53 +01:00
|
|
|
picture_list = Picture.objects.filter(article=article_id)
|
2018-01-28 21:26:36 +01:00
|
|
|
|
|
|
|
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():
|
2018-02-04 20:03:09 +01:00
|
|
|
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']
|
2018-02-04 18:32:54 +01:00
|
|
|
currency_id = request.session['currency']
|
2018-02-04 18:58:21 +01:00
|
|
|
article = Article.objects.get(id=article_id)
|
|
|
|
try:
|
2018-02-11 23:49:36 +01:00
|
|
|
# lookup if cart_id is already existent:
|
2018-02-04 18:58:21 +01:00
|
|
|
cart_id = ShoppingCart.objects.get(user=request.user)
|
|
|
|
except:
|
2018-02-11 23:49:36 +01:00
|
|
|
# if cart_id is not existent create a cart:
|
2018-02-04 18:58:21 +01:00
|
|
|
cart_id = ShoppingCart.objects.create(user=request.user)
|
|
|
|
cart_id.save()
|
|
|
|
if cart_id:
|
2018-02-11 23:49:36 +01:00
|
|
|
# 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:
|
2018-02-13 08:27:25 +01:00
|
|
|
cart_position = CartPosition.objects.filter(
|
|
|
|
id=article_id).update(
|
2018-02-11 23:49:36 +01:00
|
|
|
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,
|
2018-02-13 08:27:25 +01:00
|
|
|
position_price=article.price_in_chf,
|
2018-02-11 23:49:36 +01:00
|
|
|
cart=ShoppingCart.objects.get(user=request.user)
|
|
|
|
)
|
|
|
|
cart_position.save()
|
|
|
|
# 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()
|
2018-01-28 21:26:36 +01:00
|
|
|
|
|
|
|
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)
|
2018-01-28 21:26:36 +01:00
|
|
|
|
2017-12-29 22:32:27 +01:00
|
|
|
return render(request, 'webshop/article_details.html',
|
|
|
|
{'article': article,
|
2018-01-14 14:07:53 +01:00
|
|
|
'category_list': category_list,
|
2018-01-28 21:26:36 +01:00
|
|
|
'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):
|
2018-01-28 20:25:35 +01:00
|
|
|
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',
|
2018-01-28 20:25:35 +01:00
|
|
|
{'person': person,
|
|
|
|
'category_list': category_list})
|
2017-12-29 16:46:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def registration(request):
|
2018-01-28 20:25:35 +01:00
|
|
|
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()):
|
2018-02-05 21:25:18 +01:00
|
|
|
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()
|
|
|
|
person = Person.objects.create(
|
|
|
|
salutation=pf['salutation'],
|
|
|
|
city=City.objects.get(zip_code=pf['zip_code'],
|
|
|
|
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,
|
2018-01-28 20:37:47 +01:00
|
|
|
'category_list': category_list,
|
2017-12-29 16:46:21 +01:00
|
|
|
'user_form': user_form})
|
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
|
2018-02-19 18:39:53 +01:00
|
|
|
cart_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 = ""
|
2018-02-19 19:11:40 +01:00
|
|
|
cart_position_list = ""
|
2018-02-11 16:48:45 +01:00
|
|
|
prices_in_cart = []
|
2018-02-11 22:37:27 +01:00
|
|
|
totalprice_list = []
|
2018-02-11 16:48:45 +01:00
|
|
|
total = 0
|
2018-02-04 18:32:54 +01:00
|
|
|
|
2018-02-19 19:11:40 +01:00
|
|
|
# here we configure the users Currency:
|
2018-02-04 18:32:54 +01:00
|
|
|
if not 'currency' in request.session:
|
|
|
|
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
|
|
|
|
2018-02-19 19:11:40 +01:00
|
|
|
# here we handle the normal cart view:
|
|
|
|
# if the cart_id is set the user has already added items to cart.
|
|
|
|
try:
|
|
|
|
cart_id = ShoppingCart.objects.get(user=request.user.id)
|
|
|
|
except Exception as e:
|
|
|
|
message = "You have no items in the Basket"
|
|
|
|
print('try cart_id exception as: ', e)
|
|
|
|
cart_id = False
|
|
|
|
if cart_id:
|
|
|
|
print('cart_id', cart_id)
|
|
|
|
articles = CartPosition.objects.filter(cart=cart_id)
|
|
|
|
cart_position_list = list(articles)
|
|
|
|
# scrap out the details to calculate Total of item and Summ of All:
|
|
|
|
for idx, cart_position in enumerate(cart_position_list):
|
|
|
|
article = CartPosition.objects.get(
|
|
|
|
cart=cart_id,
|
|
|
|
article=cart_position.article.id
|
|
|
|
)
|
|
|
|
cart_position.calculate_position_price()
|
|
|
|
if currency:
|
|
|
|
print('calc currency')
|
|
|
|
# get currencyname to display:
|
|
|
|
currency_name = ExchangeRate_name.objects.get(id=currency)
|
|
|
|
# get exchange_rate multiplyed:
|
|
|
|
cart_position.price_in_chf = rate.exchange(
|
|
|
|
currency,
|
|
|
|
cart_position.price_in_chf
|
|
|
|
)
|
|
|
|
totalprice_list.append(cart_position.price_in_chf)
|
|
|
|
cart_position_list[idx] = cart_position
|
|
|
|
|
|
|
|
amount_form = CartForm(
|
|
|
|
initial=cart_position.amount
|
|
|
|
)
|
|
|
|
|
|
|
|
total = sum(totalprice_list)
|
|
|
|
|
|
|
|
# Here we handle all POST Operations:
|
2018-02-04 18:32:54 +01:00
|
|
|
if request.method == 'POST':
|
2018-02-17 10:51:32 +01:00
|
|
|
print(request.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']:
|
|
|
|
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:
|
2018-02-19 19:11:40 +01:00
|
|
|
if 'amount_field' in request.POST:
|
2018-02-17 10:51:32 +01:00
|
|
|
print('yes amount post')
|
2018-02-19 19:11:40 +01:00
|
|
|
amount_form = CartForm(request.POST,
|
|
|
|
cart_id,
|
|
|
|
cart_position.article.id)
|
|
|
|
if amount_form.is_valid():
|
|
|
|
amount = amount_form.cleaned_data['amount']
|
2018-02-11 19:58:49 +01:00
|
|
|
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()
|
2018-02-17 10:51:32 +01:00
|
|
|
|
|
|
|
if 'checkout' in request.POST:
|
2018-02-18 19:28:39 +01:00
|
|
|
print('checkout')
|
2018-02-17 10:51:32 +01:00
|
|
|
checkout_form = CheckoutForm(request.POST)
|
|
|
|
if checkout_form.is_valid():
|
|
|
|
checkout_form = checkout_form.cleaned_data['checkout']
|
|
|
|
print('views checkout checkout_form', checkout_form)
|
|
|
|
if checkout_form is True:
|
|
|
|
# add to order
|
|
|
|
order = ''
|
2018-02-18 19:28:39 +01:00
|
|
|
|
2018-02-17 10:51:32 +01:00
|
|
|
checkout_form = CheckoutForm()
|
2018-02-11 18:19:21 +01:00
|
|
|
|
2018-02-04 18:32:54 +01:00
|
|
|
|
|
|
|
return render(request, 'webshop/cart.html',
|
2018-02-18 21:58:46 +01:00
|
|
|
{'cart_position_list': cart_position_list,
|
2018-02-11 21:56:53 +01:00
|
|
|
'totalprice_list': totalprice_list,
|
2018-02-04 21:48:29 +01:00
|
|
|
'total': total,
|
2018-02-19 18:39:53 +01:00
|
|
|
'cart_form': cart_form,
|
2018-02-04 18:32:54 +01:00
|
|
|
'currencies_form': currencies_form,
|
2018-02-19 19:11:40 +01:00
|
|
|
'amount_form': amount_form,
|
2018-02-04 18:32:54 +01:00
|
|
|
'article_view': article_view,
|
|
|
|
'currency_name': currency_name,
|
2018-02-04 18:58:21 +01:00
|
|
|
'category_list': category_list,
|
|
|
|
'message': message,
|
2018-02-04 18:32:54 +01:00
|
|
|
})
|