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.
79 lines
2.4 KiB
Python
79 lines
2.4 KiB
Python
from rest_framework.permissions import AllowAny, IsAuthenticated
|
|
from rest_framework.response import Response
|
|
from rest_framework.views import APIView
|
|
|
|
from .models import GiftCertificate
|
|
from .serializers import (
|
|
BonusAccountSerializer,
|
|
CertificateValidateSerializer,
|
|
GiftCertificateSerializer,
|
|
PromoValidateSerializer,
|
|
)
|
|
from .services import (
|
|
get_or_create_bonus_account,
|
|
validate_certificate,
|
|
validate_promo,
|
|
)
|
|
|
|
|
|
class BonusView(APIView):
|
|
permission_classes = [IsAuthenticated]
|
|
serializer_class = BonusAccountSerializer
|
|
|
|
def get(self, request):
|
|
account = get_or_create_bonus_account(request.user)
|
|
transactions = account.transactions.all()[:50]
|
|
serializer = BonusAccountSerializer(
|
|
{"balance": account.balance, "transactions": transactions}
|
|
)
|
|
return Response(serializer.data)
|
|
|
|
|
|
class PromoValidateView(APIView):
|
|
permission_classes = [AllowAny]
|
|
serializer_class = PromoValidateSerializer
|
|
|
|
def post(self, request):
|
|
serializer = PromoValidateSerializer(data=request.data)
|
|
serializer.is_valid(raise_exception=True)
|
|
promo, discount = validate_promo(
|
|
serializer.validated_data["code"],
|
|
serializer.validated_data["subtotal"],
|
|
)
|
|
return Response(
|
|
{
|
|
"code": promo.code,
|
|
"discount": discount,
|
|
"discount_type": promo.discount_type,
|
|
}
|
|
)
|
|
|
|
|
|
class CertificateValidateView(APIView):
|
|
permission_classes = [AllowAny]
|
|
serializer_class = CertificateValidateSerializer
|
|
|
|
def post(self, request):
|
|
serializer = CertificateValidateSerializer(data=request.data)
|
|
serializer.is_valid(raise_exception=True)
|
|
certificate, applied = validate_certificate(
|
|
serializer.validated_data["code"],
|
|
serializer.validated_data["amount"],
|
|
)
|
|
return Response(
|
|
{
|
|
"code": certificate.code,
|
|
"applied": applied,
|
|
"remaining": certificate.balance - applied,
|
|
}
|
|
)
|
|
|
|
|
|
class CertificateListView(APIView):
|
|
permission_classes = [IsAuthenticated]
|
|
serializer_class = GiftCertificateSerializer
|
|
|
|
def get(self, request):
|
|
certificates = GiftCertificate.objects.filter(issued_to=request.user)
|
|
return Response(GiftCertificateSerializer(certificates, many=True).data)
|