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.
137 lines
4.4 KiB
Python
137 lines
4.4 KiB
Python
from django.contrib import admin
|
|
from django.utils.html import format_html
|
|
|
|
from apps.payments.models import Payment
|
|
|
|
from .models import Order, OrderItem, OrderStatusHistory
|
|
from .services import change_status
|
|
|
|
|
|
class OrderItemInline(admin.TabularInline):
|
|
model = OrderItem
|
|
extra = 0
|
|
readonly_fields = ("total",)
|
|
|
|
|
|
class OrderStatusHistoryInline(admin.TabularInline):
|
|
model = OrderStatusHistory
|
|
extra = 0
|
|
readonly_fields = ("created_at",)
|
|
|
|
|
|
class PaymentInline(admin.TabularInline):
|
|
model = Payment
|
|
extra = 0
|
|
readonly_fields = ("created_at", "paid_at")
|
|
|
|
|
|
@admin.register(Order)
|
|
class OrderAdmin(admin.ModelAdmin):
|
|
list_display = (
|
|
"number",
|
|
"customer_name",
|
|
"phone",
|
|
"status_badge",
|
|
"payment_status",
|
|
"total",
|
|
"created_at",
|
|
)
|
|
list_filter = ("status", "payment_status", "created_at")
|
|
search_fields = ("number", "phone", "email", "customer_name", "external_id")
|
|
readonly_fields = (
|
|
"number",
|
|
"external_id",
|
|
"synced_at",
|
|
"sync_error",
|
|
"created_at",
|
|
"updated_at",
|
|
)
|
|
date_hierarchy = "created_at"
|
|
inlines = [OrderItemInline, OrderStatusHistoryInline, PaymentInline]
|
|
actions = ("mark_processing", "mark_delivering", "mark_completed", "mark_cancelled")
|
|
fieldsets = (
|
|
(
|
|
"Заказ",
|
|
{"fields": ("number", "external_id", "user", "status", "payment_status")},
|
|
),
|
|
("Клиент", {"fields": ("customer_name", "phone", "email")}),
|
|
(
|
|
"Доставка",
|
|
{"fields": ("delivery_method", "delivery_address", "delivery_cost")},
|
|
),
|
|
(
|
|
"Суммы",
|
|
{
|
|
"fields": (
|
|
"subtotal",
|
|
"discount_total",
|
|
"bonus_applied",
|
|
"certificate_applied",
|
|
"total",
|
|
"promo_code",
|
|
"certificate_code",
|
|
)
|
|
},
|
|
),
|
|
(
|
|
"Служебное",
|
|
{"fields": ("comment", "synced_at", "sync_error", "created_at", "updated_at")},
|
|
),
|
|
)
|
|
|
|
@admin.display(description="Статус", ordering="status")
|
|
def status_badge(self, obj):
|
|
colors = {
|
|
Order.Status.NEW: "#6b7280",
|
|
Order.Status.PROCESSING: "#d97706",
|
|
Order.Status.DELIVERING: "#2563eb",
|
|
Order.Status.COMPLETED: "#16a34a",
|
|
Order.Status.CANCELLED: "#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(),
|
|
)
|
|
|
|
def _change(self, request, queryset, status, label):
|
|
count = 0
|
|
for order in queryset:
|
|
change_status(
|
|
order,
|
|
status,
|
|
source=OrderStatusHistory.Source.ADMIN,
|
|
comment=f"Администратор: {label}",
|
|
)
|
|
count += 1
|
|
self.message_user(request, f"Обновлено заказов: {count}")
|
|
|
|
@admin.action(description="Перевести в «В обработке»")
|
|
def mark_processing(self, request, queryset):
|
|
self._change(request, queryset, Order.Status.PROCESSING, "в обработку")
|
|
|
|
@admin.action(description="Перевести в «Доставляется»")
|
|
def mark_delivering(self, request, queryset):
|
|
self._change(request, queryset, Order.Status.DELIVERING, "в доставку")
|
|
|
|
@admin.action(description="Перевести в «Выполнен»")
|
|
def mark_completed(self, request, queryset):
|
|
self._change(request, queryset, Order.Status.COMPLETED, "выполнен")
|
|
|
|
@admin.action(description="Отменить выбранные")
|
|
def mark_cancelled(self, request, queryset):
|
|
self._change(request, queryset, Order.Status.CANCELLED, "отменён")
|
|
|
|
|
|
@admin.register(OrderItem)
|
|
class OrderItemAdmin(admin.ModelAdmin):
|
|
list_display = ("order", "name", "sku", "price", "quantity", "total")
|
|
search_fields = ("order__number", "name", "sku")
|
|
|
|
|
|
@admin.register(OrderStatusHistory)
|
|
class OrderStatusHistoryAdmin(admin.ModelAdmin):
|
|
list_display = ("order", "status", "source", "created_at")
|
|
list_filter = ("status", "source")
|