Harmony/cart/cart.py

80 lines
2.7 KiB
Python
Raw Normal View History

2024-02-19 05:43:38 +00:00
from decimal import Decimal
from django.conf import settings
from main.models import Product
class Cart:
def __init__(self, request):
"""
Initialize the cart.
"""
self.session = request.session
2024-02-24 06:59:47 +00:00
user_id = self.session.get('keycloak_user_id') # Используйте идентификатор пользователя из сессии
if user_id:
cart_session_id = f"{settings.CART_SESSION_ID}_{user_id}" # Создайте уникальный ключ сессии для корзины
else:
cart_session_id = settings.CART_SESSION_ID
cart = self.session.get(cart_session_id)
2024-02-19 05:43:38 +00:00
if not cart:
# save an empty cart in the session
2024-02-24 06:59:47 +00:00
cart = self.session[cart_session_id] = {}
2024-02-19 05:43:38 +00:00
self.cart = cart
2024-02-24 06:59:47 +00:00
self.cart_session_id = cart_session_id
2024-02-19 05:43:38 +00:00
def __iter__(self):
"""
Iterate over the items in the cart and get the products
from the database.
"""
2024-02-24 06:59:47 +00:00
product_ids = self.cart.keys() # get the product objects and add them to the cart
2024-02-19 05:43:38 +00:00
products = Product.objects.filter(id__in=product_ids)
cart = self.cart.copy()
for product in products:
cart[str(product.id)]['product'] = product
for item in cart.values():
item['price'] = Decimal(item['price'])
item['total_price'] = item['price'] * item['quantity']
yield item
def __len__(self):
"""
Count all items in the cart.
"""
return sum(item['quantity'] for item in self.cart.values())
def add(self, product, quantity=1, override_quantity=False):
"""
Add a product to the cart or update its quantity.
"""
product_id = str(product.id)
if product_id not in self.cart:
self.cart[product_id] = {'quantity': 0,
'price': str(product.price)}
if override_quantity:
self.cart[product_id]['quantity'] = quantity
else:
self.cart[product_id]['quantity'] += quantity
self.save()
def save(self):
# mark the session as "modified" to make sure it gets saved
self.session.modified = True
2024-02-24 06:59:47 +00:00
self.session[self.cart_session_id] = self.cart
2024-02-19 05:43:38 +00:00
def remove(self, product):
"""
Remove a product from the cart.
"""
product_id = str(product.id)
if product_id in self.cart:
del self.cart[product_id]
self.save()
def clear(self):
# remove cart from session
del self.session[settings.CART_SESSION_ID]
self.save()
def get_total_price(self):
return sum(Decimal(item['price']) * item['quantity'] for item in self.cart.values())