Files
santeh-ray/orders/models.py
Azimkin 52cbc1c2b3
All checks were successful
Build and Push Docker Image / build (push) Successful in 42s
feat: implement shopping cart feature with user authentication support
- Added `Cart` and `CartItem` models with migrations for managing user-specific carts and items
- Developed services for cart-related operations: adding, removing, and updating cart items
- Built specialized templates (`cart.html`) and styled them with new CSS assets (`cart.css`)
- Enabled real-time UI updates via AJAX in shopping cart interactions (`cart.js`)
- Modified header to dynamically display cart link with user authentication awareness
- Added Leaflet-based interactive map to the contacts page and integrated CSS/JS dependencies
- Expanded tests for cart functionality, item detail page cart integration, and contacts page maps
2026-05-15 23:18:16 +02:00

60 lines
1.8 KiB
Python

from django.conf import settings
from django.core.validators import MinValueValidator
from django.db import models
from items.models import Item
class Cart(models.Model):
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name="cart",
verbose_name="Пользователь",
)
created_at = models.DateTimeField(auto_now_add=True, verbose_name="Дата создания")
updated_at = models.DateTimeField(auto_now=True, verbose_name="Дата обновления")
class Meta:
verbose_name = "Корзина"
verbose_name_plural = "Корзины"
def __str__(self):
return f"Корзина пользователя {self.user}"
class CartItem(models.Model):
cart = models.ForeignKey(
Cart,
on_delete=models.CASCADE,
related_name="items",
verbose_name="Корзина",
)
item = models.ForeignKey(
Item,
on_delete=models.CASCADE,
related_name="cart_entries",
verbose_name="Товар",
)
quantity = models.PositiveIntegerField(
validators=[MinValueValidator(1)],
verbose_name="Количество",
)
created_at = models.DateTimeField(auto_now_add=True, verbose_name="Дата создания")
updated_at = models.DateTimeField(auto_now=True, verbose_name="Дата обновления")
class Meta:
ordering = ("created_at", "id")
constraints = [
models.UniqueConstraint(fields=("cart", "item"), name="unique_cart_item_per_cart"),
]
verbose_name = "Позиция корзины"
verbose_name_plural = "Позиции корзины"
def __str__(self):
return f"{self.item} x {self.quantity}"
@property
def subtotal(self):
return self.item.price * self.quantity