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.
118 lines
4.1 KiB
Python
118 lines
4.1 KiB
Python
from django.db import models
|
||
|
||
from apps.common.models import TimeStampedModel
|
||
|
||
|
||
class SyncRun(TimeStampedModel):
|
||
"""One exchange session with 1C (inbound or outbound)."""
|
||
|
||
class Direction(models.TextChoices):
|
||
INBOUND = "inbound", "1С → сайт"
|
||
OUTBOUND = "outbound", "Сайт → 1С"
|
||
|
||
class Entity(models.TextChoices):
|
||
CATALOG = "catalog", "Каталог"
|
||
ORDERS = "orders", "Заказы"
|
||
STATUSES = "statuses", "Статусы"
|
||
CUSTOMERS = "customers", "Клиенты"
|
||
BONUSES = "bonuses", "Бонусы"
|
||
|
||
class Status(models.TextChoices):
|
||
RUNNING = "running", "Выполняется"
|
||
SUCCESS = "success", "Успешно"
|
||
PARTIAL = "partial", "Частично"
|
||
FAILED = "failed", "Ошибка"
|
||
|
||
direction = models.CharField(max_length=10, choices=Direction.choices)
|
||
entity = models.CharField(max_length=20, choices=Entity.choices)
|
||
status = models.CharField(
|
||
max_length=10, choices=Status.choices, default=Status.RUNNING
|
||
)
|
||
started_at = models.DateTimeField()
|
||
finished_at = models.DateTimeField(null=True, blank=True)
|
||
stats = models.JSONField(default=dict, blank=True)
|
||
error = models.TextField(blank=True)
|
||
|
||
class Meta:
|
||
db_table = "integration_sync_run"
|
||
verbose_name = "сеанс обмена"
|
||
verbose_name_plural = "сеансы обмена"
|
||
ordering = ["-started_at"]
|
||
indexes = [models.Index(fields=["entity", "started_at"])]
|
||
|
||
def __str__(self) -> str:
|
||
return f"{self.get_entity_display()} ({self.get_status_display()})"
|
||
|
||
|
||
class SyncLog(TimeStampedModel):
|
||
class Level(models.TextChoices):
|
||
DEBUG = "debug", "Debug"
|
||
INFO = "info", "Info"
|
||
WARNING = "warning", "Warning"
|
||
ERROR = "error", "Error"
|
||
|
||
run = models.ForeignKey(
|
||
SyncRun, on_delete=models.CASCADE, related_name="logs"
|
||
)
|
||
level = models.CharField(
|
||
max_length=10, choices=Level.choices, default=Level.INFO
|
||
)
|
||
message = models.TextField()
|
||
payload = models.JSONField(default=dict, blank=True)
|
||
|
||
class Meta:
|
||
db_table = "integration_sync_log"
|
||
verbose_name = "запись журнала обмена"
|
||
verbose_name_plural = "журнал обмена"
|
||
ordering = ["created_at"]
|
||
|
||
def __str__(self) -> str:
|
||
return f"[{self.level}] {self.message[:80]}"
|
||
|
||
|
||
class OutboxMessage(TimeStampedModel):
|
||
"""Transactional outbox for reliable site → 1C delivery."""
|
||
|
||
class Status(models.TextChoices):
|
||
PENDING = "pending", "Ожидает"
|
||
SENT = "sent", "Отправлено"
|
||
FAILED = "failed", "Ошибка"
|
||
DEAD = "dead", "Исчерпаны попытки"
|
||
|
||
topic = models.CharField(max_length=64)
|
||
payload = models.JSONField(default=dict)
|
||
status = models.CharField(
|
||
max_length=10, choices=Status.choices, default=Status.PENDING
|
||
)
|
||
attempts = models.PositiveSmallIntegerField(default=0)
|
||
next_attempt_at = models.DateTimeField(null=True, blank=True)
|
||
last_error = models.TextField(blank=True)
|
||
sent_at = models.DateTimeField(null=True, blank=True)
|
||
|
||
class Meta:
|
||
db_table = "integration_outbox_message"
|
||
verbose_name = "сообщение outbox"
|
||
verbose_name_plural = "сообщения outbox"
|
||
ordering = ["created_at"]
|
||
indexes = [models.Index(fields=["status", "next_attempt_at"])]
|
||
|
||
def __str__(self) -> str:
|
||
return f"{self.topic} ({self.status})"
|
||
|
||
|
||
class IdempotencyKey(TimeStampedModel):
|
||
"""Stores responses for repeated inbound requests."""
|
||
|
||
key = models.CharField(max_length=128, unique=True)
|
||
scope = models.CharField(max_length=64)
|
||
status_code = models.PositiveSmallIntegerField(default=200)
|
||
response = models.JSONField(default=dict, blank=True)
|
||
|
||
class Meta:
|
||
db_table = "integration_idempotency_key"
|
||
verbose_name = "ключ идемпотентности"
|
||
verbose_name_plural = "ключи идемпотентности"
|
||
|
||
def __str__(self) -> str:
|
||
return f"{self.scope}:{self.key}"
|