Backend (Django 4.2 + DRF): - accounts: phone OTP login, JWT, profile, addresses - catalog: categories, products, images, filters, search, hits/new/related - orders: creation with Idempotency-Key, stock reservation, status history, cancel - loyalty: bonus accounts, promo codes, gift certificates - payments: payment records and YooKassa webhook handling - content: static pages, banners, site settings - integration: 1C exchange (catalog sync, order push via outbox, statuses, bonuses) - common: error envelope, pagination, request-id, integration auth, admin dashboard Infrastructure: Dockerfile, docker-compose (Postgres, Redis, Celery worker/beat, Nginx), requirements split by environment, OpenAPI contracts for site and 1C, architecture doc. Tests: 22 pytest cases covering catalog, orders, idempotency, OTP, integration.
133 lines
4.4 KiB
Python
133 lines
4.4 KiB
Python
from django.db import models
|
||
|
||
from apps.common.models import TimeStampedModel
|
||
from apps.common.utils import make_slug
|
||
|
||
|
||
class Category(TimeStampedModel):
|
||
"""Catalog category. Source of truth is 1C."""
|
||
|
||
external_id = models.CharField(max_length=64, unique=True)
|
||
parent = models.ForeignKey(
|
||
"self",
|
||
null=True,
|
||
blank=True,
|
||
on_delete=models.SET_NULL,
|
||
related_name="children",
|
||
)
|
||
name = models.CharField(max_length=255)
|
||
slug = models.SlugField(max_length=255, unique=True)
|
||
description = models.TextField(blank=True)
|
||
image_url = models.URLField(max_length=1000, blank=True)
|
||
is_active = models.BooleanField(default=True)
|
||
sort_order = models.IntegerField(default=0)
|
||
|
||
class Meta:
|
||
db_table = "catalog_category"
|
||
verbose_name = "категория"
|
||
verbose_name_plural = "категории"
|
||
ordering = ["sort_order", "name"]
|
||
|
||
def __str__(self) -> str:
|
||
return self.name
|
||
|
||
def save(self, *args, **kwargs):
|
||
if not self.slug:
|
||
self.slug = make_slug(self.name)
|
||
super().save(*args, **kwargs)
|
||
|
||
|
||
class Product(TimeStampedModel):
|
||
"""Product card. Price, stock and descriptions come from 1C."""
|
||
|
||
class StockStatus(models.TextChoices):
|
||
IN_STOCK = "in_stock", "В наличии"
|
||
PREORDER = "preorder", "Под заказ"
|
||
OUT_OF_STOCK = "out_of_stock", "Нет в наличии"
|
||
|
||
external_id = models.CharField(max_length=64, unique=True)
|
||
sku = models.CharField("артикул", max_length=64, db_index=True)
|
||
name = models.CharField(max_length=500)
|
||
slug = models.SlugField(max_length=500, unique=True)
|
||
category = models.ForeignKey(
|
||
Category,
|
||
null=True,
|
||
blank=True,
|
||
on_delete=models.SET_NULL,
|
||
related_name="products",
|
||
)
|
||
brand = models.CharField(max_length=255, blank=True)
|
||
short_description = models.TextField(blank=True)
|
||
description = models.TextField(blank=True)
|
||
price = models.DecimalField(max_digits=12, decimal_places=2)
|
||
old_price = models.DecimalField(
|
||
max_digits=12, decimal_places=2, null=True, blank=True
|
||
)
|
||
stock = models.IntegerField(default=0)
|
||
stock_status = models.CharField(
|
||
max_length=20,
|
||
choices=StockStatus.choices,
|
||
default=StockStatus.OUT_OF_STOCK,
|
||
)
|
||
is_hit = models.BooleanField("хит продаж", default=False)
|
||
is_new = models.BooleanField("новинка", default=False)
|
||
is_active = models.BooleanField(default=True)
|
||
attributes = models.JSONField(default=dict, blank=True)
|
||
external_updated_at = models.DateTimeField(null=True, blank=True)
|
||
|
||
class Meta:
|
||
db_table = "catalog_product"
|
||
verbose_name = "товар"
|
||
verbose_name_plural = "товары"
|
||
ordering = ["-created_at"]
|
||
indexes = [
|
||
models.Index(fields=["category", "is_active"]),
|
||
models.Index(fields=["is_hit"]),
|
||
models.Index(fields=["is_new"]),
|
||
models.Index(fields=["price"]),
|
||
]
|
||
|
||
def __str__(self) -> str:
|
||
return f"{self.sku} — {self.name}"
|
||
|
||
def save(self, *args, **kwargs):
|
||
if not self.slug:
|
||
self.slug = make_slug(self.name)
|
||
super().save(*args, **kwargs)
|
||
|
||
@property
|
||
def main_image(self) -> str | None:
|
||
images = list(self.images.all())
|
||
for image in images:
|
||
if image.is_main:
|
||
return image.url
|
||
return images[0].url if images else None
|
||
|
||
def recalculate_stock_status(self) -> str:
|
||
if self.stock > 0:
|
||
self.stock_status = self.StockStatus.IN_STOCK
|
||
elif self.stock_status == self.StockStatus.PREORDER:
|
||
self.stock_status = self.StockStatus.PREORDER
|
||
else:
|
||
self.stock_status = self.StockStatus.OUT_OF_STOCK
|
||
return self.stock_status
|
||
|
||
|
||
class ProductImage(TimeStampedModel):
|
||
product = models.ForeignKey(
|
||
Product, on_delete=models.CASCADE, related_name="images"
|
||
)
|
||
url = models.URLField(max_length=1000)
|
||
alt = models.CharField(max_length=255, blank=True)
|
||
sort_order = models.IntegerField(default=0)
|
||
is_main = models.BooleanField(default=False)
|
||
|
||
class Meta:
|
||
db_table = "catalog_product_image"
|
||
verbose_name = "изображение товара"
|
||
verbose_name_plural = "изображения товаров"
|
||
ordering = ["sort_order", "id"]
|
||
|
||
def __str__(self) -> str:
|
||
return self.url
|