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.
107 lines
3.5 KiB
Python
107 lines
3.5 KiB
Python
from django.contrib import admin
|
||
from django.utils.html import format_html
|
||
|
||
from .models import IdempotencyKey, OutboxMessage, SyncLog, SyncRun
|
||
|
||
|
||
class SyncLogInline(admin.TabularInline):
|
||
model = SyncLog
|
||
extra = 0
|
||
readonly_fields = ("level", "message", "payload", "created_at")
|
||
can_delete = False
|
||
|
||
|
||
@admin.register(SyncRun)
|
||
class SyncRunAdmin(admin.ModelAdmin):
|
||
list_display = (
|
||
"entity",
|
||
"direction",
|
||
"status_badge",
|
||
"started_at",
|
||
"finished_at",
|
||
"duration",
|
||
)
|
||
list_filter = ("entity", "direction", "status")
|
||
readonly_fields = ("started_at", "finished_at", "stats", "error")
|
||
inlines = [SyncLogInline]
|
||
actions = ("run_catalog_sync",)
|
||
|
||
@admin.display(description="Статус", ordering="status")
|
||
def status_badge(self, obj):
|
||
colors = {
|
||
SyncRun.Status.RUNNING: "#2563eb",
|
||
SyncRun.Status.SUCCESS: "#16a34a",
|
||
SyncRun.Status.PARTIAL: "#d97706",
|
||
SyncRun.Status.FAILED: "#dc2626",
|
||
}
|
||
return format_html(
|
||
'<span style="display:inline-block;padding:2px 8px;border-radius:10px;'
|
||
'background:{};color:#fff;font-size:12px;">{}</span>',
|
||
colors.get(obj.status, "#6b7280"),
|
||
obj.get_status_display(),
|
||
)
|
||
|
||
@admin.display(description="Длительность")
|
||
def duration(self, obj):
|
||
if not obj.finished_at:
|
||
return "—"
|
||
seconds = int((obj.finished_at - obj.started_at).total_seconds())
|
||
return f"{seconds} с"
|
||
|
||
@admin.action(description="Запустить синхронизацию каталога")
|
||
def run_catalog_sync(self, request, queryset):
|
||
from .tasks import sync_catalog_from_1c
|
||
|
||
try:
|
||
task = sync_catalog_from_1c.delay()
|
||
except Exception as exc: # noqa: BLE001 - show the broker error to the admin
|
||
self.message_user(
|
||
request, f"Не удалось поставить задачу: {exc}", level="error"
|
||
)
|
||
return
|
||
self.message_user(request, f"Задача поставлена в очередь: {task.id}")
|
||
|
||
|
||
@admin.register(SyncLog)
|
||
class SyncLogAdmin(admin.ModelAdmin):
|
||
list_display = ("run", "level", "message", "created_at")
|
||
list_filter = ("level",)
|
||
search_fields = ("message",)
|
||
|
||
|
||
@admin.register(OutboxMessage)
|
||
class OutboxMessageAdmin(admin.ModelAdmin):
|
||
list_display = (
|
||
"topic",
|
||
"status",
|
||
"attempts",
|
||
"next_attempt_at",
|
||
"sent_at",
|
||
"created_at",
|
||
)
|
||
list_filter = ("status", "topic")
|
||
search_fields = ("topic", "last_error")
|
||
readonly_fields = ("created_at", "updated_at")
|
||
actions = ("retry", "mark_dead")
|
||
|
||
@admin.action(description="Повторить отправку")
|
||
def retry(self, request, queryset):
|
||
updated = queryset.update(
|
||
status=OutboxMessage.Status.PENDING,
|
||
attempts=0,
|
||
next_attempt_at=None,
|
||
last_error="",
|
||
)
|
||
self.message_user(request, f"Поставлено в очередь: {updated}")
|
||
|
||
@admin.action(description="Пометить как dead-letter")
|
||
def mark_dead(self, request, queryset):
|
||
updated = queryset.update(status=OutboxMessage.Status.DEAD)
|
||
self.message_user(request, f"Помечено: {updated}")
|
||
|
||
|
||
@admin.register(IdempotencyKey)
|
||
class IdempotencyKeyAdmin(admin.ModelAdmin):
|
||
list_display = ("scope", "key", "status_code", "created_at")
|
||
search_fields = ("key", "scope")
|