You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
148 lines
5.1 KiB
148 lines
5.1 KiB
from django.shortcuts import render, redirect, get_object_or_404
|
|
from django.contrib import messages
|
|
from .models import Product, Order, OrderItem, GuestUser
|
|
from django.db.models import Sum
|
|
from django.contrib.auth.forms import AuthenticationForm
|
|
from django.contrib.auth import login
|
|
from django.contrib.auth.decorators import login_required
|
|
from .forms import GuestCheckoutForm
|
|
|
|
from . import cart
|
|
|
|
# Create your views here.
|
|
def product_list(request):
|
|
products = Product.objects.all()
|
|
cart_items = cart.get_cart_items(request)
|
|
total = cart.get_cart_total(request)
|
|
return render(request, 'shop/product_list.html', {
|
|
'products': products,
|
|
'cart_items': cart_items,
|
|
'total': total
|
|
})
|
|
|
|
def view_cart(request):
|
|
"""Display the shopping cart"""
|
|
cart_items = cart.get_cart_items(request)
|
|
total = cart.get_cart_total(request)
|
|
total_quantity = cart_items.aggregate(total_quantity=Sum('quantity'))['total_quantity']
|
|
return render(request, 'shop/cart.html', {
|
|
'cart_items': cart_items,
|
|
'total': total,
|
|
'total_quantity': total_quantity
|
|
})
|
|
|
|
def add_to_cart_view(request, product_id):
|
|
"""Add a product to the cart"""
|
|
product = get_object_or_404(Product, id=product_id)
|
|
quantity = int(request.POST.get('quantity', 1))
|
|
color_id = request.POST.get('color')
|
|
size_id = request.POST.get('size')
|
|
|
|
cart_item = cart.add_to_cart(request, product_id, quantity, color_id, size_id)
|
|
messages.success(request, f'{cart_item.quantity} x {product.title} added to your cart')
|
|
|
|
return redirect('shop:product_list')
|
|
|
|
def update_cart_view(request, product_id):
|
|
"""Update cart item quantity"""
|
|
if request.method == 'POST':
|
|
quantity = int(request.POST.get('quantity', 0))
|
|
cart.update_cart_item(request, product_id, quantity)
|
|
return redirect('shop:view_cart')
|
|
|
|
def remove_from_cart_view(request, product_id):
|
|
"""Remove item from cart"""
|
|
cart.remove_from_cart(request, product_id)
|
|
return redirect('shop:view_cart')
|
|
|
|
def clear_cart(request):
|
|
"""Clear the cart"""
|
|
cart.clear_cart(request)
|
|
messages.success(request, "Your cart has been cleared.")
|
|
return redirect('shop:product_list')
|
|
|
|
def create_order(request):
|
|
cart_items = cart.get_cart_items(request)
|
|
total_price = sum(item.get_total_price() for item in cart_items)
|
|
|
|
if request.user.is_authenticated:
|
|
# L'utilisateur est authentifié, créer la commande avec l'utilisateur
|
|
order = Order.objects.create(
|
|
user=request.user,
|
|
total_price=total_price
|
|
)
|
|
else:
|
|
# L'utilisateur n'est pas authentifié, créer la commande avec l'utilisateur invité
|
|
try:
|
|
guest_user = GuestUser.objects.get(email=request.session['guest_email'])
|
|
order = Order.objects.create(
|
|
guest_user=guest_user,
|
|
total_price=total_price
|
|
)
|
|
except (KeyError, GuestUser.DoesNotExist):
|
|
# Si l'utilisateur invité n'existe pas, créer une commande sans utilisateur
|
|
order = Order.objects.create(
|
|
total_price=total_price
|
|
)
|
|
|
|
|
|
for cart_item in cart_items:
|
|
OrderItem.objects.create(
|
|
order=order,
|
|
product=cart_item.product,
|
|
quantity=cart_item.quantity,
|
|
color=cart_item.color,
|
|
size=cart_item.size,
|
|
price=cart_item.product.price
|
|
)
|
|
|
|
# Clear the cart after creating the order
|
|
cart.clear_cart(request)
|
|
|
|
return order
|
|
|
|
def order_confirmation(request, order_id):
|
|
order = get_object_or_404(Order, id=order_id)
|
|
order_items = order.items.all()
|
|
total = sum(item.get_total_price() for item in order_items)
|
|
return render(request, 'shop/order_confirmation.html', {
|
|
'order': order,
|
|
'order_items': order_items,
|
|
'total': total
|
|
})
|
|
|
|
def checkout(request):
|
|
if request.user.is_authenticated:
|
|
# Créer la commande pour l'utilisateur authentifié
|
|
order = create_order(request)
|
|
|
|
# Rediriger vers la confirmation de commande
|
|
return redirect('shop:order_confirmation', order.id)
|
|
|
|
if request.method == 'GET':
|
|
form = GuestCheckoutForm()
|
|
return render(request, 'shop/checkout.html', {'form': form})
|
|
|
|
elif request.method == 'POST':
|
|
# Gérer la soumission du formulaire ici
|
|
form = GuestCheckoutForm(request.POST)
|
|
if form.is_valid():
|
|
# Créer ou récupérer l'utilisateur invité
|
|
email = form.cleaned_data['email']
|
|
phone = form.cleaned_data['phone']
|
|
guest_user, created = GuestUser.objects.get_or_create(email=email, defaults={'phone': phone})
|
|
|
|
# Stocker l'e-mail de l'utilisateur invité dans la session
|
|
request.session['guest_email'] = email
|
|
|
|
# Simuler le processus de paiement
|
|
# ...
|
|
|
|
# Créer la commande
|
|
order = create_order(request)
|
|
|
|
# Rediriger vers la confirmation de commande
|
|
return redirect('shop:order_confirmation', order.id)
|
|
|
|
# Gérer les autres méthodes (par exemple, PUT, DELETE) si nécessaire
|
|
return redirect('shop:product_list')
|
|
|