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.
67 lines
2.0 KiB
Python
67 lines
2.0 KiB
Python
from rest_framework import viewsets
|
|
from rest_framework.decorators import action
|
|
from rest_framework.response import Response
|
|
|
|
from .filters import ProductFilter
|
|
from .models import Category, Product
|
|
from .serializers import (
|
|
CategorySerializer,
|
|
ProductDetailSerializer,
|
|
ProductListSerializer,
|
|
)
|
|
|
|
|
|
class CategoryViewSet(viewsets.ReadOnlyModelViewSet):
|
|
queryset = Category.objects.filter(is_active=True).select_related("parent")
|
|
serializer_class = CategorySerializer
|
|
lookup_field = "slug"
|
|
pagination_class = None
|
|
|
|
|
|
class ProductViewSet(viewsets.ReadOnlyModelViewSet):
|
|
queryset = (
|
|
Product.objects.filter(is_active=True)
|
|
.select_related("category")
|
|
.prefetch_related("images")
|
|
)
|
|
serializer_class = ProductListSerializer
|
|
lookup_field = "slug"
|
|
filterset_class = ProductFilter
|
|
search_fields = ["name", "sku", "brand", "description"]
|
|
ordering_fields = ["price", "created_at", "name"]
|
|
ordering = ["-created_at"]
|
|
|
|
def get_serializer_class(self):
|
|
if self.action == "retrieve":
|
|
return ProductDetailSerializer
|
|
return ProductListSerializer
|
|
|
|
def get_queryset(self):
|
|
queryset = super().get_queryset()
|
|
if self.action == "hits":
|
|
queryset = queryset.filter(is_hit=True)
|
|
elif self.action == "new":
|
|
queryset = queryset.filter(is_new=True)
|
|
return queryset
|
|
|
|
@action(detail=False, methods=["get"])
|
|
def hits(self, request):
|
|
return self.list(request)
|
|
|
|
@action(detail=False, methods=["get"])
|
|
def new(self, request):
|
|
return self.list(request)
|
|
|
|
@action(detail=True, methods=["get"])
|
|
def related(self, request, slug=None):
|
|
product = self.get_object()
|
|
queryset = (
|
|
Product.objects.filter(is_active=True, category=product.category)
|
|
.exclude(pk=product.pk)
|
|
.prefetch_related("images")[:8]
|
|
)
|
|
serializer = ProductListSerializer(
|
|
queryset, many=True, context={"request": request}
|
|
)
|
|
return Response(serializer.data)
|